This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlets + File Upload + HTTP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets + File Upload + HTTP" Watch "Servlets + File Upload + HTTP" New topic

Servlets + File Upload + HTTP

david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
I am still at uni and have just finished a java subject which has lead to more questions. I am working on a site that allows a user to select a file to upload and then calls a servlet to process it. The servlet is called FileUpload and it has a function called saveFile()that gets the file from the HTTP request.
It works fine!
What I would like to know is when a TCP connection is established between the server and the client and the then file is segmented and sent to across the network, when does the FileUpload servlet and its saveFile() function get invoked. Does it get invoked when the the WHOLE file is recieved by the FileUload servlet or does it get invoked when the first few segements arrive?
The servlet doGet function looks like this(pseudo code)
forward user to file upload data page.
The reason I want to know all this is (apart from being interested) I am wanting to write an applet that sends back a time estimate of when the uploaded file will arrive at the server. Once it has arrived I want to send back to the client the names of the files that are being processed.
thanks for your help.
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
It depends on where the file upload is taking place - in my web apps, I have a filter between forms and the servlets, that intercepts all incoming data and files and takes care of the uploading process. But without seeing the actual servlet I can only speculate about its exact workings.
It's my understanding that when the filter (or whatever) is accessing the request object to get the file is when the actual transfer is taking place. AFAIK there is no way to tell how big the file size is, you just keep grabbing the chunks of data until you run out This is how file uploading works for an http transfer - but if you're going the applet route you'll have much more control over file transfer and you'll be able to tell exactly whats going on. The only problem is that you'll have much more control over file transfer and pretty much have to code everything from scratch, opening a socket, transferring data, etc.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Did you write a multi-part mime reader? I'm just doing one. I found an example for JSP (link below) but I'm also writing my own HTTP server (long story) so I'm adapting the algorithms to run outside a J2EE container. Also, the code looked like a C programmer with a very big hammer tried to pound the OO out of Java.
The total content length comes in on a regular HTTP header just like any other GET or POST. This includes all the following parts, I'm pretty sure, so the server knows how big the upload will be. I guess your reader could feed progress events to an object that could talk to your applet. Could be kinda cool.
OReilly's utilities:

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
I had found the code to do my own multipart reader, but it got to be too big of a hassle to get working perfectly - so I started using jakarta's file upload package, pretty nice and easy to use.
david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
Thanks everyone.
Stan tell me do you think it is possible for determine the size of the file that is being uploaded by looking at the http request?
If that is possible that would be great as I would then be able to allow http to manage the upload and the applet to concentrate on the status of the upload.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
I wrote a servlet (packaged as a .war) that does file uploads and downloads and displays a progress bar to the user. It's all done in with servlets - no applets were used. It's free for home use - you can download it here:
I haven't released the source, but if you have any specific questions about how I did it, feel free to ask.

My Java Freeware:<br />MACCC - <a href="" target="_blank" rel="nofollow"></a><br />Nator - <a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: Servlets + File Upload + HTTP
Similar Threads
Uploading Image file from client to the server
get file upload progress for multiple files
Displaying image which is outside my web application
doPut & doDelete
Programmatically simulate user activity on server side to prolongate the HttpSession