Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Uploading a file to a servlet  RSS feed

 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my app, people can upload their picture. But when the request gets to the server, there are problems.

This code makes a request to a front controller, then the controller reads the hidden field to know where the request comes from. When I add ENCTYPE to the form tag, the controller can't read the hidden field with request.getParameter().

I also need a quick way to retrieve the file so I can store it as a BLOB in the database.

Any suggestions?

<form name="${FORM_NAME }"
action="${WEBCONTROLLER }"
method="POST"
ENCTYPE="multipart/form-data">

<input type="hidden" name="${FORM_TYPE}" value="${FORM_TYPE_MAIN_PICTURE_MAINT}"/>

<input name="${ELEMENT_NAME_MAIN_PICTURE_MAINT_UPLOAD_PICTURE}" type="file" accept="image/jpg"/>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you supply a non-default enctype, the getParameter (and ilk) methods cannot be used. This is because if the input stream is read by the container, it won't be available to you to obtain the data of the uplaoded file.

Most 3rd party file upload packages have ways to get the parameters along with the file data.
[ March 09, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bear for the link
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a side but quite related note, when using commons-fileupload I could never figure out a way to get the GET parameters from a request. POST params were easy. However, the COS library makes GET parameters readily accessible but COS's license makes it an unappealing choice.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't really consider that a problem; I can't imagine a scenario where I would be using a GET request to upload a file.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not use get request, however your action can look like:

I didn't know that it's a problem, I always get these parameters without problem. probably it's web container
 
Anant Jagania
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am really not getting one thing is, once we have "browse file" field in HTML page and send it to server, what we get in the servlet if we use getParameter()?

Do we get inputstream at serv er side or what do we get at server side/servlet to upload the file in server?

Regards,
Anant
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Standard servlet machinery can parse only parameters of post and get requests. Encodetype mutlipart-form-data isn't supported by servlets (probably you should check spec),so it's responsibility of a programmer to obtain input stream and process it accordingly Encodetype. Another example is usage servlets for web services. In this case input stream provides XML or some other format data and programmer should take care of processing them as well.

PS I use some servlet framework which takes care of parsing different request types, so I can forget about servlet spec limitations.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by D Rog:
Standard servlet machinery can parse only parameters of post and get requests.


A file uplaod operation typically is a POST request. Servlets parse parameters only if the enctype is the default (x-www-form-urlencoded).


Encodetype mutlipart-form-data isn't supported by servlets


Sure it is. It's just the automatic request body parsing that is disabled unless the enctype is the default as stated above.
[ March 12, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anant Jagania:

I am really not getting one thing is, once we have "browse file" field in HTML page and send it to server, what we get in the servlet if we use getParameter()?


Again, nothing. When you are doing a file upload using a multi-part form, the automatc request body parsing that the servlet base class does on your behalf is disabled.

Do we get inputstream at server side


As was correctly pointed out, the servlet is responsible for reading the input stream and parsing it according to the rules of a multi-part request. This is something that isn't rocket science, but it's far from trivial.

Which is why most people use one of the 3rd party packages that already does it for you. Did you not follow the link I provided?
[ March 10, 2007: Message edited by: Bear Bibeault ]
 
Anant Jagania
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, i went through the link.... but got more confused because when i was using a customised version of struts and uploading file.. i could not get how it is uploading file in the server without any coding done by me, but it was a part of framwork so i couldn't identify.

Well now i m clear about it... thanks a lot to all
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


Sure it is. It's just the automatic request body that is disabled unless the enctype is the default as stated above.

[ March 10, 2007: Message edited by: Bear Bibeault ]
You are correct here.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!