2

I've created a form and I need the user to enter some info then upload a picture.
So lets say I have something like this:

<form method="post" enctype="multipart/form-data" action="some servlet/filter">
<input type="file" name="logo">
</form>

I need to use java to change that data to a byte[] then to blob so I can insert to a table. How do I get the data from this form?

A bit of info on this: I created the page using javascript, then when submitted it will call a java function to handle the data from the form. It seems that when I submit the form the data for the file is not passed over to the servlet.

So far the few methods I've tried have returned null and I'm outta ideas...

Any examples/help is greatly appreciated!

I think the main question I have is where is the file data stored before the java file start working on it? Is a special global variable holding the data or something like that?

iCodeLikeImDrunk
  • 17,085
  • 35
  • 108
  • 169
  • possible duplicate of [How to upload files in JSP/Servlet?](http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet) – BalusC Aug 16 '11 at 21:25

3 Answers3

2

You can use the Apache Commons FileUpload library.

The Commons FileUpload package makes it easy to add robust, high-performance, file upload capability to your servlets and web applications.

FileUpload parses HTTP requests which conform to RFC 1867, "Form-based File Upload in HTML". That is, if an HTTP request is submitted using the POST method, and with a content type of "multipart/form-data", then FileUpload can parse that request, and make the results available in a manner easily used by the caller.

Community
  • 1
  • 1
Marcelo
  • 11,218
  • 1
  • 37
  • 51
  • thank you for suggestion! but unfortunately i cannot use outside library =/ – iCodeLikeImDrunk Aug 16 '11 at 22:38
  • If you can't use a library, then you would have to implement the RFC 1867 "Form-based File Upload in HTML" standard http://www.faqs.org/rfcs/rfc1867.html yourself. You can also look at the Apache Commons FileUpload source to understand how it works. – Marcelo Aug 16 '11 at 22:44
  • that i did.. but for some reason im getting this error: 14:06:00,355 ERROR [JsonFilter] java.lang.NoSuchMethodException: byte.(java.lang.String) the logo is byte[] in java and i get data from the form like so: test.prototype.handlesubmit = function(value, form) { value.logo = form.logo.value; this.caller.setLogo(value); } i used a LOT of sysouts in the java servlet to make sure that this will get to it but it has not reached the servlet file.. – iCodeLikeImDrunk Aug 17 '11 at 18:11
1

If I understood you right, you need something similar to this example:

http://www.servletworld.com/servlet-tutorials/servlet-file-upload-example.html

pkk
  • 3,691
  • 2
  • 21
  • 29
1

I used the following tutorial one year ago:

http://balusc.blogspot.com/2009/12/uploading-files-in-servlet-30.html

It looks like it's a lot, but it's actually easy to understand, and it has a lot of good information.

AndrewBourgeois
  • 2,634
  • 7
  • 41
  • 58