When using a form to transmit data, if the form adds the attribute enctype="multipart/form-data", then when the form request is passed to another jsp or servlet,
you cannot use request.getParameter() to get each form element of the value.
This can be used in general (API provided by the upload component):
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
// Create a factory for disk-based file items
org.apache.commons.fileupload.FileItemFactory factory = new DiskFileItemFactory ();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List /* FileItem */items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items .iterator();
while (iter.hasNext()) {
org.apache.commons.fileupload.FileItem item = (org.apache.commons.fileupload.FileItem) iter
.next();
if (item.isFormField()) {
String name = item.getFieldName();
String value = item .getString("GBK");
//out.println(name + "=" + value);
params.put(name.toUpperCase(), value.trim());
} ......
=== ===================================================== ===========================
When using multipart/form-data to upload, the request sent is different from the general http, and it needs to be converted before it can be read Other parameters.
If you use spring, it provides a MultiRequestResolver, just need:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Then you can read the parameters normally:
multipartRequest.getParameter("xxx");
以下是spring的处理方法,必须首先安装commons-fileupload组件:
public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
DiskFileUpload fileUpload = this.fileUpload;
String enc = determineEncoding(request);
// use prototype FileUpload instance if the request specifies
// its own encoding that does not match the default encoding
if (!enc.equals(this.defaultEncoding)) {
fileUpload = new DiskFileUpload();
fileUpload.setSizeMax(this.fileUpload.getSizeMax());
fileUpload.setSizeThreshold(this.fileUpload.getSizeThreshold());
fileUpload.setRepositoryPath(this.fileUpload.getRepositoryPath());
fileUpload.setHeaderEncoding(enc);
}
try {
List fileItems = fileUpload.parseRequest(request);
Map parameters = new HashMap();
Map multipartFiles = new HashMap();
for (Iterator it = fileItems.iterator(); it.hasNext();) {
FileItem fileItem = (FileItem) it.next();
if (fileItem.isFormField()) {
String value = null;
try {
value = fileItem.getString(enc);
}
catch (UnsupportedEncodingException ex) {
logger.warn("Could not decode multipart item '" + fileItem.getFieldName() +
"' with encoding '" + enc + "': using platform default");
value = fileItem.getString();
}
String[] curParam = (String[]) parameters.get(fileItem.getFieldName());
if (curParam == null) {
// simple form field
parameters.put(fileItem.getFieldName(), new String[] { value });
}
else {
// array of simple form fields
String[] newParam = StringUtils.addStringToArray(curParam, value);
parameters.put(fileItem.getFieldName(), newParam);
}
}
else {
// multipart file field
CommonsMultipartFile file = new CommonsMultipartFile(fileItem);
multipartFiles.put(file.getName(), file);
if (logger.isDebugEnabled()) {
logger.debug("Found multipart file [" + file.getName() + "] of size " + file.getSize() +
" bytes with original filename [" + file.getOriginalFilename() + "], stored " +
file.getStorageDescription());
}
}
}
/***** 注意 parameters 就是普通的text之类的字段的值 *****/
<td height="30" align="right">Upload business license image:</td>
<td><INPUT TYPE="FILE" NAME="uploadfile" SIZE="34" onChange="checkimage()" ></td>
</tr>
must add ENCTYPE="multipart/form-data".
The meaning of enctype="multipart/form-data" in the form is to set the MIME encoding of the form. By default, this encoding format is application/x-www-form-urlencoded, which cannot be used for file uploading; only when multipart/form-data is used, the file data can be completely transmitted, and the following operations are performed.
enctype=\"multipart/ form-data\" is to upload binary data; the value of input in the form is passed in binary format.
The value of the input in the form is passed in binary format, so the request will not get the value. That is to say, if this code is added, the transfer will be unsuccessful with request. When adding the
form value to the database, use the following:
SmartUpload su = new SmartUpload();//Create a new SmartUpload object
su.getRequest().getParameterValues (); Take the array value
su.getRequest().
The experience of using enctype="multipart/form-data" of form form
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326655146&siteId=291194637
Recommended
Ranking