upload and download files from ftp server using servlet

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.........









Related Tutorials/Questions & Answers:
upload and download files from ftp server using servlet - Ajax
for upload and download files from ftp server using servlet and how to use...upload and download files from ftp server using servlet  Hi,Sir... to disconnect from FTP server " + "after server refused connection. "+e.toString
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
Advertisements
upload and download files - JSP-Servlet
upload and download files  HI!! how can I upload (more than 1 file) and download the files using jsp. Is any lib folders to be pasted? kindly... and download files in JSP visit to : http://www.roseindia.net/jsp/file_upload
How to download files from server to local disk using ZIP format in JSP page - JSP-Servlet
How to download files from server to local disk using ZIP format in JSP page  hi i have an application to upload and download multiple files. As i can upload multiple files .now i have to download all uploaded files on local
upload files to apache ftp server - Ajax
upload files to apache ftp server  hi,sir I want to upload multiple files to apache ftp server. I am using ajax framework for j2ee.Now I am okay for my customize code for my ftp testing.But I want to upload and download to ftp
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
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 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
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
FTP Server : Download file
This tutorial contains description of file downloading from the FTP server using java
php download file from ftp
php download file from ftp  Need to download files from ftp using php. a simple example
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
File Upload in FTP - JSP-Servlet
File Upload in FTP  hi sir, i am doing upload files in FTP using java application. is there any jar file to use the privileges of sun.net.ftp. i am writing my program in java servlet. i am getting errors like 1.cannot Find
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
FTP FILE UPload - JSP-Servlet
FTP FILE UPload  Hi sir, i am doing the file upload via ftp.... exception javax.servlet.ServletException: Servlet execution threw an exception root cause java.lang.NoClassDefFoundError: org/apache/commons/net/ftp
FTP Server : List Files and Directories
In this tutorial we will discuss how to list files and directories on 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
FTP Server: List all files name
This tutorial represents how to list the entire files name on FTP server using java
Upload and Download in JSP - JSP-Servlet
Upload and Download in JSP  Respected Sir/Madam, I am... and downloading a file in JSP.. When the admin clicks upload, he must be able to upload files and similarly when the user clicks the download option, he must be able
Upload and download file - JSP-Servlet
Upload and download file  What is JSP code to upload and download a document in a web page?  Hi Friend, Try the following code to upload... from a database but before downloading a file, server should ask me
FTP Server : Upload file
This tutorial contains description of file uploading to the FTP server using java
Delete a file from FTP Server
In this section you will learn how to delete file from FTP server using java
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
File download from server to client machine - JSP-Servlet
File download from server to client machine  hi, I want to save a pdf file to my client machine from a button provoded in a jsp page.The file... the generated report in server as follow. exporter.exportIntoPdfFile("c://reports
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
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 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
File Upload And download JSP Urgent - JSP-Servlet
Download in JSP.. In the Admin window, There must be "Upload" provision where admin can upload files.. And in the user window, There must be a "Download" provision...File Upload And download JSP Urgent  Respected Sir/Madam, I
how to copy the directory in to ftp server using java
how to copy the directory in to ftp server using java  how to copy the directory in to ftp server using java ?   Hello venaktehs As much..._TO_REPLACE_2 // Here "ftp" is the Object of Ftp ftp.setDir(directoryName); Now
download code using servlet - Servlet Interview Questions
download code using servlet  How to download a file from web to our system using Servlet
Using a image for Browse button instead of normal html Browse button for Uploading files from a JSP - JSP-Servlet
Using a image for Browse button instead of normal html Browse button for Uploading files from a JSP  I am using the following code snippet... a Upload button, when I upload the file selected, it gives me errors
Java FTP Library
connection with FTP server, rename, download , upload files, and delete files... where we want to write a pure Java application that must download files from... few feature are following here: * FTP files directly from your
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
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
upload and download all kinds of files in jsp - Java Beginners
upload and download all kinds of files in jsp  how to upload and download all kinds of files ex: image file,pdf,.xls in jsp. your help is highly appreciated.  we can upload the all type s of files into oracle 10g
JSP Upload and Downloading files - JSP-Servlet
, R.Ragavendran..   Hi friend, Code to download file from...JSP Upload and Downloading files  Respected Sir/Madam, Very... and downloading files in JSP, I am facing a problem.. 1) The file name is getting inserted
FTP server
FTP server  How to store a series of files in a ftp server using java
FTP File Upload in Java
a connection to FTP Server and perform upload/download/move/delete operations...FTP File Upload in Java This tutorial shows you how you can write Java program that uploads the file on FTP server. Using Apache FTPClient it's very
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
Creat a folder, inside it upload and download files in jsp and mysql
Creat a folder, inside it upload and download files in jsp and mysql  Create one or more folder .inside it we can upload and download multiple files   Here is an application that will upload the file and save
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
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
ftp server
ftp server  ftp server
ftp server
ftp server  ftp server
ftp server
ftp server  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
Simple FTP upload in Java
Simple FTP upload in Java  How to write a program for Simple FTP upload in Java? Any code example of Simple FTP upload in Java will be very helpful for me. Thanks
upload a file and write it in JSP using servlet
upload a file and write it in JSP using servlet  Hello, I'm facing a problem here. I want to upload a file through abc.jsp and write the contents of file using a servlet in xyz.jsp. It is supposed to be a excel file which
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... Directory FTP Server : Upload file FTP Server : Download
File Upload in J2ee on solaris machine using sftp - JSP-Servlet
File Upload in J2ee on solaris machine using sftp  Hi, Currently we are using FTP to upload the file from our J2EE web application. This is working perfectly fine. we are using below command to do so. ftp://abc

Ads