webwork jakarta 上传乱码解决方案

在使用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了...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值