各位好,本人初学Android,在用Http下载文件时出现一个奇怪的问题。
1.下面代码中得urlStr是正确的,我用IE和迅雷试过都可以下载。
2.在模拟器上调试没问题
3.在真机上调试时候如果用我们自己的服务器下载url为:http://**.**.**.**:8089/androiddata/dishes.zip会报文件未找到,可是吧url随便换位网络上的地址,例如http://c.csdn.net/bbs/t/5/i/pic_logo.gif就可以。这些问题在模拟器上都不会出现。请高手们帮忙看下,先谢谢了! public boolean downLoadFile(String urlStr,String path,String fileName) throws IOException{
url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int contentLength = con.getContentLength();
int len;
byte[] bs= new byte[1024];
try
{
InputStream is = con.getInputStream(); //这句异常,FileNotFoundException
if (new Functions().isFolderExists(path) == false){
new File(path).mkdir();
}
OutputStream os = new FileOutputStream(new File(path+ fileName));
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
is.close();
return true;
}
catch (IOException e)
{
System.out.println("12345"+e.toString());
return false;
}
}
1.下面代码中得urlStr是正确的,我用IE和迅雷试过都可以下载。
2.在模拟器上调试没问题
3.在真机上调试时候如果用我们自己的服务器下载url为:http://**.**.**.**:8089/androiddata/dishes.zip会报文件未找到,可是吧url随便换位网络上的地址,例如http://c.csdn.net/bbs/t/5/i/pic_logo.gif就可以。这些问题在模拟器上都不会出现。请高手们帮忙看下,先谢谢了! public boolean downLoadFile(String urlStr,String path,String fileName) throws IOException{
url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int contentLength = con.getContentLength();
int len;
byte[] bs= new byte[1024];
try
{
InputStream is = con.getInputStream(); //这句异常,FileNotFoundException
if (new Functions().isFolderExists(path) == false){
new File(path).mkdir();
}
OutputStream os = new FileOutputStream(new File(path+ fileName));
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
is.close();
return true;
}
catch (IOException e)
{
System.out.println("12345"+e.toString());
return false;
}
}
执行这句的时候,就报错了。。 为什么系统提示是你说的那句呢。 因为啊。。 outputStream有错,系统就会爆 对象错误的。。