Android:从URL下载返回的Zip文件
如果我有一个URL,当在web浏览器中提交时,会弹出一个对话框来保存zip文件…我如何在android中捕获和下载此zip文件?用于下载URL,虽然我不确定是否有重定向,但DownloadManager会处理它。用于下载URL,虽然我不确定是否有任何重定向,但DownloadManager会处理它。哇,我不明白您的连接和管理以及inputstreams的出色示例。。。我阅读文档。。。。但我不知道如何才能将公共静态最终字符串列\u原因mm。。。讽刺的是,我不理解你的优秀例子,你的关系和管理以及你的输入流。。。我阅读文档。。。。但我不知道如何才能将公共静态最终字符串列\u原因mm。。。Ironyth是一种魅力。。。因为有时它可能引发networkOnMainThreadException。在这种情况下,在AsnycTask中执行此过程就可以了。效果很好。非常感谢:这是一种魅力。。。因为有时它可能引发networkOnMainThreadException。在这种情况下,在AsnycTask中执行此过程就可以了。效果很好。非常感谢你:Android:从URL下载返回的Zip文件,android,url,zip,webserver,download,Android,Url,Zip,Webserver,Download,如果我有一个URL,当在web浏览器中提交时,会弹出一个对话框来保存zip文件…我如何在android中捕获和下载此zip文件?用于下载URL,虽然我不确定是否有重定向,但DownloadManager会处理它。用于下载URL,虽然我不确定是否有任何重定向,但DownloadManager会处理它。哇,我不明白您的连接和管理以及inputstreams的出色示例。。。我阅读文档。。。。但我不知道如何才能将公共静态最终字符串列\u原因mm。。。讽刺的是,我不理解你的优秀例子,你的关系和管理以及你的
public void downloadFromUrl(String url, String outputFileName) {
File dir = new File(Environment.getExternalStorageDirectory() + "/");
if (dir.exists() == false) {
dir.mkdirs();
}
try {
URL downloadUrl = new URL(url); // you can write any link here
File file = new File(dir, outputFileName);
/* Open a connection to that URL. */
URLConnection ucon = downloadUrl.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}