Java 如何下载文件并在本地获取路径位置
我有一个网址,即。 如果我将其粘贴到选项卡上并按enter键,则文件(SubAngle.exe)将被下载并保存在下载文件夹中。这是手动过程。但可以使用java代码完成。 我使用文件名SubAngle.exe编写了获取绝对路径的代码 要求:-在下载URL文件的帮助下,验证文件是否已下载并返回文件的绝对路径Java 如何下载文件并在本地获取路径位置,java,Java,我有一个网址,即。 如果我将其粘贴到选项卡上并按enter键,则文件(SubAngle.exe)将被下载并保存在下载文件夹中。这是手动过程。但可以使用java代码完成。 我使用文件名SubAngle.exe编写了获取绝对路径的代码 要求:-在下载URL文件的帮助下,验证文件是否已下载并返回文件的绝对路径 where locfile is "http://downloadplugins.verify.com/Windows/SubAngle.exe" public String dow
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;
}
从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();
}
}
}
}
正确配置fileName的值,以了解文件的存储位置。
资料来源:
源已被修改为用http 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();
}
}
}
}
输出:
java下载文件
开始下载
下载和保存文件的时间(毫秒):100184从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();
}
}
}
}
正确配置fileName的值,以了解文件的存储位置。
资料来源:
源已被修改为用http 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();
}
}
}
}
输出:
java下载文件
开始下载
下载并以毫秒为单位保存文件的时间:100184不要编写这么庞大的代码,而是使用Apache的commons.io 试试这个:
URL ipURL = new URL("inputURL");
File opFile = new File("outputFile");
FileUtils.copyURLToFile(ipURL, opFile);
不要编写这么庞大的代码,而是使用Apache的commons.io 试试这个:
URL ipURL = new URL("inputURL");
File opFile = new File("outputFile");
FileUtils.copyURLToFile(ipURL, opFile);
我使用的简单而通用的功能:
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());
}
}
我使用的简单而通用的功能:
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());
}
}
不,你应该有一个本地路径,它反映了你磁盘上的一个位置,比如C:\Users\{your user name}\Downloadsrelated:可能重复的否,你应该有一个本地路径,它反映了你磁盘上的一个位置,比如C:\Users\{your user name}\Downloadsrelated:如果你复制粘贴,可能重复的,至少要使用OP提供的url和文件,还要注意你的链接断开了,这可能是打字错误。我在“下载”中提供了工作链接,如果文件大小大于1 GB怎么办?它可以处理,因为我们正在写入大小为1024字节的字节数组。它不需要1 GB RAM,但您只需要1 GB磁盘空间。如果您遇到1 GB文件大小的问题,请告诉我。out.write()之后是Add out.flush()说真的,你为什么要将文件读入ByteArrayOutputStream?显然,如果文件太大,这会失败。更不用说它毫无意义且效率低下。如果你复制粘贴,至少要使用OP提供的url和文件,还要注意你的链接断开了,这可能是打字错误。我在“中提供了工作链接”下载“如果文件大小大于1GB怎么办?它可以处理,因为我们正在编写大小为1024字节的字节数组。它不需要1GB的RAM,但您只需要1GB的磁盘空间。如果您遇到1 GB文件大小的问题,请告诉我。在out.write()之后添加out.flush()。认真地说,为什么要将文件读入ByteArrayOutputStream?显然,如果文件太大,这将失败。更不用说它毫无意义,效率低下。