在使用表单传送数据的时候,如果form 加了enctype="multipart/form-data" 这个属性,那么表单请求传到另一个jsp或servlet 里时
是不能用request.getParameter()来获取到各个表单元素的值的。
可以通用这样(上传组件提供的API):
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());
} ......
**************************************************************************************
使用multipart/form-data上传时,发送的请求和一般的http不一样,需要转化后才能读其他参数。
如果你用spring,它提供一个MultiRequestResolver,只需要:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
然后就能正常读取参数:
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之类的字段的值 *****/
return new DefaultMultipartHttpServletRequest(request, multipartFiles, parameters);
}
catch (FileUploadBase.SizeLimitExceededException ex) {
throw new MaxUploadSizeExceededException(this.fileUpload.getSizeMax(), ex);
}
catch (FileUploadException ex) {
throw new MultipartException("Could not parse multipart request", ex);
}
}
**************************************************************************************
<form name="userInfo" method="post" action="first_submit.jsp" ENCTYPE="multipart/form-data">
表单标签中设置enctype="multipart/form-data"来确保匿名上载文件的正确编码。
如下:
<tr>
<td height="30" align="right">上传企业营业执照图片:</td>
<td><INPUT TYPE="FILE" NAME="uploadfile" SIZE="34" onChange="checkimage()"></td>
</tr>
就得加ENCTYPE="multipart/form-data"。
表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application /x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form- data,才能完整的传递文件数据,进行下面的操作.
enctype="multipart/form-data"是上传二进制数据; form里面的input的值以2进制的方式传过去。
form里面的input的值以2进制的方式传过去,所以request就得不到值了。 也就是说加了这段代码,用request就会传递不成功,
取表单值加入数据库时,用到下面的:
SmartUpload su = new SmartUpload();//新建一个SmartUpload对象
su.getRequest().getParameterValues();取数组值
su.getRequest().getParameter( );取单个参数单个值