Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

java - How to download a file and get the path location locally

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.

Requirement:- With the help of the URL file gets downloaded,Verify the file has been downloaded and returns the absolute path of the file.

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...