tags:

views:

258

answers:

4

Specifically how can I:

  • Show a button which will let the user browse through his computer and select a file
  • Show a progress bar as the files are uploaded
  • And store the files to a location on the server of the website on which the applet is being run

Any ideas? And yes, I must do this in an applet, and I will make it a trusted/signed applet, have looked into all that.

A: 

I would load/stream in the file, convert it to Base64 (or not) and send a POST request (using URLConnection) containing it to a servlet at the other end.

From a client side viewpoint its easier than doing a multi-part/form-data file upload and have the Commons FileUpload wait for it on the other end1, in my opinion.

1 unless there are libraries available to do just that easily on the client side.

kd304
How would i show a progress bar for it if I just did a POST to a server-side servlet / PHP script?
Click Upvote
If you don't want a 'sub-realistic' progress indication, Show a JProgressBar with setIndeterminate(true) - then it just 'bounces' back and forth during the upload. If you want a realistic progress bar perform the write() to the URLConnection in batches of some KB of data, count the bytes written and pass the percentage to the progress bar. Although, the solutions suggested by others are better than mine.
kd304
+1  A: 

There is no need to sign the applet since 6u10. Instead you can use the FileOpenService to read the file through a standard Swing file chooser (technically implementation dependent). Then it is just a matter of sending the file back as a browser would with a multipart MIME HTTP POST.

Tom Hawtin - tackline
Is 6u10 fairly well adopted?
Click Upvote
Yeah. Of course some people are running jview (nice and secure!!). The last SSR (Synchronized Security Release) was for 6u13 (which Mac OS X users have), and so everyone(!) should have autoupdated to at least that. The 5.0 releases train is still supported, but due to complete its End Of Service Life in a few months from now.
Tom Hawtin - tackline
How do you suggest showing a progress bar for this? Especially if the server-side is a PHP script rather than a servlet?
Click Upvote
It shouldn't matter what the server side is. So long as the network dominates performance (and I think even PHP can cope with calling a library function to copy some bytes).
Tom Hawtin - tackline
Copy which bytes? I'm new to java, can you explain what the server servlet is doing?
Click Upvote
The server side is identical as if this had been a file upload through an HTML form. It need not communicate back to the client (any more than any other HTML POST does) during the upload. The client can quite easily figure out how much of the file has been uploaded (other than a little bit of uncertainty due to buffering).
Tom Hawtin - tackline
A: 

I'm bored. I think I'll just copy the links from the last time I answered this question.

Signed Applet

JFileChooser

JProgressBar

JButton

Apache HttpClient's PostMethod with a MultipartRequestEntity that wraps the JFileChooser's file in a FilePart).

R. Bemrose
+1  A: 

I've had to do just this, with very large (4Gb+) files. The piece of code at the bottom of this Google Answers post helped me out a LOT: http://answers.google.com/answers/threadview?id=193780

It showed a way of uploading files chunked into smaller bits, so you can easily use a JProgressBar.

Eric Wendelin
Will this work if the server-side is written in PHP rather than a servlet?
Click Upvote
It should if you can read in binary data in PHP and append it to a file. I'm not so great at PHP so I can't say with certainty.
Eric Wendelin
Yes it can do all that. So what the client is basically doing is, breaking the file into X chunks, then uploading one chunk at a time. The servlet then takes the chunk, and appends it to the file on server. Is that right? And if so, how does the servlet know the name of the file it should append to? is that passed on by the client?
Click Upvote
1 more thing, is it possible to upload those chunks of data as a string so they'll be treated like a regular string by the server side? if we did it as a multipart file upload, php might not be able to handle it if the file is incomplete because it might come off as corrupt
Click Upvote
You could but I'm not sure I'd recommend it. I don't think the file would be corrupt, though.
Eric Wendelin