在我的应用程序中从Web下载大量文件时,它们大约是200Mb文件(Zipped).所以任何人都可以告诉我下载文件的最佳方式.实际上我关心的是代码的性能.如果可能的话,请给我一个想法或一些代码下载.如何处理之间的错误和网络问题.
谢谢你,Srinivas
xil*_*il3 16
这是我最近为此写的一些代码:
try {
URL u = new URL("http://your.url/file.zip");
InputStream is = u.openStream();
DataInputStream dis = new DataInputStream(is);
byte[] buffer = new byte[1024];
int length;
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/" + "file.zip"));
while ((length = dis.read(buffer))>0) {
fos.write(buffer, 0, length);
}
} catch (MalformedURLException mue) {
Log.e("SYNC getUpdate", "malformed url error", mue);
} catch (IOException ioe) {
Log.e("SYNC getUpdate", "io error", ioe);
} catch (SecurityException se) {
Log.e("SYNC getUpdate", "security error", se);
}
Run Code Online (Sandbox Code Playgroud)
这会下载文件并将其放在您的SD卡上.
您可以修改它以满足您的需求.:)
| 归档时间: |
|
| 查看次数: |
10840 次 |
| 最近记录: |