下载的代码如下,
这是一段测试代码,下载到本地的APK文件老是损坏的。直接输地址到浏览器下载下来的则是完整的。我觉得网络的不稳定性是一个因素,但是肯定是可以通过改进代码排除网络因素造成的影响的。求大侠指教!public static void main(String[] args) {
InputStream is = null;
HttpURLConnection conn=null;
try {
URL url = new URL("http://www.dubblogs.cc:8751/Android/Test/Apk/EX04_14.apk");
conn = (HttpURLConnection) url.openConnection();
conn.connect();
is = conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos = null;
BufferedInputStream bis = null;
try {
fos = new FileOutputStream("e:/test4.apk");
byte[] buf = new byte[1024];
bis = new BufferedInputStream(is);
while (bis.read(buf,0,1024) != -1) {
fos.write(buf);
System.out.println(buf);
}
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (fos != null)
fos.close();
if (bis != null)
bis.close();
if (is != null)
is.close();
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是一段测试代码,下载到本地的APK文件老是损坏的。直接输地址到浏览器下载下来的则是完整的。我觉得网络的不稳定性是一个因素,但是肯定是可以通过改进代码排除网络因素造成的影响的。求大侠指教!public static void main(String[] args) {
InputStream is = null;
HttpURLConnection conn=null;
try {
URL url = new URL("http://www.dubblogs.cc:8751/Android/Test/Apk/EX04_14.apk");
conn = (HttpURLConnection) url.openConnection();
conn.connect();
is = conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fos = null;
BufferedInputStream bis = null;
try {
fos = new FileOutputStream("e:/test4.apk");
byte[] buf = new byte[1024];
bis = new BufferedInputStream(is);
while (bis.read(buf,0,1024) != -1) {
fos.write(buf);
System.out.println(buf);
}
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (fos != null)
fos.close();
if (bis != null)
bis.close();
if (is != null)
is.close();
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
解决方案 »
- 急!!多国语言切换
- 谁能给个照片上传的代码
- 要疯了 还是蓝牙的问题啊 求教高人帮忙啊
- android中怎么定义旋转动画的旋转速度啊?
- 翻页效果的实现
- 既然:Android禁止其他子线程来更新由UI thread创建的试图
- java.lang.VerifyError: com/example/weipan/utils/OAuth
- 安卓学习体会5:安卓手机录音试验
- 如何从一个非Activity跳转到Activity,比如从controller跳转到Activity?
- 造起来,茄泥们!联想茄子快传创意征集大赛期待您的参与~
- Tabactivity中如何同时加载全部子activity?
- 怎么让短信界面看见发送方为英文
int size = -1;
while ((size = bis.read(buf)) != -1) {
fos.write(buf, 0, size);
Log.e(TAG, buf + ":buf");
}这样就好了
fos.write(buf);
System.out.println(buf);
}
read每次读取1024,返回值为实际读取的大小
写入流时必须写入实际大小
fos.write(buf,o,size);