Get parameter when multipart request in JSP

后端 未结 2 1436
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 14:29
<%@page contentType=\"text/html\" pageEncoding=\"UTF-8\"%>

    
        

        
相关标签:
2条回答
  • 2020-12-21 15:03

    Since you post using multipart encoding ('multipart/form-data') the parameters are not present as you expect.

    If for instance you are using commons-fileupload the parameters would be present as and are identifiable using the 'isFormField' method on the FileItem object.

    This thead on coderanch explains how: coderanch

    Most (every) modern webframeworks abstract this away and make this sort of stuff much easier by the way. Refer this site it will help you

    CODE

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for (FileItem item : items) {
                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 job here)
                } else {
                    // Process form file field (input type="file").
                    String fieldname = item.getFieldName();
                    String filename = FilenameUtils.getName(item.getName());
                    InputStream filecontent = item.getInputStream();
                    // ... (do your job here)
                }
            }
        } catch (FileUploadException e) {
            throw new ServletException("Cannot parse multipart request.", e);
        }
    
        // ...
    }
    
    0 讨论(0)
  • 2020-12-21 15:14

    use Annotation

    @MultipartConfig

    for your Servlet

    0 讨论(0)
提交回复
热议问题