1

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 a manual process. But it can be done with java code. I wrote the code for getting the absolute path with the help of the 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;
}
Borislav Stoilov
  • 3,247
  • 2
  • 21
  • 46
Little bird
  • 1,106
  • 7
  • 28
  • 58
  • 1
    No, you should have a local path, which reflects a location on your disk, something like C:\Users\{your user name}\Downloads – MadProgrammer Oct 20 '15 at 08:32
  • related: http://stackoverflow.com/questions/43157/easy-way-to-write-contents-of-a-java-inputstream-to-an-outputstream/51753#51753 – Thomas Weller Oct 20 '15 at 13:29
  • 2
    Possible duplicate of [How to download and save a file from Internet using Java?](http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java) – Thomas Weller Oct 20 '15 at 13:35

3 Answers3

2

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());
        }
    }
cantSleepNow
  • 9,691
  • 5
  • 31
  • 42
Gal Levy
  • 39
  • 3
1

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);
Varshney P.
  • 208
  • 1
  • 12
0

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

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • 1
    if you copy paste, at least use url and file provided by OP, also note your link is broken – Jordi Castilla Oct 20 '15 at 08:33
  • That may be typo error. I have provided working link in "download' – Ravindra babu Oct 20 '15 at 08:35
  • What if the size of file is greater than 1 GB ? – Little bird Oct 20 '15 at 12:56
  • It will handle since we are writing byte array of size 1024 bytes. It don't need 1 GB RAM but you just need 1 GB disk space. Let me know if you face issues with 1 GB file size. Add out.flush () after out.write() – Ravindra babu Oct 20 '15 at 13:13
  • On a different note of reading large file, have a look at this post: http://stackoverflow.com/questions/18508836/java-outofmemoryerror-in-reading-a-large-text-file/31724120#31724120 – Ravindra babu Oct 20 '15 at 13:20
  • @ravindra: really? `byte[] response` sounds much like everything is in that byte[] before it gets written to disk – Thomas Weller Oct 20 '15 at 13:21
  • Small change is required in my code. After out.write (),write to fos immediately. Have a look at : http://www.devmanuals.com/tutorials/java/corejava/files/java-read-large-file-efficiently.html – Ravindra babu Oct 20 '15 at 13:23
  • 2
    Seriously, why would you read the file into a ByteArrayOutputStream? Obviously this fails if the file is too big. Not to mention that it is pointless and inefficient. – Stephen C Oct 20 '15 at 13:25
  • Have you checked the license of the code that you are copying? – Thomas Weller Oct 20 '15 at 13:26
  • @ravindra : Are you sure that this will work for >1GB file size?Because stiil byte array [1024] is being defined in the code. Thanks – Little bird Oct 20 '15 at 13:52
  • I have changed the code to take filename as input from args[0]. 117 MB file is downloaded in 2 mins. e.g. 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 – Ravindra babu Oct 20 '15 at 13:58