0

In my java application i am uploading multiple files using java servlet. All things works fine until i added extra text field in my form.

I am getting null document when i add text field.

Here is my code:-

JSP Form:-

<form action="upload/servlet"  method="post" enctype="multipart/form-data">

        <table>
            <tr>
                <td>Upload File:&nbsp;</td>
                <td><input type="file" name="file" multiple/>
                </td>
                <td style="color: red; font-style: italic;"><form:errors
                        path="file" />
                </td>
            </tr>

            <tr>
                <td>Generate Key</td><td>&nbsp;</td>
                <td><a href="generateWebKey.do"><input type="button" value="Change Key"/></a>
                </td>
                <td>${key}</td>

            </tr>

            <tr>
                <td>Zip Code</td><td>&nbsp;</td>
                <td><input type="text" value="100001" name="zipcode"/>
                </td>
                <td>&nbsp;</td>
            </tr>

                <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="Upload" />
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>

 </form>

Here is my servlet:-

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (!ServletFileUpload.isMultipartContent(req)) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST,"Multipart content expected!");
    }
     ModelMap model = new ModelMap();

    try {
                    @SuppressWarnings("unchecked")
        List<FileItem> files = this.upload.parseRequest(req);
        String userName=req.getSession().getAttribute("username").toString();
        String fileName;
        String contentType;
        byte[] content;
        System.out.print("Zipcode is "+req.getParameter("zipcode"));
        for(FileItem item : files) {
            if(item.isFormField()){
            fileName = item.getName();
            contentType = item.getContentType();
            content = item.get();
            String id=this.indexDocument(fileName, contentType, content,userName,req);
            model.put(id, fileName);
            System.out.println("Done for  "+fileName+ "   id   "+id);
            }
        }

    } catch (FileUploadException e) {
        System.out.println("Error FileUploadException: "+e.getMessage());
        throw new ServletException(e);
    } 
    catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error "+e.getMessage());
    }

    req.setAttribute("message", model);
    req.getSession().setAttribute("tmpRetMessage", model);
//  RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/success.do");
    //dispatcher.forward(req, resp);
    resp.sendRedirect("../success.do");
}

If i add two text fields then getting null document error two times. If i add three times then getting error three times.

Renu Thakur
  • 551
  • 11
  • 35

1 Answers1

1

Here form enctype type multipart (enctype="multipart/form-data"). So request.getParameter() value will be null. So you need to process file field and regular fields means other than file like text, radio, etc separately.

see for more how to get request parameters

Community
  • 1
  • 1
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
  • Thanks. Something is working now. Now i am getting value by 'item.getFieldName()', But in this we cant pass parameter, So i tried to get field name and field value seprately, then i will add condition according to filed name. But i am able to get filed value not field name. 'item.getName()' returning null. – Renu Thakur Jul 29 '15 at 07:08
  • Got it. We need to use getString not getName. Thanks for the help. – Renu Thakur Jul 29 '15 at 07:12