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.
Could someone give me an example?
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.
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" ???
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 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.
Ashish Jani wrote:please supply me code
That's not how things work here. Please read NotACodeMill
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... ;)