2015-10-20 91 views
0

我有一個URL,即http://downloadplugins.verify.com/Windows/SubAngle.exe。 如果我把它粘貼在標籤上並按回車鍵,那麼文件(SubAngle.exe)就會被下載並保存在下載文件夾中。這是手動過程。但可以用java代碼完成。 我編寫了用於獲得絕對路徑的代碼,即藉助文件名即SubAngle.exe。如何下載文件並在本地獲取路徑位置

要求: - 在URL文件下載幫助下,驗證文件已下載並返回文件的絕對路徑。

where locfile is "http://downloadplugins.verify.com/Windows/SubAngle.exe" 

    public String downloadAndVerifyFile(String locfile) { 
    File fileLocation = new File(locfile); 
    File fileLocation1 = new File(fileLocation.getName()); 
    String fileLocationPath = null; 
    if(fileLocation.exists()){ 
     fileLocationPath = fileLocation1.getAbsolutePath(); 

    } 
    else{ 
     throw new FileNotFoundException("File with name "+locFile+" may not exits at the location"); 
    } 
    return fileLocationPath; 
} 
+0

不,你應該有一個本地路徑,它反映了磁盤上的位置,類似於C:\ Users \ {您的用戶名} \ Downloads – MadProgrammer

+0

相關:http://stackoverflow.com/questions/43157/easy-way-to-write-contents- java-inputstream-to-an-outputstream/51753#51753 –

+0

可能du plicate [如何使用Java下載和保存文件從互聯網?](http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java ) –

回答

0

守則DownloadFile從URL

import java.net.*; 
import java.io.*; 

public class DownloadFile { 
    public static void main(String[] args) throws IOException { 
     InputStream in = null; 
     FileOutputStream out = null; 
     try { 
      // URL("http://downloadplugins.verify.com/Windows/SubAngle.exe"); 
      System.out.println("Starting download"); 
      long t1 = System.currentTimeMillis(); 
      URL url = new URL(args[0]); 
      // Open the input and out files for the streams 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      in = conn.getInputStream(); 
      out = new FileOutputStream("YourFile.exe"); 
      // Read data into buffer and then write to the output file 
      byte[] buffer = new byte[8192]; 
      int bytesRead; 
      while ((bytesRead = in.read(buffer)) != -1) { 
       out.write(buffer, 0, bytesRead); 
      } 
      long t2 = System.currentTimeMillis(); 
      System.out.println("Time for download & save file in millis:"+(t2-t1)); 
     } catch (Exception e) { 
      // Display or throw the error 
      System.out.println("Erorr while execting the program: " 
        + e.getMessage()); 
     } finally { 
      // Close the resources correctly 
      if (in != null) { 
       in.close(); 
      } 
      if (out != null) { 
       out.close(); 
      } 
     } 

    } 

} 

配置文件名的值正確地知道該文件得到存儲在哪裏。 來源:http://www.devmanuals.com/tutorials/java/corejava/files/java-read-large-file-efficiently.html

源進行了修改,以取代HTTP URL

輸出本地文件:

的Java DownloadFile http://download.springsource.com/release/TOOLS/update/3.7.1.RELEASE/e4.5/springsource-tool-suite-3.7.1.RELEASE-e4.5.1-updatesite.zip

開始下載

以毫秒時間下載&保存文件:100184

+1

如果你複製粘貼,至少使用OP提供的url和文件,還要注意你的鏈接是壞的 –

+0

這可能是錯字錯誤。我在「下載」中提供了工作鏈接 –

+0

如果文件大小大於1 GB會怎麼樣? –

0

而不是寫這個龐大的代碼,去Apache的commons.io 試試這個:

URL ipURL = new URL("inputURL"); 
File opFile = new File("outputFile"); 
FileUtils.copyURLToFile(ipURL, opFile); 
相關問題