download

by eit on December 27th, 2010
No notes
Syntax: Java
Show lines - Hide lines - Show in textbox - Download
  1. public static void fliedownload(String filelocation) throws IOException {
  2. String urlstring = filelocation; // 欲下載的網址
  3. String outputFile = "./temp.zip"; // 下載儲存的檔案
  4. File desFile = new File(outputFile);
  5. if (desFile.exists())
  6. desFile.delete();
  7. URL url = new URL(urlstring);
  8. URLConnection connection = url.openConnection();
  9.  
  10. byte[] data = new byte[1]; // InputStream
  11.  
  12. // 設定接收資料流來源 ,就是要下載的網址
  13. BufferedInputStream bufferedInputStream = new BufferedInputStream(
  14. connection.getInputStream());
  15. // 設定 儲存 要下載檔案的位置.
  16. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
  17. new FileOutputStream(desFile));
  18. while (bufferedInputStream.read(data) != -1) {
  19. bufferedOutputStream.write(data);
  20. }
  21. bufferedOutputStream.flush();// 將緩衝區中的資料全部寫出
  22. bufferedInputStream.close(); // 關閉資料流
  23. bufferedOutputStream.close();
  24. System.out.println("下載完成");
  25. }

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS