• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
pedro abs
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" ???
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oK,

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

Tks again !
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the entry in the JSP FAQ on file uploading.
 
pedro abs
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:See the entry in the JSP FAQ on file uploading.



Sorry, I didn't get... where?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok ! Thank you very much
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you got this.............?
please supply me code..........?
thanks
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashish Jani wrote:please supply me code


That's not how things work here. Please read NotACodeMill
 
Greenhorn
Posts: 2
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
     
    For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic