×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: userc865b
Added: Aug 30, 2017 3:26 PM
Views: 5
Tags: java
downloads a file from server into specific directory via https
  1. downloadFileHttpsConnection("https://www.test.de/_test_/ceck.apk", "C:/Users/Test/Desktop/Mull");  
  2.    
  3.    /**
  4.      * Downloads a file from a URL
  5.      * @param fileURL HTTP URL of the file to be downloaded
  6.      * @param saveDir path of the directory to save the file
  7.      * @throws IOException
  8.      */
  9.     public static void downloadFileHttpsConnection(String fileURL, String saveDir)
  10.             throws IOException {
  11.         final int BUFFER_SIZE = 4096;
  12.  
  13.  
  14.         URL url = new URL(fileURL);
  15.         HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
  16.         int responseCode = httpsConn.getResponseCode();
  17.  
  18.         // always check HTTP response code first
  19.         if (responseCode == HttpsURLConnection.HTTP_OK) {
  20.             String fileName = "";
  21.             String disposition = httpsConn.getHeaderField("Content-Disposition");
  22.             String contentType = httpsConn.getContentType();
  23.             int contentLength = httpsConn.getContentLength();
  24.  
  25.             if (disposition != null) {
  26.                 // extracts file name from header field
  27.                 int index = disposition.indexOf("filename=");
  28.                 if (index > 0) {
  29.                     fileName = disposition.substring(index + 10,
  30.                             disposition.length() - 1);
  31.                 }
  32.             } else {
  33.                 // extracts file name from URL
  34.                 fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
  35.                         fileURL.length());
  36.             }
  37.  
  38.             System.out.println("Content-Type = " + contentType);
  39.             System.out.println("Content-Disposition = " + disposition);
  40.             System.out.println("Content-Length = " + contentLength);
  41.             System.out.println("fileName = " + fileName);
  42.  
  43.             // opens input stream from the HTTP connection
  44.             InputStream inputStream = httpsConn.getInputStream();
  45.             String saveFilePath = saveDir + File.separator + fileName;
  46.  
  47.             // opens an output stream to save into file
  48.             FileOutputStream outputStream = new FileOutputStream(saveFilePath);
  49.  
  50.             int bytesRead = -1;
  51.             byte[] buffer = new byte[BUFFER_SIZE];
  52.             while ((bytesRead = inputStream.read(buffer)) != -1) {
  53.                 outputStream.write(buffer, 0, bytesRead);
  54.             }
  55.  
  56.             outputStream.close();
  57.             inputStream.close();
  58.  
  59.             System.out.println("File downloaded");
  60.         } else {
  61.             System.out.println("No file to download. Server replied HTTP code: " + responseCode);
  62.         }
  63.         httpsConn.disconnect();
  64.     }