Hi Arne,
Add to that i am also using NetComponents class. My purpose is to
create a Web Based ftp to upload the file to the ftp server. So far i
can only upload .txt and .zip. If i upload other file using
ENCTYPE="multipart/form-data". I will receive some error.
Thanks for replying my posting.
======================================================================================
/**
*
* This is to upload the file that the client submitted
* to the ftp server.
*
*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import com.oroinc.net.ftp.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
public class FtpUpload extends HttpServlet{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
HttpSession session = request.getSession(true);
if (!session.isNew()){
DataInputStream inputLocalBuf = null;
String fileName=null;
try{
// Check that we have a file upload request
boolean isMultipart =
ServletFileUpload.isMultipartContent(request);
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
fileName = item.getName();
InputStream uploadedStream = item.getInputStream();
inputLocalBuf = new DataInputStream(uploadedStream);
uploadedStream.close();
}
}
}
catch( FileUploadException fue) {
throw new ServletException("FileUploadError: " +
fue.getMessage(), fue);
}
/* create a UserDetails object. This object will retrieve
* the id & password from the session object*/
UserDetails userDetails = new UserDetails();
userDetails = (UserDetails) session.getAttribute ("userInfo");
String ftpUserId = userDetails.getName();
String ftpPassword = userDetails.getPassword();
String newFileName = null;
StringTokenizer st = new StringTokenizer(fileName,"\\");
while (st.hasMoreTokens()) {
newFileName = st.nextToken();
}
String FTP_SERVER = "192.168.200.100";
String FTP_DIRECTORY = "";
FTPClient ftp = null;
try {
ftp = new FTPClient();
ftp.connect(FTP_SERVER);
int reply = ftp.getReplyCode();
if
(!com.oroinc.net.ftp.FTPReply.isPositiveCompletion(reply))
throw new java.io.IOException("Could not connect to ftp
server " +
FTP_SERVER);
ftp.login(ftpUserId, ftpPassword);
ftp.setFileType(com.oroinc.net.ftp.FTP.BINARY_FILE_TYPE);
ftp.changeWorkingDirectory(ftp.printWorkingDirectory());
try {
/* Upload the file to the client ftp /HOME directory*/
//inputLocal = new FileInputStream(objToFtpPut);
if (!ftp.storeFile(newFileName,inputLocalBuf))
throw new java.io.IOException(
"Copy file to remote ftp host failed");
} finally {
if (inputLocalBuf != null) inputLocalBuf.close();
//if (inputLocal != null) inputLocal.close();
/*Display message to inform that file is downloaded
successfully*/
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD>\n"+
"<TITLE>F T P</TITLE></HEAD>\n" +
"<BODY>\n"+
"<CENTER><IMG src=\"/ftp/images/banner.jpg\"
width=\"760\" height=\"75\">\n" +
"</CENTER>\n<BR>\n<BR>\n" +
"<CENTER>\n<TABLE width=\"760\" border=\"0\"
cellspacing=\"0\" cellpadding=\"0\">\n" +
"<TR>\n<TD>\n"+
"<FONT face=\"Verdana, Arial, Helvetica,
sans-serif\"><B><FONT color=\"#000000\">\n" +
"<FONT size=\"2\"> M E S S A G
E</FONT></FONT></B></FONT>\n" +
"</TD>\n<TD width=\"615\">\n" +
"<HR size=\"1\" width=\"615\" noshade color=\"#990000\"
align=\"right\">" +
"</TD></TR>\n" +
"<TR>\n<TD>\n"+
"<A href=\"/InvalidateSession\">\n"+
"<FONT face=face=\"Verdana, Arial, Helvetica,
sans-serif\" size=\"1\">[Sign Out]</A>\n"+
"</TD></TR></TABLE>\n"+
"<TABLE width=\"760\" border=\"0\" cellspacing=\"0\"
cellpadding=\"0\">\n" +
"<TR>\n<TD>\n" +
"Your file have been uploaded <A HREF= \"" +
response.encodeURL("/Validation") +
"\"> Upload another file </A>\n"+
"</TD>\n<TR>\n</TABLE>\n" +
"<TABLE width=\"760\" border=\"0\" cellspacing=\"0\"
cellpadding=\"0\">" +
"<TR>\n<TD>\n"+
"<HR width=\"760\" align=\"left\" noshade size=\"1\"
color=\"#990000\">"+
"</TD>\n</TR>\n</TABLE>\n</CENTER>\n"+
"</BODY></HTML>");
}
ftp.logout();
} finally {
if (ftp != null) {
try {
ftp.disconnect();
} catch (Exception e) {
}
}
}
}
}
}