aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes FTP via web application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "FTP via web application" Watch "FTP via web application" New topic
Author

FTP via web application

Jill Wah
Greenhorn

Joined: Jan 25, 2007
Posts: 2
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();
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
Welcome to JavaRanch.

No, you can't use FTP with HTML/JSP.

A quick glance at the code leaves me confused. The first part seems to be Jakarta FileUpload stuff, which is HTTP only, so you can't use it for FTP.

Then there is some Commons Net FTP code, but that's an FTP client - it needs a server to connect to, and in all likelihood it won't be able to connect to the machine where the XLS file sits.

So I'm not sure how the overall flow should work (and I suspect that you're not quite sure, either).

Barring further information, I'd say investigate why it takes so long to upload the file through HTTP. Could it be that it's not the upload, but the server-side processing of the file that takes so long? In that case, using FTP instead if HTTP wouldn't help to begin with. Also make sure that there's enough memory for POI - it can be quite the resource hog, especially for large files.
[ January 26, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Jill Wah
Greenhorn

Joined: Jan 25, 2007
Posts: 2
Ulf,

Thanks for the quick response. Good to know that I am not overlooking something and that FTP can't be used with HTML/JSP.

Sorry for the confusing code. I was trying to reuse the upload part so I could create the file to FTP based on the file the user browsed to on their PC. So yes, the FileUpload and Net stuff were mixed.

Since FTP is faster than HTTP for large files I was hoping cutting out that time would reduce time enough that it would process ok. The first time we loaded our prototype we didn't have enough memory, but we at least got an OutOfMemoryException. So we increased the heapsize locally (-Xms378m -Xmx512m) and the Websphere Admin put us on our own JVM (reluctantly) with the default JVM heapsize. It processes, and when it completes it redirects to a "Confirmation" JSP with a confirmation message. Based on debugging statements, the page is getting built it just for some reason won't display back to the browser - when the file size is large.

I keep going with the notion that is isn't the code - it is some sort of connection or request timeout or something. Especially since in the Acceptance environment - it doesn't even get to the Servlet. Unfortunately I am at a standstill looking at those type of things b/c the Websphere and Network admins have higher priorities. I can't even get them to tell me if there are differences in how the two environments are setup. (In theory they are supposed to be identical)

We are using HTTPS and we go through IIS to direct us in a cluster environment to one of two assigned IBM Websphere Application Servers. I don't know if that gives you anymore info. I have seen mention of upgrading isapi_redirect.dll to isapi_redirect2.dll filters - but I think those might only be when hitting a Tomcat server?

Anyway, thanks for the initial response and let me know if any of my additional information sparks anymore ideas from you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FTP via web application