2012-11-23 148 views
-4

假設我有一個URL,比如something.domain/myfile.txt,那麼我想用這個「保存文件」對話框保存這個文件。使用Java下載並保存文件

我盡我所能去做,但每次使用對話框保存文件都不在那裏。

一個例子或某處我可以找到有關這方面的信息會有很大的幫助!

 URL website = null; 
       try { 
        website = new URL(<insert url here>); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } 
       ReadableByteChannel rbc = null; 
       try { 
        rbc = Channels.newChannel(website.openStream()); 
       } catch (IOException e2) { 
        e2.printStackTrace(); 
       } 
       FileOutputStream fos = null; 
       try { 
        fos = new FileOutputStream(new File("minecraft.jar")); 
       } catch (FileNotFoundException e1) { 
        e1.printStackTrace(); 
       } 
       try { 
        fos.getChannel().transferFrom(rbc, 0, 1 << 24); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

        JFileChooser fileChooser = new JFileChooser(); 
        if (fileChooser.showSaveDialog(fileChooser) == JFileChooser.APPROVE_OPTION) { 
         File dir = fileChooser.getCurrentDirectory(); 
         dir.mkdir(); 
         //After this point is where I need help. 
+2

你能告訴我們一些代碼? – jlordo

+0

你在說什麼「保存文件對話框」? API中沒有內置此功能;你需要設計你自己的保存對話框。 – Vulcan

+0

如果您正在討論標準的Swing JFileChoose或AWT FileDialog,那麼它們只能瀏覽文件系統。他們不瀏覽網頁。 –

回答

0

我相信,這是你在找什麼:

if (fileChooser.showSaveDialog(fileChooser) == JFileChooser.APPROVE_OPTION) 
{ 
    File file = fileChooser.getSelectedFile(); 
    // whatever you want to do with the file 
    System.out.println("The file is "+file.getAbsolutePath()); 
    // fos = new FileOutputStream(file) ... 
} 
0

你有沒有注意到,在你的代碼試圖保存/給予用戶選項來選擇之前下載的文件目的地?

我將在代碼分成三個不同的操作:

  1. 在電荷轉移從InputStream(網絡)中的字節的方法,向OutputStream(文件)。
  2. 一種向用戶顯示對話框的方法,以便他可以選擇將文件存儲在何處。
  3. 完成整個任務的方法:選擇一個文件並將字節從網絡傳輸到它。

1)請問是這樣的(你不需要使用NIO API來實現它):

public void transfer(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[2048]; 
    int bytesRead; 
    while ((bytesRead = in.read(buffer)) > 0) { 
     out.write(buffer, 0, bytesRead); 
    } 
} 

2)將是非常類似的東西到什麼Dukeling已經說明:

public File chooseFile() { 
    File result = null; 
    JFileChooser fileChooser = new JFileChooser(); 
    if (fileChooser.showSaveDialog(fileChooser) == JFileChooser.APPROVE_OPTION) { 
    result = fileChooser.getSelectedFile(); 
    } 
    return result; 
} 

3)然後,結合這兩個操作起來非常簡單:

public void saveFileFromWeb(URL url) { 
    File file = chooseFile(); // 1. Choose the destination file 
    if (file != null) { 
    // 2. Create parent folder structure 
    File folder = file.getParentFile(); 
    if (!folder.exist()) { 
     folder.mkdirs(); 
    } 

    InputStream in = null; 
    OutputStream out = null; 
    try { 
     // 3. Initialise streams 
     in = url.openStream(); 
     out = new FileOuputStream(file); 
     // 4. Transfer data 
     transfer(in, out); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     // 5. close streams 
     if (in != null) { 
     try { 
      in.close(); 
     } catch (IOException e) { /* ignore */ } 
     } 
     if (out != null) { 
     try { 
      out.close(); 
     } catch (IOException e) { /* ignore */ } 
    } 
    } 
} 

注意:1)和2)可能是私人方法。當然,你可以這樣做只是一個單一的操作,但拆分它會給你一個執行不同步驟的概述。

注2::我簡化了例外處理部分