文件下载保存到本地

寒冰意志 2008-12-29 12:23:08

保存网络文件【百度的图片】到本地保存成功后, 说格式不对, 打开代码是

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head>
<meta http-equiv="Cache-Control" content="max-age=0" forua="true"/>
<meta http-equiv="Cache-Control" content="no-cache" forua="true"/>
<meta http-equiv="Cache-Control" content="must-revalidate" forua="true"/>
</head>
<card id="card" title="" >
<onevent type="onenterforward">
<go href="http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg?t=00578"></go></onevent>
</card>
</wml>

有人见过吗,这是什么问题吗。
...全文
223 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxf209 2009-03-09
  • 打赏
  • 举报
回复
可参考MID Profile, 用POST方式访问,
os.flush() 在真机中记得加上, 模拟器中要注释掉

void postViaHttpConnection(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;

try {
c = (HttpConnection)Connector.open(url);

// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("If-Modified-Since",
"29 Oct 1999 19:43:31 GMT");
c.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");

// Getting the output stream may flush the headers
os = c.openOutputStream();
os.write("LIST games\n".getBytes());
os.flush(); // Optional, openInputStream will flush

// Opening the InputStream will open the connection
// and read the HTTP headers. They are stored until
// requested.
is = c.openInputStream();

// Get the ContentType
String type = c.getType();
processType(type);

// Get the length and process the data
int len = (int)c.getLength();
if (len > 0) {
byte[] data = new byte[len];
int actual = is.read(data);
process(data);
} else {
int ch;
while ((ch = is.read()) != -1) {
process((byte)ch);
}
}
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
if (c != null)
c.close();
}
}
lixiurui 2009-02-12
  • 打赏
  • 举报
回复
那个是移动的资费提醒页面,把请求重发一次就能获取到数据了
stanleystanley 2009-02-11
  • 打赏
  • 举报
回复
用cmnet就没有这个问题了
yesvery 2009-02-07
  • 打赏
  • 举报
回复
关注中
dxj1234 2009-02-06
  • 打赏
  • 举报
回复
是否是网络设置的问题,换成cmnet的网络试试,可能真机上选的是cmwap网络,访问了wap网关,baidu可能没有在wap网提供这个服务。

(楼上写的代码不是一样的功能嘛?)
cccloveyf 2009-01-05
  • 打赏
  • 举报
回复


import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.io.*;

public class Midlet1 extends MIDlet {

private static Midlet1 instance;

public Midlet1() {
instance = this;
}

public void startApp() {
try {
String url = "http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg";
String root = System.getProperty("fileconn.dir.photos") + "baidu.jpg";
HttpConnection connection = (HttpConnection) Connector.open(url);
int state = connection.getResponseCode();
if (state == HttpConnection.HTTP_OK) {
InputStream is = connection.openInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int count = is.read(b);
while (count > 0) {
baos.write(b, 0, count);
count = is.read(b);
}

byte[] buffer = baos.toByteArray();
FileConnection fc = (FileConnection) Connector.open(root, Connector.READ_WRITE);
if (!fc.exists()) {
fc.create();
OutputStream out = fc.openOutputStream();
out.write(buffer);
out.flush();
out.close();
}
fc.close();
}
connection.close();
} catch (IOException ex) {
ex.printStackTrace();
}
quitApp();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
}
noenoughmemory 2008-12-29
  • 打赏
  • 举报
回复
帮顶
寒冰意志 2008-12-29
  • 打赏
  • 举报
回复
在模拟器上没问题 ,真机器上就不行 。
public void startApp() {
try {
display = Display.getDisplay(this);
String url = "http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg";
String root = "file:///c:/Images/baidu.jpg";
HttpConnection hc = (HttpConnection) Connector.open(url);

int state = hc.getResponseCode();
if(state == HttpConnection.HTTP_OK){
InputStream is = hc.openInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buffer = null;
int chr = 0;
while((chr=is.read())!=-1){
baos.write(chr);
}

buffer = baos.toByteArray();
FileConnection fc = (FileConnection) Connector.open( root, Connector.READ_WRITE);
fc.create();
OutputStream out = fc.openOutputStream();
out.write(buffer);
out.flush();
out.close();
fc.close();
}

} catch (IOException ex) {
ex.printStackTrace();
}
}

代码比较粗糙。 主要是为了测试。

在 hc.getHeaderField("content-type"); 在真机上是 "text/vnd.wap.wml" 在模拟器上是 "image/jpeg"

cccloveyf 2008-12-29
  • 打赏
  • 举报
回复
你的代码贴过来看看。
我请求http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg?t=00578地址没问题。
送过来的直接就是一个图片,没有WAP页啊。
content-type:image/jpeg
content-length:3897
server:Apache
寒冰意志 2008-12-29
  • 打赏
  • 举报
回复
我改成 http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg?t=00578

xml 又变成


<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head>
<meta http-equiv="Cache-Control" content="max-age=0" forua="true"/>
<meta http-equiv="Cache-Control" content="no-cache" forua="true"/>
<meta http-equiv="Cache-Control" content="must-revalidate" forua="true"/>
</head>
<card id="card" title="" >
<onevent type="onenterforward">
<go href="http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg?t=00578&t=49168"></go></onevent>
</card>
</wml>


谁能告诉我这是什么原因呢。
寒冰意志 2008-12-29
  • 打赏
  • 举报
回复
我改成 http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg?t=00578

xml 又变成


<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head>
<meta http-equiv="Cache-Control" content="max-age=0" forua="true"/>
<meta http-equiv="Cache-Control" content="no-cache" forua="true"/>
<meta http-equiv="Cache-Control" content="must-revalidate" forua="true"/>
</head>
<card id="card" title="" >
<onevent type="onenterforward">
<go href="http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg?t=00578&t=49168"></go></onevent>
</card>
</wml>


谁能告诉我这是什么原因呢。
寒冰意志 2008-12-29
  • 打赏
  • 举报
回复
这的原理 我还不太明白。 我是下载的图片,却变成了XML 我在模拟器上就能下载, 到真机上就这错。
怎么会生成一个XML文件呢。 我想看相关的资料怎么查呢。
kf156 2008-12-29
  • 打赏
  • 举报
回复
你那wml里的
<go href="http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg?t=00578"> </go>
寒冰意志 2008-12-29
  • 打赏
  • 举报
回复
http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg?t=00578 这个地址是怎么来的那。
socool627 2008-12-29
  • 打赏
  • 举报
回复
看看
cccloveyf 2008-12-29
  • 打赏
  • 举报
回复
下载时为什么不直接请求http://wap.baidu.com/r/wise/wapsearchindex/logoindex.jpg?t=00578个地址呢?

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