How to submit Jsp Page with encoding multipart/for

2019-09-17 04:28发布

I am submitting html form with encoding (multipart/form-data) I have following fields in Jsp Page First name Last Name file name to be uploaded

file is uploaded perfectly how to get first name and last name ? I want to save in database.

1条回答
别忘想泡老子
2楼-- · 2019-09-17 04:56

You need to use the same API to extract the text fields as you've used to get the file content. Assuming that you're using the (de facto standard) Apache Commons FileUpload for this, then you need to act on whenever FileItem#isFormField() returns true.

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);
}

See also:

查看更多
登录 后发表回答