java 如何下载文件并在本地获取路径位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33231511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download a file and get the path location locally
提问by Little bird
I have a URL i.e http://downloadplugins.verify.com/Windows/SubAngle.exe. If i paste it on the tab and press enter then the file (SubAngle.exe) is getting downloaded and saved in the download folder.This is manual process.But it can be done with java code. I wrote the code for getting the absolute path with the help of file name i.e SubAngle.exe.
我有一个 URL,即http://downloadplugins.verify.com/Windows/SubAngle.exe。如果我将其粘贴到选项卡上并按 Enter,则文件 (SubAngle.exe) 正在下载并保存在下载文件夹中。这是手动过程。但可以使用 java 代码完成。我编写了借助文件名(即 SubAngle.exe)获取绝对路径的代码。
Requirement:- With the help of the URL file gets downloaded,Verify the file has been downloaded and returns the absolute path of the file.
要求:- 借助下载的 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;
}
回答by Ravindra babu
Code to DownloadFile from URL
从 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();
}
}
}
}
Configure the value of fileName properly to know where the file is getting stored.Source: http://www.devmanuals.com/tutorials/java/corejava/files/java-read-large-file-efficiently.html
正确配置 fileName 的值以了解文件的存储位置。来源:http: //www.devmanuals.com/tutorials/java/corejava/files/java-read-large-file-efficiently.html
The source was modified to replace local file with http URL
源码修改为用http URL替换本地文件
Output:
输出:
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
Starting download
开始下载
Time for download & save file in millis:100184
以毫秒为单位下载和保存文件的时间:100184
回答by Varshney P.
Instead of writing this huge code, go for Apache's commons.ioTry this:
与其写这么大的代码,不如去Apache 的 commons.io试试这个:
URL ipURL = new URL("inputURL");
File opFile = new File("outputFile");
FileUtils.copyURLToFile(ipURL, opFile);
回答by Gal Levy
easy and general function that im using:
我正在使用的简单和通用的功能:
import org.apache.commons.io.FileUtils;
public static void downLoadFile(String fromFile, String toFile) throws MalformedURLException, IOException {
try {
FileUtils.copyURLToFile(new URL(fromFile), new File(toFile), 60000, 60000);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("exception on: downLoadFile() function: " + e.getMessage());
}
}