1

I was looking how to add image to the form then I found These codes. While integrating this code I failed because I have other fields in the form to be posted when the form is submitted.

So can you please help me to post for instance a field Lastname along with the file and show how can I get it it( Block of code in the servlet to get the lastname) using the same servlet.

user1577291
  • 366
  • 1
  • 3
  • 15
  • Please post the relevant parts of your code (e.g. html form and servlet)! – home Aug 09 '12 at 13:42
  • 3
    Warning: roseindia.net is one of the **worst** sites when it comes to "best practices" (in other words, that site is so full of *bad* practices). Be extremely careful when copypasting examples unmodified and make sure that you have also consulted vendor's own official documentation such as [here](http://commons.apache.org/fileupload/). – BalusC Aug 09 '12 at 14:16

2 Answers2

2

You're mixing Apache Commons FileUpload and Servlet 3.0 @MultipartConfig. Those two are entirely distinct ways to parse multipart/form-data requests. A HTTP request can be parsed only once. So if one of those two ways has already parsed it beforehand, the other way would not be able to parse it anymore and end up with null/empty data.

You should use the one or the other way to parse the request and not both ways. Apache Commons FileUpload was the "de facto" standard to parse multipart/form-data requests before Servlet 3.0 was introduced (Dec 2009). But since Servlet 3.0 there's the new @MultipartConfig annotation and the new request.getPart() method which makes Apache Commons FileUpload superfluous.

When using Apache Commons FileUpload, you should remove the Servlet 3.0 @MultipartConfig and all request.getParameter() lines and extract the "regular" request parameters from the List items instead.

When using Servlet 3.0 @MultipartConfig, you should remove all code related to Apache Commons FileUpload and use request.getPart() instead to obtain the uploaded file.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Ok The annotation @MultipartConfig was used because previous I failed. previously It was like you see after removing the annotation. – user1577291 Aug 09 '12 at 14:34
  • I see that you removed it from your question as well (you actually shouldn't do that as that would lead to confusion among future readers...). In your real code you should by the way also have removed all `request.getParameter()` lines and use Apache Commons FileUpload exclusively to get the regular parameters, exactly as stated in my answer. – BalusC Aug 09 '12 at 14:39
  • Thanks for see also , First I realized the error of using request.getParameter because the form without the file worked well, but after the file has been added it returned null to all field. – user1577291 Aug 09 '12 at 14:47
  • I'm afraid you didn't understand my answer at all, most likely because you don't understand the "under the hoods" workings at all. I'd suggest to carefully read the "See also" link at the bottom of my answer to understand how exactly uploading files works. Once again, to fix your problem you should **remove** all `request.getParameter()` lines (because they parse the request before Apache Commons FileUpload get chance to do so!) and extract them using Apache Commons FileUpload instead. – BalusC Aug 09 '12 at 14:49
  • Ok I understood and even if I didn't do automatically, as you see in my previous message I wrote that after removing it in my local machine. So What I need now is to get clear field names usin FileItem – user1577291 Aug 09 '12 at 15:01
  • Thank you @BalusC for your answers. Now I am uploading and displaying images. My questions were solved successfully. – user1577291 Aug 10 '12 at 23:14
  • Ok I was asking myself how to mark the correct answer. Thanks – user1577291 Aug 11 '12 at 11:24
  • @BalusC but how do you get the form fields values uploaded together with the image? I believe this is what the OP was asking but if you answered it I didn't get it. – qualebs Mar 27 '22 at 15:27
  • @qualebs: it's detailed in the answer behind the "See also" link. – BalusC Mar 28 '22 at 07:37
0

The question is a little confusing, you want to use the apache commons fileupload library in the servlet and do something like this to grab the image file upload.

            DiskFileItemFactory  fileItemFactory = new DiskFileItemFactory ();


        ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
        try {
            /*
             * Parse the request
             */
            List items = uploadHandler.parseRequest(request);
            Iterator itr = items.iterator();
            while(itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                /*
                 * Handle Form Fields.
                 */
                if(item.isFormField()) {
                    //do stuff here if the item currently is not a file upload
                } 
                                else {
              //DO stuff here to handle the file upload 
                                 }
marcoo
  • 821
  • 1
  • 9
  • 25