Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Ajax upload and download files from ftp server using servlet
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


tin linn soe
upload and download files from ftp server using servlet
2 Answer(s)      5 years and 10 months ago
Posted in : Ajax



Advertisement
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 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 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 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
php download file from ftp
php download file from ftp  Need to download files from ftp using php. a simple example
FTP Server : Download file
This tutorial contains description of file downloading from the FTP server using java
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 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
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 Server : List Files and Directories
In this tutorial we will discuss how to list files and directories on FTP server using java
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
FTP Server: List all files name
This tutorial represents how to list the entire files name on FTP server using java
FTP Server : Upload file
This tutorial contains description of file uploading to the FTP server using java
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
Delete a file from FTP Server
In this section you will learn how to delete file from 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
FTP server  How to store a series of files in a 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
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
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
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...("MKD"+directoryName); Change the working directory : // Here "ftp" is the Object
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
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
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
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 server
ftp server  ftp server
 

 

 

DMCA.com