Multipart / form-data不支持request.getparamerter [英] Multipart/form-data does not support for request.getparamerter
问题描述
我有一张表格。它有文件上传部分以及几个输入字段。我使用 request.getParameter(
)从jsp获取值到servlet。
但是当我添加 encrypt = multipart
时,request.get参数不起作用。它返回null。我知道multipart不支持 request.getParameter()
。是否有任何上传文件的解决方案。我也想使用request.get参数。
I have a form. it has file uploading part as well as several input Fields. i am using request.getParameter(
) to get values from the jsp into the servlet.
But when i add encrypt=multipart
, request.get parameter doesn't work. it returns null. i know multipart does not support for the request.getParameter()
. Is there any solution for upload files. I want to use request.get parameter also.
推荐答案
apache commons库对这样的要求很有用。
apache commons library will be useful for such requirement.
参考: http ://javakart.blogspot.in/2012/11/file-upload-example-using-servlet.html
http://www.tutorialspoint.com/servlets/servlets-file-uploading.htm
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//this will help you identify request is of type multipart or not.
一旦检查,解析请求并使用库获取表单字段和文件项。
once you check, parse the request and get the form fields and File Item using library.
示例:
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)
}
}
这篇关于Multipart / form-data不支持request.getparamerter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!