• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FTP via web application

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Jill Wah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic