如果我有一個網址,當它在網絡瀏覽器中提交時,會彈出一個對話框來保存一個zip文件......我如何去捕獲和下載這個zip文件的android?Android:下載從URL返回的Zip文件
1
A
回答
4
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();
}
}
0
使用DownloadManager下載Url,雖然我不確定是否有任何重定向,但DownloadManager會照顧它。
+0
哇,我不明白你與你的連接和管理人員真棒例子,你inputstreams ...我閱讀文檔....但我不知道我怎樣才能把「公共靜態最後絃樂COLUMN_REASON」嗯...(諷刺) – delive 2015-11-20 16:48:10
相關問題
- 1. 返回一個zip文件angularjs下載
- 2. 使用perl從url下載zip文件
- 3. 使用VBA從url下載zip文件
- 4. 返回zip從django的視圖下載
- 5. AsyncTask下載zip文件android
- 6. Android Java - 如何從URL下載zip文件?
- 7. 從Zip文件中下載PDF文件
- 8. 在android中的zip文件下載
- 9. 下載ZIP文件
- 10. 下載.zip文件
- 11. Zip文件下載
- 12. 下載壓縮文件返回使用servlets損壞的zip
- 13. 使用$ .post或$ .get返回一個可下載的zip文件
- 14. 文件下載返回IOException與Android 2.3
- 15. 下載回形針文件,ZIP
- 16. 從wcf c中的URL下載zip文件#
- 17. 從servlet下載生成的zip文件
- 18. Android - 從URL下載HTML文件
- 19. 使用Android下載ZIP文件
- 20. 使用凌空下載Zip文件android
- 21. Android下載一個Zip文件
- 22. 下載的zip文件
- 23. 從URL下載XML文件返回NIL在NSData
- 24. 安卓:從URL下載內容返回MP3文件
- 25. 腳本下載和解壓縮zip文件返回錯誤
- 26. 從字節返回文件下載[]
- 27. 從url數組下載mp3到zip文件
- 28. 使用download.file從URL下載一個zip文件R
- 29. C#根據輸入參數從url下載zip文件
- 30. 嘗試從url下載zip文件時停止活動
謝謝.. 它的工作原理 – TheReaper 2012-08-01 11:35:00
作爲一個魅力... 有時它可能會拋出「networkOnMainThreadException」。在這種情況下,在AsnycTask中執行此過程並且沒問題。 – 2016-03-31 12:45:34
工程就像一個魅力。非常感謝你 :) – 2017-08-03 07:40:34