下载的代码如下,
这是一段测试代码,下载到本地的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();
}
}
}