简体   繁体   中英

HttpURLConnection and download large files

I use HttpURLConnection to download files from a url.

 URL obj = new URL(url);
 HttpURLConnection con = (HttpURLConnection) obj.openConnection();

 // optional default is GET
 con.setRequestMethod("GET");
 con.setRequestProperty("Cache-Control", "no-cache");

 int responseCode = con.getResponseCode();

 System.out.println("\nSending 'GET' request to URL : " + url);
 System.out.println("Response Code : " + responseCode);

 try {
     InputStream inputStream = con.getInputStream();
     FileOutputStream outputStream = new FileOutputStream("C:\\programs\\TRYFILE.csv");

     int bytesRead = -1;
     byte[] buffer = new byte[4096];

     while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
     }

 } catch(Exception e) {

 } finally {
       outputStream.close(); 
       inputStream.close();
 }

The code works to download small sized files (ie 25 KB). I didn't try to download large files (on the order of 100 MB), because the files from the particular URL are always small.

I want to know what happens if I try to download larger files with this code: will it continue to work or throw an exception? Do I need to implement code (utilizing, say, setConnectTimeout or setReadTimeout ) for bigger files?

Is there a url you can suggest where I can try to download large file using this code?

As suggested in the comments, create a large file yourself. To serve it over HTTP, the easiest way is probably to run Python 3 from the directory where you put the file -

python -m http.server

which will start a server on 8000. See this blog post for more details, or check out the python documentation.

Then you can test this yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM