Below is the code which demonstrates the way to download a csv file from an FTP using java.
Also this code shows the way to configure the FTP details in a properties file and use in the code.
File 1: Resource_Properties.java
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");
}
public ResourceBundle getBundle_Ftp()
{
Locale currentLocale = new Locale("EN","IN");
ResourceBundle myResources_collection = ResourceBundle.getBundle("FTP_PROPERTIES_FILE",currentLocale);
return myResources_collection;
}
}
File 2: FTP_PROPERTIES_FILE.properties
#FTP SERVER CONFIGURATION
ftp_host = 232.323.23.43
ftp_username = sdfsfsdfsdfsd
ftp_password = sdfsdfsdfsdfd
File3: DownloadCsvFromFtp.jsp
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.Properties"%>
<%@ page import="java.util.ResourceBundle"%>
<%
String inputLine=null;
BufferedReader in=null;
URL url=null;
PrintWriter Out=null;
String fileName=null;
String extension=null;
String filePath=null;
String FTPHOST=null;
String FTPUName=null;
String FTPPwd=null;
try
{
//FTP DETAILS
ResourceBundle myResources;
myResources = (new Resource_Properties()).getBundle_Ftp();
FTPHOST = myResources.getString("ftp_host");
FTPUName = myResources.getString("ftp_username");
FTPPwd = myResources.getString("ftp_password");
fileName = "Test.csv";
fileName=fileName.substring(fileName.lastIndexOf("\\")+1,fileName.length());
extension=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
filePath = "testacc/"+fileName;
if(extension.equalsIgnoreCase("csv")){
response.setContentType("application/csv");
}
url = new URL("ftp://"+FTPUName+":"+FTPPwd+"@"+FTPHOST+"/"+filePath);
in = new BufferedReader(new InputStreamReader(url.openStream()));
Out = response.getWriter();
response.setHeader("Content-Disposition","attachment; filename="+fileName);
response.setContentLength((int) fileName.length());
while ((inputLine = in.readLine()) != null)
{
Out.write(inputLine);
Out.write("\n");
}
in.close();
Out.flush();
Out.close();
}
catch(Exception e)
{
System.out.println("Error"+e);
}
%>
Also this code shows the way to configure the FTP details in a properties file and use in the code.
File 1: Resource_Properties.java
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");
}
public ResourceBundle getBundle_Ftp()
{
Locale currentLocale = new Locale("EN","IN");
ResourceBundle myResources_collection = ResourceBundle.getBundle("FTP_PROPERTIES_FILE",currentLocale);
return myResources_collection;
}
}
File 2: FTP_PROPERTIES_FILE.properties
#FTP SERVER CONFIGURATION
ftp_host = 232.323.23.43
ftp_username = sdfsfsdfsdfsd
ftp_password = sdfsdfsdfsdfd
File3: DownloadCsvFromFtp.jsp
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.Properties"%>
<%@ page import="java.util.ResourceBundle"%>
<%
String inputLine=null;
BufferedReader in=null;
URL url=null;
PrintWriter Out=null;
String fileName=null;
String extension=null;
String filePath=null;
String FTPHOST=null;
String FTPUName=null;
String FTPPwd=null;
try
{
//FTP DETAILS
ResourceBundle myResources;
myResources = (new Resource_Properties()).getBundle_Ftp();
FTPHOST = myResources.getString("ftp_host");
FTPUName = myResources.getString("ftp_username");
FTPPwd = myResources.getString("ftp_password");
fileName = "Test.csv";
fileName=fileName.substring(fileName.lastIndexOf("\\")+1,fileName.length());
extension=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
filePath = "testacc/"+fileName;
if(extension.equalsIgnoreCase("csv")){
response.setContentType("application/csv");
}
url = new URL("ftp://"+FTPUName+":"+FTPPwd+"@"+FTPHOST+"/"+filePath);
in = new BufferedReader(new InputStreamReader(url.openStream()));
Out = response.getWriter();
response.setHeader("Content-Disposition","attachment; filename="+fileName);
response.setContentLength((int) fileName.length());
while ((inputLine = in.readLine()) != null)
{
Out.write(inputLine);
Out.write("\n");
}
in.close();
Out.flush();
Out.close();
}
catch(Exception e)
{
System.out.println("Error"+e);
}
%>
No comments:
Post a Comment