各位好,本人初学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;
}
}
解决方案 »
- 关于view的OnMesure(int w , int h)方法,求大虾相助!
- android程序被退出问题
- [百撕不得骑姐:求教]android JNI调用本地C串口通讯
- SurfaceView与ListView的focus问题
- android下大家都用什么数据库框架?
- 请教服务器端开发的问题
- android setContentView()方法报错
- 关于cocos2dxActivity监听不到返回健的问题
- android.support.v4.app.FragmentTabHost是什么意思啊
- Android碎片中的多按钮的监听问题
- layout_gravity也garvity的区别
- android对图像的处理
执行这句的时候,就报错了。。 为什么系统提示是你说的那句呢。 因为啊。。 outputStream有错,系统就会爆 对象错误的。。