0

I am writing an Application to Upload Image files in to the server. I am declaring a form giving the type as enctype="multipart/form-data"> I have declare few hidden variables also inside this form. When I am trying to get the Values of these hidden variable inside a Servlet it comes as "Null". If I remove the enctype, I could retrieve the correct values of the variable.

JSP Form : 

    <FORM name="form1" enctype="multipart/form-data"><!-- T1787NM -->

                <input type=hidden name="stringurl" value="<%=stringurl%>">
                <input type=hidden name="savetype" value="<%=ACTION%>"> 
                <INPUT type="hidden" name="iReq" value="<%=iReq%>">     
                 <INPUT type="File" name="FileUpload" id="fileInput">   
    </FORM>

Servlet : 

String stringurl=(String) req.getParameter("stringurl");
other code..

I want to know how to access these variables in Servlet. Thanks in Advance.

Nagesh
  • 434
  • 1
  • 8
  • 26
  • 1
    Here you can find detailed answer: http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet – user3714601 May 06 '15 at 14:49
  • @user3714601 f I use the above method I can get the values in the form which has enctype declared. But this servlet also handles some form which are not of this type. For those forms following Exception is thrown : **Exception in...org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded** – Nagesh May 06 '15 at 15:18
  • @NageswaranM that is probably a hint that you need to split up your servlet into different servlets. Otherwise you'll have to make your servlet smart enough to check if the request is a multipart request or not so you can know when to use fileupload and when not. The user guide shows you how to do that: https://commons.apache.org/proper/commons-fileupload/using.html – Gimby May 06 '15 at 15:37

1 Answers1

1

You need to write following code in servlet

    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (!isMultipart) {
    } else {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<?> items = null;
        String stringUrl = null;
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        Iterator<?> itr = items.iterator();
        while (itr.hasNext()) {
            FileItem item = (FileItem) itr.next();
            if (item.isFormField()) {
                System.out.println("Form Field");
                String parameterName = item.getName();
                if(parameterName.equals("stringurl")) {
                    stringUrl = item.getString();
                }
            } else {
                // upload file logic here
            }
        }
    }

likewise you need to get all other form fields using if else ladder.

Also you have to add commons-fileupload.jar, commons-logging.jar and commons-io.jar to project build path.

ELITE
  • 5,815
  • 3
  • 19
  • 29
  • If I use the above method I can get the values in the form which has enctype declared. But this servlet also handles some form which are not of this type. For those forms following Exception is thrown : Exception in...org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded – Nagesh May 06 '15 at 15:11
  • add the code for other forms in if(!isMultipart) { ..... } – ELITE May 07 '15 at 05:03