Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Why I cannot do ftp in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why I cannot do ftp in servlet" Watch "Why I cannot do ftp in servlet" New topic
Author

Why I cannot do ftp in servlet

xiaorui chen
Greenhorn

Joined: Sep 05, 2000
Posts: 9
I am able to do ftp in standalone application.
However, when I put the similar code in servlet, the servlet hang for a long while, then "The page cannot be displayed".
I can see in the log file that the conenction is fine, it hangs when ftp begin to do the real transfer of the data part.
Thanks.

import java.io.*;

public class FtpServlet extends HttpServlet {

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

String server = "133.133.1.133";
int port = 21;
String username = null;
String password = null;
String filename = null;
String rdir = null;
String ldir = null;

res.setContentType("text/html");
PrintWriter out = new PrintWriter(res.getOutputStream());
rdir = req.getParameter("rdestination");
ldir = req.getParameter("lsource");
String lfname = req.getParameter("lfname");
String rfname = req.getParameter("rfname");
if(rfname == null) {
rfname = lfname;
}
else if(rfname.trim().equals("")) {
rfname = lfname;
}
else {
rfname = req.getParameter("rfname");
}


out.println("Hello");
FTPC ftpclient = null;
try {


ftpclient = new FTPC(server, 21, true);
ftpclient.login("sss", "sss");
ftpclient.setRemoteDirectory(rdir);
ftpclient.storeFile(rfname, ldir + "/" + lfname, 1);
ftpclient.logout();


}catch(FTPException ftpe) {
ftpe.printStackTrace();
}

out.flush();
out.close();
}
}
Anshuman Purohit
Greenhorn

Joined: Jan 24, 2004
Posts: 1
use FTPServlets instead of HTTPServlet.


Anshuman Purohit<br />SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why I cannot do ftp in servlet