【问题标题】:How to download a file and get the path location locally如何下载文件并在本地获取路径位置
【发布时间】:2015-10-20 08:20:13
【问题描述】:

我有一个网址,即 http://downloadplugins.verify.com/Windows/SubAngle.exe 。 如果我将其粘贴到选项卡上并按 Enter,则文件(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;
}

【问题讨论】:

标签: java


【解决方案1】:

从 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://www.devmanuals.com/tutorials/java/corejava/files/java-read-large-file-efficiently.html

源被修改为用http URL替换本地文件

输出:

java下载文件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

【讨论】:

  • 如果你复制粘贴,至少使用OP提供的url和文件,还要注意你的链接坏了
  • 这可能是拼写错误。我在“下载”中提供了工作链接
  • 如果文件大小大于 1 GB 怎么办?
  • 它将处理,因为我们正在编写大小为 1024 字节的字节数组。它不需要 1 GB RAM,但您只需要 1 GB 磁盘空间。如果您遇到 1 GB 文件大小的问题,请告诉我。在 out.write() 之后添加 out.flush()
  • 说真的,为什么要将文件读入 ByteArrayOutputStream?如果文件太大,显然这会失败。更不用说它没有意义和效率低下。
【解决方案2】:

不要编写如此庞大的代码,而是使用 Apache 的 commons.io 试试这个:

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

【讨论】:

    【解决方案3】:

    我正在使用的简单通用功能:

    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());
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多