我有一個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;
}
不,你應該有一個本地路徑,它反映了磁盤上的位置,類似於C:\ Users \ {您的用戶名} \ Downloads – MadProgrammer
相關:http://stackoverflow.com/questions/43157/easy-way-to-write-contents- java-inputstream-to-an-outputstream/51753#51753 –
可能du plicate [如何使用Java下載和保存文件從互聯網?](http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java ) –