Download file from FTP using java


Below is the code which is a complete implementation to download any file \(xls,doc,pdf,..) from FTP using java.

It also shows the way to keep the FTP credentials in a properties file and access it using java.




File1: downloadFileFromFTP.jsp

<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.Properties"%>
<%@ page import="java.util.ResourceBundle"%>
<%@ page import="PROPERTIES.*"%>


<%
URL url=null;

String fileName=null;
String extension=null;
String filePath=null;
String FTPHOST=null;
String FTPUName=null;
String FTPPwd=null;
String id="0";

try
{
   

    fileName = request.getParameter("Docname");
    id = request.getParameter("id");
    fileName=fileName.substring(fileName.lastIndexOf("\\")+1,fileName.length());
    extension=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
    filePath = "TARGETFOLDER/"+fileName;
    id=id+"."+extension;
   
   
    //GET FTP DETAILS
   
    ResourceBundle rb;
    rb = (new rbprop()).getFTPDetails();
   
   
    FTPHOST = rb.getString("ftp_host");
    FTPUName = rb.getString("ftp_username");
    FTPPwd = rb.getString("ftp_password");

   
   
    if((extension.equalsIgnoreCase("pdf")) || (extension.equalsIgnoreCase("PDF"))){
       
          response.setContentType("application/pdf");
          response.setHeader("Content-Disposition","attachment; filename="+claimid);
    }
    else if((extension.equalsIgnoreCase("csv")) || (extension.equalsIgnoreCase("CSV"))){
            response.setContentType("application/csv");
          response.setHeader("Content-Disposition","attachment; filename="+claimid);
    }
    else if((extension.equalsIgnoreCase("doc")) || (extension.equalsIgnoreCase("docx"))){
 
         response.setContentType("Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
         response.setHeader("Content-Disposition","attachment; filename="+claimid);
    }
    else if((extension.equalsIgnoreCase("xls")) || (extension.equalsIgnoreCase("xlsx"))){
         response.setContentType("application/vnd.ms-excel");
         response.setHeader("Content-Disposition","attachment; filename="+claimid);
    }
   
 
url = new URL("ftp://"+FTPUName+":"+FTPPwd+"@"+FTPHOST+"/"+filePath);

BufferedInputStream buf = new BufferedInputStream(url.openStream());

ServletOutputStream sos = response.getOutputStream();

//Starting to write bytes to output stream
int readBytes = 0;

while ((readBytes = buf.read()) != -1) {
sos.write(readBytes);
}
//Finished writing bytes to output stream

sos.flush();
sos.close();
}
catch(Exception e)
{
    System.out.println("Error DISPLAY"+e);
}

%>



File 2: ftpproperties.properties

ftp_host= HOST NAME
ftp_username=USERNAME
ftp_password=PASSWORD





File 3: properties.java



package PROPERTIES;


import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;


public class Resource_Properties
{
   
 
    public static void main(String ar[])
    {      
   
      Locale currentLocale = new Locale("EN","IN");
         
     
      try
      {
      ResourceBundle mr= ResourceBundle.getBundle("ftpproperties",currentLocale);
   

      }
      catch(MissingResourceException e)
      {
     //
      }
           
    }
   
 
    public ResourceBundle getFTPDetails()
    {
      Locale currentLocale = new Locale("EN","IN");    
      ResourceBundle mr = ResourceBundle.getBundle("ftpproperties",currentLocale);    
      return mr;
    }
   
   
   
}

No comments:

Post a Comment