Uploading files using a servlet?

Note:

Before reading this question and its answer, check that your input element has an attribute name.

I am trying to upload a file using a servlet. The Eclipse console does not show errors. But the file does not load. Everything seems beautiful to me. But I’m wrong somewhere.

In console, I get just

Inside Servlet //Printed by code
Items: [] // Printed by Cdoe

HTML code:

<form action="ImageUploadServlet" method="post" enctype="multipart/form-data">
<table>
<tr>
  <td><label>Select Image: </label></td>
  <td><input type="file" id="sourceImage" /></td>
   <tr>
        <td colspan="3">
         <input type="submit" value="Upload"/><span id="result"></span>
        </td>
      </tr> 
</table>
</form>

Servlet Code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
    System.out.println("Inside Servlet");
    if(!isMultiPart){
        System.out.println("Form type is not multipart/form-data");
        System.out.println("File Not Uploaded");
    }
    else
    {
        FileItemFactory dfit = new DiskFileItemFactory();
        ServletFileUpload sfu = new ServletFileUpload(dfit);
        List aList = null;

        try {
            aList = sfu.parseRequest(request);
            System.out.println("Items: "+aList);
        } 
        catch (FileUploadException fue) 
        {
            fue.printStackTrace();
        }

        Iterator itr = aList.iterator();
        while(itr.hasNext())
        {
            FileItem fi = (FileItem) itr.next();
            if(fi.isFormField())
            {
                System.out.println("File Name: "+fi.getFieldName());
                System.out.println("File Size: "+fi.getSize());

                try 
                {
                    File f = new File("D:/MyUploads/", fi.getName());
                    fi.write(f);
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
            else
            {
                System.out.println("It Not Form Item;");
            }
        }
    }
}
}

Any suggestions would be appreciated.

Thank!

+3
source share
2 answers

There are two problems:

First, you need to specify the field a name. It becomes the name of the request parameter.

<input type="file" id="sourceImage" name="sourceImage" />

-, else FileItem#isFormField() . sysout.

if (item.isFormField()) {
    // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
    String fieldname = item.getFieldName();
    String fieldvalue = item.getString();
    // ... (do your regular form field processing job here)
} else {
    // Process form file field (input type="file").
    String fieldname = item.getFieldName();
    String filename = FilenameUtils.getName(item.getName());
    // ... (do your uploaded file job here)
    File file = new File("D:/MyUploads/", filename);
    item.write(file);
}

, FilenameUtils#getName() , MSIE . . FileUpload.

, . , .

. :

+7

Source: https://habr.com/ru/post/1775010/


All Articles