日期:2014-05-16  浏览次数:20406 次

修改JSPsmartUpload组件实现无中文乱码上传下载

使用JSPsmartUpload组件上传文件,文件名出现中文字符,下载文件,文件名出现中文字符,都会导致文件名在浏览器出现乱码,自己修改了JSPsmartUpload组件原文件,编译打包成.jar文件。

使用说明:

?

一、解决上传出现乱码问题

  1. 我的WEB项目编码一致为“GBK”,为WEB项目编写一个编码过滤器
    package com.model.filter;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.servlet.Filter;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.http.HttpServletRequest;
    
    public class CharacterEncodingFilter implements Filter {
    
        private FilterConfig config;
        private String encoding = "UTF-8";
    
        public void init(FilterConfig config) {
            this.config = config;
            String encoding = config.getInitParameter("encoding");
            if (encoding != null) {
                this.encoding = encoding;
                System.out.println("获取编码成功!!");
            } else {
                System.out.println("无法获取编码,采用默认的UTF-8");
            }
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
            try {
                HttpServletRequest hrequest = (HttpServletRequest) request;
                hrequest.setCharacterEncoding(this.encoding);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CharacterEncodingFilter.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                chain.doFilter(request, response);
            } catch (IOException ex) {
                Logger.getLogger(CharacterEncodingFilter.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ServletException ex) {
                Logger.getLogger(CharacterEncodingFilter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        public void destroy() {
        }
    }
    ?
  2. 过滤器在XML配置文件里需要注册,如下:
        <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>com.model.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>gb2312</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    ?
  3. 在JSP上传页面,编码设置为“GBK”
    <%-- 
        Document   : upload
        Created on : 2010-8-5, 18:36:04
        Author     : Gavin
    --%>
    
    <%@page contentType="text/html" pageEncoding="gb2312"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
            <title>JSP Page</title>
        </head>
        <body>
            <h1 align="center">Test File Upload</h1>
            <p>
            <form action="upload" enctype="multipart/form-data" name="uploadform" method="post" >
                文件路径1:<br>
                <input type="file" name="file1" size="60" /><br>
                <textarea rows="5" cols="69" name="fileinfo1"></textarea><br>
                <input type="file" name="file2" size="60" /><br>
                <textarea rows="5" cols="69" name="fileinfo2"></textarea><br>
                <input type="submit" value="提交" >&nbsp;&nbsp;
                <input type="reset" value="重置">
            </form>
        </body>
    </html>
    ?

二、在JSPsmartUpload组件中增加下载编码,具体过程不再赘述,提供修改编译打包的.jar文件,可直接使用。