• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why I cannot do ftp in servlet

 
xiaorui chen
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use FTPServlets instead of HTTPServlet.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic