环境:jdk 1.5,tomcat5.0,struts-1.2.9,WinXP
    我在jsp中用<html:file  property="theFile"/>选择上传的文件。
    然后在ACTION中用下面的代码获取文件名:
      FormFile file = formbean.getTheFile();
      String fileName = new String(file.getFileName().getBytes(), "utf-8");
    当文件的后缀名是txt,rar等的时候,中英文文件名均显示正常;
    但是,当后缀名是doc,xls等MS OFFIC文件时,文件名的最后一个字符会变成乱码,导致文件不能上传(如 工作表.xls 会变为 工作??.xls)。
    请问大家这问题如何解决,谢谢!

解决方案 »

  1.   

    到网上找找在web.xml里面加过滤器,就可以了。。
      

  2.   

    我试过在网上别人提供的FILTER了,但还是不行啊……
      

  3.   

    是不是"utf-8"的问题啊 改成GBK试试
      

  4.   

    改成GBK就全部都变乱码了……
      

  5.   

    我试过加了如下过滤器,还是不行:
    public class SetCharacterEncodingFilter implements Filter {  protected FilterConfig filterConfig;
      protected String encodingName;
      protected boolean enable;  public SetCharacterEncodingFilter() {
        this.encodingName = "utf-8";
        this.enable = false;
      }  public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        loadConfigParams();
      }  private void loadConfigParams() {
        //encoding
        this.encodingName = this.filterConfig.getInitParameter("encoding");
        //filter enable flag...
        String strIgnoreFlag = this.filterConfig.getInitParameter("enable");
        if (strIgnoreFlag.equalsIgnoreCase("true")) {
          this.enable = true;
        } else {
          this.enable = false;
        }
      }  public void doFilter(ServletRequest request, ServletResponse response,
                           FilterChain chain) throws IOException, ServletException {
        if(this.enable) {
          request.setCharacterEncoding(this.encodingName);
        }
        System.out.println("-------------------------SetCharacterEncodingFilter---------------------------");
        chain.doFilter(request, response);
      }  public void destroy() {
      }}
      

  6.   

    web.xml:
    <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>com.bnusri.orims.school.course.courseware.tool.SetCharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>utf-8</param-value>
        </init-param>
        <init-param>
          <param-name>enable</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <servlet-name>action</servlet-name>
      </filter-mapping>
      

  7.   

    public ActionForward upload(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    String path = getServlet().getServletContext().getRealPath("/"); FileUploadForm fuf = (FileUploadForm) form;
    FormFile file = fuf.getFile(); String fname = file.getFileName();
    int t = 0;
    for (int i = 0; i < fname.length(); i++) {
    if (fname.charAt(i) == '.') {
    t = i;
    }
    }
    /**
     * 文件名
     */
    String filename = null;
    if (t > 0) {
    filename = fname.substring(0, t);
    }
    String urlFileName = "2006_" + fuf.getLx();
    urlFileName += fname.substring(t);
    /**
     * 保存文件路径
     */
    String filepath = "/pages/lawcase/wenshu/" + urlFileName; /**
     * 先生成文件保存的路径
     */
    if (!(new File(path + "/pages/lawcase/wenshu/")).exists()) {
    (new File(path + "/pages/lawcase/wenshu/")).mkdirs();
    }
    /**
     * 创建文件
     */
    OutputStream os = new FileOutputStream(path + "/pages/lawcase/wenshu/"
    + urlFileName); int readBytes = 0;
    byte buffer[] = new byte[8192];
    InputStream stream = file.getInputStream();
    while ((readBytes = stream.read(buffer, 0, 8192)) != -1) {
    os.write(buffer, 0, readBytes);
    }
    os.close();
    stream.close();
    file.destroy();
    request.setAttribute("message", "上传成功!");
    return mapping.findForward("success");
    }
      

  8.   

    >>>修改一下:String urlFileName = "2006_" + fuf.getLx();改成你要保存的文件名比如:
    >>>String urlFileName = "2006-11-13";
      

  9.   

    感谢楼上的兄弟提供详细代码。我按照您生成子串的思路试过了,但问题依旧……还有,楼上的代码中没有转换编码,保存到OS时文件名不会变乱码吗?
      转换编码后,楼上兄弟的和我的效果一样,即上传一些txt,rar时,中文名没问题,但遇到.doc  .xls 等等offic文件就不行了。
      但无论如何,在下都非常感谢楼上的热心帮助!!
      问题还未解决,还请大家继续赐教!
      

  10.   

    过滤的时候用 encoding 用 GBK
    页面编码 用 
    <%@ page pageEncoding="GBK" contentType="text/html; charset=GBK"%>什么都不用做
    直接上传就是拉
    我已经用这个做了一个文件上传没有出现任何问题
      

  11.   

    页面编码和过滤器的要一致(最好都用utf-8)
      

  12.   

    你可用newxy,好处是:1、不用写代码实现文件上传下载,字符编码自动解决,2、几乎不用配置,3、数据库的增、删、改、查也可不写代码,4、只要有点jsp知识就可。
        newxy网站:http://www.newxy.net
      

  13.   

    重编译了struts源码。可以了。。