How to download a file and get the path location locally

前端 未结 3 599
不思量自难忘°
不思量自难忘° 2020-12-07 05:20

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 sav

相关标签:
3条回答
  • 2020-12-07 05:49

    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());
            }
        }
    
    0 讨论(0)
  • Instead of writing this huge code, go for Apache's commons.io Try this:

    URL ipURL = new URL("inputURL");
    File opFile = new File("outputFile");
    FileUtils.copyURLToFile(ipURL, opFile);
    
    0 讨论(0)
  • 2020-12-07 06:02

    Code to DownloadFile from 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

    The source was modified to replace local file with 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

    0 讨论(0)
提交回复
热议问题