1

Possible Duplicate:
How to upload files to server using JSP/Servlet?

I wrote this code in jsp to send a file to a Servlet:

<input type="file" name="inputFoto" id="inputFoto"/>

and my Servlet is:

{...
File fotoImg = (File) request.getAttribute("inputFoto");
byte[] foto = convertiInArrayByte(fotoImg);
..}

It does not work. How can I get a file in a Servlet from a JSP? Can someone help me? Maybe there are some problems with the path of the file (on my pc)!?!?

Community
  • 1
  • 1
Gio
  • 195
  • 1
  • 5
  • 14
  • what does _it doesn't work_ mean? Have you mapped Servlet correctly in web.xml? – Pradeep Simha Jan 22 '13 at 11:36
  • Yes, it is correctly mapped. The form action is linked to the Servlet. But I hava this exception java.lang.NullPointerException java.io.File.(Unknown Source) @PradeepSimha – Gio Jan 22 '13 at 11:39
  • 2
    is this request MULTIPART ? – MRX Jan 22 '13 at 11:39
  • 2
    You might find the [_Uploading Files with Java Servlet Technology_](http://docs.oracle.com/javaee/6/tutorial/doc/glrbb.html) chapter at Oracle's tutorials useful. – Xavi López Jan 22 '13 at 11:40
  • No, it is a single request. @kshitij – Gio Jan 22 '13 at 11:40
  • 1
    it has to multipart request. – MRX Jan 22 '13 at 11:41
  • 1
    If you are open to using Apache Commons File upload, you might also be interested in [How to upload files to server using JSP/Servlet?](http://stackoverflow.com/q/2422468/851811) – Xavi López Jan 22 '13 at 11:45
  • You cannot use `request.getAttribute("ttt")` when processing `multipart form data`. At first you have to add `type` attribute to your `form` with value of `multipart-form/data` and second you have to use option `request.getPart("inputFoto")`. – Michał Kupisiński Jan 22 '13 at 11:45
  • The method getPart(String) is undefined for the type HttpServletRequest..? @emka86 – Gio Jan 22 '13 at 12:04
  • It's available from Servlet Spec 3.0. What Servlet API version are you using? If belove is it possible to upgrade your project to Servlet API 3.0? – Michał Kupisiński Jan 22 '13 at 12:09

1 Answers1

4

file type of inputs are not simple attributes, they are sent in separate chunk of the request. Therefore you must have at least 2 parts in your HTTP request.

So, you must use Multipart Form Data processing to parse the file. There are a number of examples here, for example:

Most commonly the Apache Commons Fileupload http://commons.apache.org/fileupload is used for this.

Community
  • 1
  • 1
gaborsch
  • 15,408
  • 6
  • 37
  • 48
  • Using the guide in the first link, I have a question: Can I call the method processRequest in the method doPost in the Servlet? @GaborSch – Gio Jan 22 '13 at 11:55
  • I'm not sure I know the exact method you are talking about, but generally `doPost()` is the place to process the request. – gaborsch Jan 22 '13 at 12:05
  • Yes but "The method getPart(String) is undefined for the type HttpServletRequest" @GaborSch – Gio Jan 22 '13 at 12:07
  • I recommend to use Apache Commons Fileupload, there are good examples for that, and they have solved these kind of problems you are facing now. – gaborsch Jan 22 '13 at 12:11
  • OK. Can you help me? I want to process only input type="file" because there are other input in the form and then I want to convert the input type="file" in a byte[]; I tried to use this guide (http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet?answertab=votes#tab-top) but I have a problem with the List: The type List is not generic; it cannot be parameterized with arguments . Thanks @GaborSch – Gio Jan 22 '13 at 12:38
  • "You need to have at least commons-fileupload.jar and commons-io.jar files in the /WEB-INF/lib of your webapp" - there you will find `org.apache.commons.fileupload.FileItem` (and all other necessary files) – gaborsch Jan 22 '13 at 12:44
  • Yes I did it but I have this error on List.@GaborSch – Gio Jan 22 '13 at 13:43
  • You also have to put these jars on your compile classpath, and `import org.apache.commons.fileupload.FileItem;`, naturally. – gaborsch Jan 22 '13 at 13:52
  • I did all these things. I solved that error. When I have the inputStream filecontent, can I convert it in byte[]? (Is it my file??) @GaborSch – Gio Jan 22 '13 at 14:05
  • Now my code in the else is: else { // Process form file field (input type="file"). String fieldname = item.getFieldName(); String filename = FilenameUtils.getName(item.getName()); InputStream filecontent = (InputStream) item.getInputStream(); byte[] foto = IOUtils.toByteArray(filecontent); Is it correct?? – Gio Jan 22 '13 at 14:10
  • Yes, it looks good. It is not necessary to cast `(InputStream)` when assigning to `filecontent`. If it's working, maybe you could accept the answer. – gaborsch Jan 22 '13 at 14:16
  • OK but java.io.FileInputStream cannot be cast to org.omg.CORBA.portable.InputStream. @GaborSch – Gio Jan 22 '13 at 14:20
  • Which is the difference between getfilename() getfieldname()? Which have i to use to have the correct path of my file and convert it to a byte[]??@GaborSch – Gio Jan 22 '13 at 14:22
  • `filecontent` should be `java.io.InputStream` – gaborsch Jan 22 '13 at 14:22
  • `getFilename()` is the file name you want. `getFieldName()` is the field name from the HTML (`name="inputFoto"`) – gaborsch Jan 22 '13 at 14:25
  • I want to have the correct path of the file and then convert it in byte[]. If i convert filecontent in byte[] do I have the byte[] of my image or not?? @GaborSch – Gio Jan 22 '13 at 14:28
  • Yes, you will have them, but you can even check it if you debug your code. – gaborsch Jan 22 '13 at 14:33
  • I changed InputStream in java.io.InputStream but I always have the same exception! @GaborSch – Gio Jan 22 '13 at 14:34
  • [java.lang.ClassCastException: java.io.ByteArrayInputStream cannot be cast to org.omg.CORBA.portable.InputStream] caused by [java.io.InputStream filecontent = (InputStream) item.getInputStream();] @GaborSch – Gio Jan 22 '13 at 14:46
  • 1: Remove import for `org.omg.CORBA.portable.InputStream`, add import for `java.io.InputStream`. 2: Write like this: `InputStream filecontent = item.getInputStream();`. The 2 classes from `java.io` and `org.omg.CORBA` have the same name, and it misleads the compiler – gaborsch Jan 22 '13 at 14:58
  • I solved the problem casting to java.io.InputStream.. – Gio Jan 22 '13 at 15:12
  • I have another problem. In my form I have multiple input; in my Servlet I have multiple getParameter() refer to String (I use FileItem only for type file, not for others); If I insert in my form the value [multipart-form/data] I have problems with all the getParameter(). Can you say me why?? – Gio Jan 22 '13 at 16:28
  • Yes, you have to process also the parameter part of the HTTP request (where your input parameters are coming), not only the File part. See the tutorials, probably there is an example code for that. – gaborsch Jan 22 '13 at 16:32