I am using the apache.commons.net package to FTP a file up to the server for processing. I got the example simple application to work, but I am having problems when I put it in the
servlet and tried to run it on the server. My question is, can I FTP from an HTML/JSP form or can it only be done with an application?
This is how I want the web app to work. From the web page the user will click on the browse button and navigate to a 18MB excel file. They click the submit button. The file is uploaded to a servlet, and I basically parse through (using Jakarta POI) the file and save the data to our database. This works fine running on our local machine - processing usually takes about 5-6 minutes. When we move it the integration
test environment, it takes a little longer and while the processing does complete, on files larger than about 6MB an Internal Server Error 500 is displayed back to the browser. In Acceptance test, if I try to upload any file larger than about 6MB, it 'tries' for about 2 minutes and comes back with the Internal Server Error 500 message, it didn't even get to the Servlet. According to the admins - they can't find any error messages anywhere.
With that being said, I thought I'd try to FTP instead of using HTTP (well HTTPS). So my plan was the user has the same screen, but instead of trying to upload it to the WEB-INF directory, I FTP it to an FTP server, then immediately FTP it down to the WEB-INF directory and then the
Java code processes the data. The problem I keep running into though is that when I try to FTP the file, the code is looking for the file on the webserver, not on the client's machine.
Here is some code:
File lUploadFolder = new File(aUploadPath);
//Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
//Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
//Parse the request
List lItems = upload.parseRequest(aRequest);
String lUploadFilePath = "";
Hashtable lParameters = new Hashtable();
File lUploadFile = null;
for (int i = 0; i < lItems.size(); ++i)
{
FileItem lItem = (FileItem) lItems.get(i);
if (lItem.isFormField())
{
String lName = lItem.getFieldName();
String lValue = lItem.getString();
lParameters.put(lName, lValue);
}
else
{
String lName = lItem.getName();
lUploadFilePath = lName;
lUploadFile = new File(lUploadFilePath);
try
{
lItem.write(lUploadFile);
lItem.delete();
}
catch(Exception e)
{
throw new FileUploadException(e.toString());
}
}
}
//FTP .xls file from local to an FTP Server
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
try
{
int reply;
ftp.connect(server);
log.debug("FTP: Connected to " + server + ".");
// After connection attempt,
you should check the reply code to verify success
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
throw new FileUploadException("FTP server refused connection.");
}
}
catch (IOException e)
{
if (
ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
throw new FileUploadException("FTP: Could not conntect to FTP server. " + e.toString());
}
__putfile:
try
{
if (!ftp.login(username, password))
{
ftp.logout();
error = true;
break __putfile;
}
if (binaryTransfer)
ftp.setFileType(FTP.BINARY_FILE_TYPE);
// Use passive mode as default because most of us are behind firewalls these days.
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory("ABCD/MyAppNm");
if (storeFile)
{
InputStream input;
input = new FileInputStream(lUploadFilePath);
ftp.storeFile(remote, input);
input.close();
}
else
{
OutputStream output;
output = new FileOutputStream(local);
ftp.retrieveFile(remote, output);
output.close();
}
ftp.logout();
}
catch (FTPConnectionClosedException e)
{
error = true;
e.printStackTrace();
}
catch (IOException e)
{
error = true;
e.printStackTrace();
}