File download using HTTP Socket

// Socket Programming
// File download using Http Socket

import java.io.*;
import java.net.*;

public class Filedownload
{
     private final static int BUF_SIZE = 4096;
    
     public static void downloadFile(String fileURL, String targetDir) throws IOException
     {
          URL url = new URL(fileURL);
    
          // Establish connection to remote URL
          HttpURLConnection conc = (HttpURLConnection) url.openConnection();
         
          // Check response code for connection status.
          // Use static variable (HttpURLConnection.HTTP_OK) or its code (200).
          int responseCode = conc.getResponseCode();
          // if (responseCode == HttpURLConnection.HTTP_OK)
          if (responseCode == 200)         
          {
              // Get information about the file and its content
              String fileName = "";
              String content = conc.getHeaderField("Content-Disposition");
              String contentType = conc.getContentType();
              int contentLength = conc.getContentLength();





              if (content != null)
              {
                   // Extract file name from header field
                   int index = content.indexOf("filename=");
                   if (index > 0)
                   {
                        fileName = content.substring(index + 10,content.length() - 1);
                   }
              }
              else
              {
                   // Extract file name from URL
                   fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
              }

System.out.println("Content-Type = " + contentType);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);

// Open input stream from the HTTP connection
InputStream in = conc.getInputStream();
String filepath = targetDir + File.separator + fileName;

// Open output stream to save content to file
FileOutputStream out = new FileOutputStream(filepath);
                            
int data = -1;
byte[] buf = new byte[BUF_SIZE];
              while ((data = in.read(buf)) != -1)
              {
                   out.write(buf, 0, data);
              }

out.close();
in.close();

System.out.println("File downloaded");
}



else
{
System.out.println("No file to download. ");
System.out.println("Server replied HTTP code: " + responseCode);
}
conc.disconnect();
     }
         
public static void main(String[] args)
{
String fileURL = "http://drranurekha.com/wp-content/uploads/2018/02/3-Segmentation.pdf";
         
String targetDir = "D:/cnlab/Http_socket";
       
try
{
     downloadFile(fileURL, targetDir);
}
catch (IOException E)
{
     E.printStackTrace();
}
}
}




/*
Note 1: Specify the URL for file to download clearly
Example:
     Open a pdf file in BROWSER.
     Check the address bar and copy the link in url=""
     Paste it in string fileURL
*/

/*
Note 2:
response code retrieves the status of the http connection.
Example:
     HTTP_OK - HTTP Status-Code 200: OK.
     HTTP_ACCEPTED - HTTP Status-Code 202: Accepted.
     HTTP_NO_CONTENT - HTTP Status-Code 204: No Content.
     HTTP_FORBIDDEN - HTTP Status-Code 403: Forbidden.
     HTTP_CLIENT_TIMEOUT - HTTP Status-Code 408: Request Time-Out.
     HTTP_BAD_GATEWAY - HTTP Status-Code 502: Bad Gateway.
*/

/*
Note 3:
The MIME Content-disposition header provides presentation information for the body-part. It is often added to attachments specifying whether the attachment body part should be displayed (inline) or presented as a file name to be copied (attachment)
*/

Sample Output:

> javac Filedownload.java
> java Filedownload
Content-Type = application/pdf
Content-Length = 211589
fileName = 3-Segmentation.pdf
File downloaded
> 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu