0

I’m trying to upload an excel in servlet and process it. While uploading I set enctype=“multipart/form-data” in my form. But in my servlet .isMultiPart(request) returns false.

JSP code:

function fSubir()

{

fFreezeButtons();

this.document.forms[0].action="../servlet/renault.saf.demandepiece.demandes.servlet.AjouterPoste";

if (this.document.forms[0].Flag.value == "1")

{

this.document.forms[0].Flag.value = "0";

this.document.forms[0].submit();

}

}

Select .xlsx type File :

<input type="submit" value="upload" onclick="fSubir()"/>

My .Jsp also has another form of get method which doesn’t have any enctype.

Servlet code;

public class AjouterPoste extends SapprServlet{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private final String UPLOAD_DIRECTORY = "/appli01/safdev01/saf_cl2/test/";

public void performTask(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    try {
        System.out.println("inside the AjouterPoste class - performTask");
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        System.out.println("Inside doPost:"+isMultipart+":"+request);

Please find the parts of my code on which I’m trying to upload a file.

Charan
  • 143
  • 1
  • 3
  • 8
  • If I remember correctly, you can't upload a file using a JS function for security reasons. So just submit the form without calling fSubir(). – dsp_user Oct 05 '17 at 09:40
  • Getting same exception even after tried removing function fSubir() – Charan Oct 05 '17 at 09:49
  • What exception exactly? Post your jsp code containing your upload form. – dsp_user Oct 05 '17 at 09:57
  • Sorry, not an exception. The value return by .isMultiPart(request) is false. But I’m uploading a file only as input. – Charan Oct 05 '17 at 09:59
  • Take a look here https://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet – dsp_user Oct 05 '17 at 10:18
  • I did, still couldn't resolve that. Actually I create a new small application with one screen and servlet. in that I could able to do the upload and .isMultiPart(request) returns true. – Charan Oct 05 '17 at 10:55
  • But getting False always while using the code I've shared. I'm doing anything wrong? – Charan Oct 05 '17 at 10:56
  • Your code seems fine to me. You haven't posted the form code but since you set multipart/form-data, I assume that it's OK. Try to close your browser and restart your web application because sometimes the browser will use the old JS code even if you removed it (because it's still in cache) – dsp_user Oct 05 '17 at 16:27
  • This is first form i've used.
    ................................
    Second Form:
    Select .xlsx type File :
    When i request.getParamter. I'm not able to retrieve the values of 2nd form in my java class.
    – Charan Oct 06 '17 at 06:08

1 Answers1

0

When you submit a form having multipart/form-data, you can't use request.getParameter(paramName). Instead use the code below (part of the Apache FileUpload library)

try {
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        for (FileItem item : items) {
            if (item.isFormField()) {
                // this part is used instead of request.getParameter
                String fieldName = item.getFieldName();
                String fieldValue = item.getString();
                // do something here
            } else {
                // this is the file processing part
                String fieldName = item.getFieldName();
                String fileName = FilenameUtils.getName(item.getName());
                InputStream fileContent = item.getInputStream();
       ...
            }
        }
    } catch (FileUploadException e) {
        throw new ServletException("exception", e);
    }

You can tell that a specific item is a regular form item (and not a file) by checking that FileItem.isFormField() method returns true.

dsp_user
  • 2,061
  • 2
  • 16
  • 23