upload and download files from ftp server using servlet

Ads
 

upload and download files from ftp server using servlet

View Answers

December 29, 2008 at 3:59 AM

Mr............

i don't know where r u fencing problem but i converted your source code in servlet. It may help to solve your problem.......


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.net.ftp.*;

public class FTPServerConnection extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello FTP Server</title></title>");
pw.println("<body>");
pw.println("<h1>Hello FTP Server</h1>");
pw.println("</body></html>");
try{
String ftpHost = "localhost";
String ftpUserName = "admin";
String ftpPassword = "admin";
//String ftpHost = "192.168.0.144";
//String ftpUserName = "rootadmin";
//String ftpPassword = "rootadmin";
String ftpRemoteDirectory = "/Ah Kyi Ta Chat";
String fileToTransmit = "C:\\my.ini";

FTPClient ftp = new FTPClient();
int reply;

ftp.connect(ftpHost);
reply = ftp.getReplyCode();
System.out.println("Reply:"+reply);
System.out.println("FTP Reply:"+FTPReply.isPositiveCompletion(reply));

if(!FTPReply.isPositiveCompletion(reply)) {
try {
ftp.disconnect();
} catch (Exception e) {
System.err.println("Unable to disconnect from FTP server " + "after server refused connection. "+e.toString());
} throw new Exception ("FTP server refused connection.");
}

-----------------------------------------------------------

December 29, 2008 at 4:00 AM

System.out.println("Connected to " + ftpHost + ". "+ftp.getReplyString());

System.out.println("User Name:"+ftpUserName+",Password:"+ftpPassword);
if (!ftp.login(ftpUserName, ftpPassword)) {
throw new Exception ("Unable to login to FTP server " + "using username "+ftpUserName+" " + "and password "+ftpPassword);
}
System.out.println(ftp.getReplyString());
System.out.println("Login Successful.");
System.out.println("Remote system is " + ftp.getSystemName());

ftp.setFileType(FTP.BINARY_FILE_TYPE);

if (ftpRemoteDirectory != null && ftpRemoteDirectory.trim().length() > 0) {
System.out.println("Changing to FTP remote dir: " + ftpRemoteDirectory);
ftp.changeWorkingDirectory(ftpRemoteDirectory);
reply = ftp.getReplyCode();
System.out.println("Change dir Okay:");
System.out.println("Reply Code:"+reply);
if(!FTPReply.isPositiveCompletion(reply)) {
throw new Exception ("Unable to change working 5 " + "to:"+ftpRemoteDirectory);
}
}

System.out.println("File Directory:"+fileToTransmit);
File f = new File(fileToTransmit);
System.out.println("Storing file as remote filename: " + f.getName());
boolean retValue = ftp.storeFile(f.getName(), new FileInputStream(f));
if (!retValue) {
throw new Exception ("Storing of remote file failed. ftp.storeFile()" + " returned false.");
}
System.out.println("File uploaded successful.");

/* String fileNames[]=ftp.listNames();
for(int i=0;i<fileNames.length;i++){
System.out.println("Files names:"+fileNames[i]);
}*/
FTPFile files[] = ftp.listFiles();
reply = ftp.getReplyCode();
System.out.println("File length:"+files.length);
if(!FTPReply.isPositiveCompletion(reply)) {
throw new Exception ("Unable to get list of files to dowload.");
}

if (files.length == 0) {
System.out.println("No files are available for download.");
} else {
for (int i=0; i<files.length; i++) {
System.out.println("Downloading file "+files[i].getName()+" Size:"+files[i].getSize());
String outputFileName = "c:\\temp\\"+files[i].getName();
f = new File(outputFileName);
retValue = ftp.retrieveFile(files[i].getName(), new FileOutputStream(f));
if (!retValue) {
throw new Exception ("Downloading of remote file "+files[i].getName()+" failed. ftp.retrieveFile() returned false.");
}

/*
retValue = ftp.deleteFile(files[i].getName());
if (!retValue) {
log.error ("Unable to delete remote file "+files[i].getName()+". ftp.deleteFile() returned false.");
}
*/
}
}

try {
//ftp.logout();
ftp.disconnect();
} catch (Exception exc) {
System.err.println("Unable to disconnect from FTP server. " + exc.toString());
}
} catch (Exception e) {
System.err.println("Error: "+e.toString());
}
System.out.println("Process Complete.");
System.exit(0);
}
}

Thanks.........

Ads









