在使用webwork2.1的时候,我的jsp全是用的UTF-8国际编码,而我的系统是gbk环境,这时候如果用jakarta上传文件的时候,无论是单个还是多个,只是要中国字全是乱码???怎么办???经过我深入研究它的源代码,发现在
com.opensymphony.webwork.dispatcher.multipart.JakartaMultiPartRequest里面有一个名叫public JakartaMultiPartRequest(HttpServletRequest servletRequest, String saveDir, int maxSize)
throws IOException
这个方法出了问题!原因如下,它在调用DiskFileUpload组件的时候,没有设置其编码,按照惯例,如不设置编码即为系统编码,因为我的系统编码是gbk,很显然地与utf-8出现了冲突.怎么办?改掉它.
/**
* Multipart form data request adapter for Jakarta's file upload package.
*
* @author Bruce Ritchie
*/
public class JakartaMultiPartRequest extends MultiPartRequest {
//~ Instance fields
// maps parameter name -> List of FileItem objects
private Map files = new HashMap();
// maps parameter name -> List of param values
private Map params = new HashMap();
//~ Constructors ///
/**
* Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
* multipart classes (see class description).
*
* @param maxSize maximum size post allowed
* @param saveDir the directory to save off the file
* @param servletRequest the request containing the multipart
*/
public JakartaMultiPartRequest(HttpServletRequest servletRequest, String saveDir, int maxSize)
throws IOException {
DiskFileUpload upload = new DiskFileUpload();
// we must store all uploads on disk because the ww multipart API is missing streaming
// capabilities
upload.setSizeThreshold(0);
upload.setSizeMax(maxSize);
//以下是我加的
String charset = servletRequest.getCharacterEncoding();
if (charset != null) {
upload.setHeaderEncoding(charset);
}
//以上是我加的
if (saveDir != null) {
upload.setRepositoryPath(saveDir);
}
// Parse the request
try {
List items = upload.parseRequest(servletRequest);
for (int i = 0; i < items.size(); i++) {
FileItem item = (FileItem) items.get(i);
log.debug("Found item " + item.getFieldName());
if (item.isFormField()) {
log.debug("Item is a normal form field");
List values = null;
if (params.get(item.getFieldName()) != null) {
values = (List) params.get(item.getFieldName());
} else {
values = new ArrayList();
}
// note: see http://jira.opensymphony.com/browse/WW-633<wbr></wbr>
// basically, in some cases the charset may be null, so
// we're just going to try to "other" method (no idea if this
// will work)
//将下面这句话提前.
//String charset = servletRequest.getCharacterEncoding();
if (charset != null) {
values.add(item.getString(charset));
} else {
values.add(item.getString());
}
params.put(item.getFieldName(), values);
} else if (item.getSize() == 0) {
log.debug("Item is a file upload of 0 size, ignoring");
} else {
log.debug("Item is a file upload");
List values = null;
if (files.get(item.getFieldName()) != null) {
values = (List) files.get(item.getFieldName());
} else {
values = new ArrayList();
}
values.add(item);
files.put(item.getFieldName(), values);
}
}
} catch (FileUploadException e) {
log.error(e);
}
}
经过以上修改,然后再到web.xml里面配置一个SetCharacterEncoding的过滤器并设置其编码为UTF-8就OK了...
com.opensymphony.webwork.dispatcher.multipart.JakartaMultiPartRequest里面有一个名叫public JakartaMultiPartRequest(HttpServletRequest servletRequest, String saveDir, int maxSize)
throws IOException
这个方法出了问题!原因如下,它在调用DiskFileUpload组件的时候,没有设置其编码,按照惯例,如不设置编码即为系统编码,因为我的系统编码是gbk,很显然地与utf-8出现了冲突.怎么办?改掉它.
/**
* Multipart form data request adapter for Jakarta's file upload package.
*
* @author Bruce Ritchie
*/
public class JakartaMultiPartRequest extends MultiPartRequest {
//~ Instance fields
// maps parameter name -> List of FileItem objects
private Map files = new HashMap();
// maps parameter name -> List of param values
private Map params = new HashMap();
//~ Constructors ///
/**
* Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
* multipart classes (see class description).
*
* @param maxSize maximum size post allowed
* @param saveDir the directory to save off the file
* @param servletRequest the request containing the multipart
*/
public JakartaMultiPartRequest(HttpServletRequest servletRequest, String saveDir, int maxSize)
throws IOException {
DiskFileUpload upload = new DiskFileUpload();
// we must store all uploads on disk because the ww multipart API is missing streaming
// capabilities
upload.setSizeThreshold(0);
upload.setSizeMax(maxSize);
//以下是我加的
String charset = servletRequest.getCharacterEncoding();
if (charset != null) {
upload.setHeaderEncoding(charset);
}
//以上是我加的
if (saveDir != null) {
upload.setRepositoryPath(saveDir);
}
// Parse the request
try {
List items = upload.parseRequest(servletRequest);
for (int i = 0; i < items.size(); i++) {
FileItem item = (FileItem) items.get(i);
log.debug("Found item " + item.getFieldName());
if (item.isFormField()) {
log.debug("Item is a normal form field");
List values = null;
if (params.get(item.getFieldName()) != null) {
values = (List) params.get(item.getFieldName());
} else {
values = new ArrayList();
}
// note: see http://jira.opensymphony.com/browse/WW-633<wbr></wbr>
// basically, in some cases the charset may be null, so
// we're just going to try to "other" method (no idea if this
// will work)
//将下面这句话提前.
//String charset = servletRequest.getCharacterEncoding();
if (charset != null) {
values.add(item.getString(charset));
} else {
values.add(item.getString());
}
params.put(item.getFieldName(), values);
} else if (item.getSize() == 0) {
log.debug("Item is a file upload of 0 size, ignoring");
} else {
log.debug("Item is a file upload");
List values = null;
if (files.get(item.getFieldName()) != null) {
values = (List) files.get(item.getFieldName());
} else {
values = new ArrayList();
}
values.add(item);
files.put(item.getFieldName(), values);
}
}
} catch (FileUploadException e) {
log.error(e);
}
}
经过以上修改,然后再到web.xml里面配置一个SetCharacterEncoding的过滤器并设置其编码为UTF-8就OK了...