How to submit Jsp Page with encoding multipart/form-data
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.开发者_JAVA技巧
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:
- How to upload files in JSP/Servlet?
精彩评论