File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Uploading Images via browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Uploading Images via browser" Watch "Uploading Images via browser" New topic

Uploading Images via browser

soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300

I would like any ideas about implementing the following scenario.
I have a couple of images on my local machine and HTML-Javascript running on my browser.
Servlets running on the server.
I need to upload these images to the server via the browser.

Now, is there any way i can send the image files with plain HTML/Javascript ?! I have seen people using third party tools to write huge textfiles to the server. But is image transfer even theoretically possible, if so how ?

(Or, is an applet the only way ?)


Thanks Eric, i realised my question was not precise
[ July 30, 2004: Message edited by: soumya ravindranath ]
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
You need to use the server side technology to do it. JavaScript has nothing to do with this. HTML is used with the file upload field.

Moving to servlets.

Atul Prabhu
Ranch Hand

Joined: Dec 17, 2002
Posts: 60

This is the html part :

<FORM ENCTYPE='multipart/form-data'
method='POST' action='/myservlet'>
<INPUT TYPE='file' NAME='mptest'>
<INPUT TYPE='submit' VALUE='upload'>

On the server side :
You need two jar files -- mail.jar & activation.jar

U will have to use MimeMultiPart ---- javax.mail.internet.MimeMultipart
which takes javax.activation.DataSource in the constructor. You will have to provide the implementation of this datasource.

1. Get the count on MimeMultiPart which tells the total files uploaded.
2. Call getBodyPart(index i) .
3. BodyPart has the api's like getFileName(), getInputStream() which is the
content of the part for ex. image file.
4. Now you have to create a file on the server and transfer this inputstream of the bodypart into the newly created file.

Hope this helps.
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
Thanks for the input, Atul, will try it
Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
I wouldn't use the mail classes to upload the file... I'd use a separate set designed especially for uploads. Two that come to mind are the jakarta commons upload classes, and a set from O'Reily (think they're by Jason Hunter, the same person who wrote their servlet book).
I agree. Here's the link:
subject: Uploading Images via browser
It's not a secret anymore!