<html:file property="custPrdCurForm.bigCustAgreementFile" size="35"></html:file>
如果提交時不加 enctype="multipart/form-data“ 就會報錯:
java.lang.IllegalArgumentException: - argument type mismatch - had objects of type "java.lang.String" but expected signature "org.apache.struts.upload.FormFile"
表單中enctype="multipart/form-data"的意思,是設定表單的MIME編碼。預設情況,這個編碼格式是application/x-www-form-urlencoded,不能用於檔案上傳;只有使用了multipart/form-data,才能完整的傳遞檔案資料,進行下面的操作.
enctype="multipart/form-data"是上傳位元據; form裡面的input的值以2進位的方式傳過去。
表單form的enctype="multipart/form-data"使用體會
在使用表單傳送資料的時候,如果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( );取單個參數單個值