Related Tutorials/Questions & Answers:
How to upload files to server using JSP/Servlet?
How to upload files to server using JSP/Servlet?  How to upload files to server using JSP/Servlet
files upload to apache ftp server - Ajax
files upload to apache ftp server  Please, how to upload multiple files to apache ftp server using ajax . I want to upload files using drag drop... line. but I am trying to upload files using ajax. I want you to answer to me
Advertisements
files upload to apache ftp server - Ajax
files upload to apache ftp server  Please, how to upload multiple files to apache ftp server using ajax . I want to upload files using drag drop... line. but I am trying to upload files using ajax. I want you to answer to me
Upload and Download Large files in jsp
Upload and Download Large files in jsp  I am not able to download large files (>200mb) from any server. I need a code to download and upload large files (atleast 4 gb) to a server using jsp page
Upload and Download multiple files
Upload and Download multiple files  Hello Sir/Madam, I need a simple code for upload and download multiple files(it may be image,doc and txt)which has to be store in database and also retrieve the file from database
FTP Server : Download file
This tutorial contains description of file downloading from the FTP server using java
FTP Hosting
or information on web from any place using the hosted FTP services. FTP clients... and the hosting server where the uploaded files are to be stored or downloaded from.... Using FTP hosting service you can share as number of files as you want without
FTP Server : List Files and Directories
In this tutorial we will discuss how to list files and directories on FTP server using java
FTP Server: List all files name
This tutorial represents how to list the entire files name on FTP server using java
How to upload file on FTP Server
How to upload file on FTP Server  In my application there is a requirement of uploading the file on FTP server. How to upload file on FTP Server... is good and can be used in Java applications. Read FTP File Upload in Java for more
Delete a file from FTP Server
In this section you will learn how to delete file from FTP server using java
download code using servlet - Servlet Interview Questions
download code using servlet  How to download a file from web to our system using Servlet
FTP Server : Upload file
This tutorial contains description of file uploading to the FTP server using java
how to upload file using FTP in java
how to upload file using FTP in java  hai, i want to upload file using FTP in java Webservices.. Tel me some Articles Regards Asha
Web Based FTP
, it is not necessary now to install FTP client software in order to download files from a website. Web based FTP allows you to download files to your computer directly using the web browser. You can easily connect to the FTP addresses using your web
Java upload file to ftp
Java upload file to ftp  Hi, How to uploaded file to a ftp server... that uploads files on FTP server. You have to use the Apache FTP client library in your program. Apache FTP Client library can be downloaded from apache website
FTP server
FTP server  How to store a series of files in a ftp server using java
how to upload and download images using buttons in jsp?
how to upload and download images using buttons in jsp?  how to upload and download images using buttons in jsp
how to download the uploaded folder files using jsp
how to download the uploaded folder files using jsp  how to download the uploaded folder files using jsp
Get all file size on FTP Server
In this section we are going to describe how to get size of FTP server files using java
FTP Programming in Java tutorials with example code
will learn how to create program in Java that connects to FTP server and upload/download the files from FTP server. FTP Servers are very popular for sending files... FTP Server : Upload file FTP Server : Download file Use
How to implement FTP using java
How to implement FTP using java  Hi, I am a B.tech student and I want to implement FTP using Java to transfer files and exchange files between FTP client and FTP server. Could anyone help me for How to implement FTP using java
Java FTP Upload
in Java. View the FTP Server : Upload file example for complete code example...Java FTP Upload  Is there any example code for Java FTP Upload? Which is the easy to use Java FTP Upload example program at roseindia.net? Thanks
Java FTP Download
a file from FTP server? Tell me the good and easy to understand code for Java FTP Download. Thanks   Hi, The easiest way to connect to FTP server and then download file from server is to use the Apache FTPClient library. View
FTP File Upload in Java
a connection to FTP Server and perform upload/download/move/delete operations... that uploads the file on FTP server. Using Apache FTPClient it's very...); // Upload file to the ftp server
Image uploading in FTP Server
Image uploading in FTP Server  I want to upload images to a ftp server every 1 hour automatically using java..please help
Upload csv or .xlsx file from JSP form to Database Table using servlet
Upload csv or .xlsx file from JSP form to Database Table using servlet  dear sir, i need the Servlet code that reads the .xlsx or CSV excel file... to Submit My Project i am using following code which is Working for .xls Excel file
ftp server
ftp server  ftp server
ftp server
ftp server  ftp server
ftp server
ftp server  ftp server
How display a image on servlet from file upload - JSP-Servlet
How display a image on servlet from file upload  Dear Sir, My issue is: How display a image on servlet from file upload I receive your answer today... upload page Employee Image

Ads