File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Upload files from a HTML <input type= with a servlet" title="" /> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Upload files from a HTML <input type="file"> with a servlet" Watch "Upload files from a HTML <input type="file"> with a servlet" New topic
Author

Upload files from a HTML <input type="file"> with a servlet

pedro abs
Ranch Hand

Joined: Mar 25, 2010
Posts: 33
Hi there

I have an HTML page



and a servlet:



I get the return of 44 bytes from the line in the servlet in the console of TOMCAT when I select an image from the <input type="file"> in HTML page.

LINE 14 -- System.out.println(request.getContentLength());

Why I can't persist this content as a file.

I do not want to use the apache project FileUpload because I want to learn how to do this in that way.
http://commons.apache.org/fileupload/using.html

Could someone give me an example?

Tks !
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

pedro abs wrote:I do not want to use the apache project FileUpload because I want to learn how to do this in that way.

This is very shoirt-sighted. You can't do it "that way". The form must be uploaded as a multi-part form and it must be parsed as such. That's the reason that the upload libraries exist.

Trying to reinvent the wheel may not be the best use of your time.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
pedro abs
Ranch Hand

Joined: Mar 25, 2010
Posts: 33
Dear Sir;

I don't want to try to reinvent the wheel. I Just want to understand the concept.

1) Why that approach exposed in the first post doesn't work?

2) What is the functionality of the HttpServletRequest.getInputStream? It's useful for what?

3) Maybe I have to submit the form with a method="PUT" ???
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Your approach didn't work because you assumed that the only thing in the upload would be the contents of the file being uploaded. That is not the case.

The getInputStream() method provides an InputStream through which the servlet can read the contents of the HTTP request. That's what it's useful for. Of course if you don't understand how the request is structured, and you just guess, you aren't going to get satisfactory results. But that doesn't make the InputStream useless.

Use PUT? Well, that's just another guess on your part, isn't it? You'll find that the format of an HTTP file upload is documented in an RFC somewhere. If you really want to process a file upload with your own code then your first step should be to read that documentation.
pedro abs
Ranch Hand

Joined: Mar 25, 2010
Posts: 33
oK,

Why I have <input type="file">? How can I use it? Could you give-me some example of code?

Tks again !
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

pedro abs wrote:Why I have <input type="file">? How can I use it? Could you give-me some example of code?


I don't understand. You already have an example of the HTML code for a file upload... it's in your question there. But maybe you meant to ask for an example of Java code to handle a file upload at the server? In which case you already have an example of that, too, namely the Apache code.

In other words I don't understand what your goal is.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

See the entry in the JSP FAQ on file uploading.
pedro abs
Ranch Hand

Joined: Mar 25, 2010
Posts: 33
Bear Bibeault wrote:See the entry in the JSP FAQ on file uploading.


Sorry, I didn't get... where?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

The JSP FAQ is linked to from the top of the JSP forum.

You should be familiar with the FAQs and read through them prior to posting a question which may already be answered.
pedro abs
Ranch Hand

Joined: Mar 25, 2010
Posts: 33
Ok ! Thank you very much
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Ashish Jani
Greenhorn

Joined: Apr 21, 2011
Posts: 1

are you got this.............?
please supply me code..........?
thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Ashish Jani wrote:please supply me code

That's not how things work here. Please read NotACodeMill
Adrian Rodriguez
Greenhorn

Joined: Apr 24, 2011
Posts: 2

Well, a couple of comments may be added.

Taken from Java file upload and berkeley.edu File upload form:

The Servlet and JSP specs do not make any special provisions for multipart requests (file uploads).
Servlet and JSP developers either have to depend on third party libraries or make their own framework for this.
The two most popular are Jakarta Commons FileUpload and com.oreilly.servlet


The file upload form looks just like any other form except that:
  • the form tag must specify the POST method
  • the form tag must specify an enctype of multipart/form-data
  • the form must contain an <input type=file> element.



  • So, correcting your HTML code including the encoding type in the form:

    Probably if you make this change, you will obtain more than 44 from request.getContentLength().

    As already said, to process this request you should use a 3rd party library like the ones pointed out before. I'm used to use Commons-FileUpload. Works perfectly and has a complete documentation.

    The field of type "file" together with the appropriate encoding type tells the browser to send the contents of the file in one of the parts of the request.

    BTW, if you return the contents of a .jpg you should better write the appropriate headers... ;)

    Regards
     
     
    subject: Upload files from a HTML <input type="file"> with a servlet