试试:
part.getFileName()改成new String(part.getFileName(),"ISO-8859-1")

解决方案 »

  1.   

    说错了:
    part.getFileName()改成new String(part.getFileName().getBytes(),"ISO-8859-1")
      

  2.   

    javamail附件中文乱码:
    /*
            @从BodyPart中提取使用ISO-8859-1编吗的文件名
            @因为BodyPart.getFilename()过程已经对文件名作了一次编码,有时不能直接使用
        */
        public static String getISOFileName(Part body){
            //设置一个标志,判断文件名从Content-Disposition中获取还是从Content-Type中获取
            boolean flag=true;
            if(body==null){
                return null;
            }
            String[] cdis;
            try{
                cdis=body.getHeader("Content-Disposition");
            }
            catch(Exception e){
                return null;
            }
            if(cdis==null){
                flag=false;
            }
            if(!flag){
                try{
                    cdis=body.getHeader("Content-Type");
                }
                catch(Exception e){
                    return null;
                }
            }
            if(cdis==null){
                return null;
            }
            if(cdis[0]==null){
                return null;
            }
            //从Content-Disposition中获取文件名
            if(flag){
                int pos=cdis[0].indexOf("filename=");
                if(pos<0){
                    return null;
                }
                //如果文件名带引号
                if(cdis[0].charAt(cdis[0].length()-1)=='"'){
                    return cdis[0].substring(pos+10,cdis[0].length()-1);
                }
                return cdis[0].substring(pos+9,cdis[0].length());
            }
            else{
                int pos=cdis[0].indexOf("name=");
                if(pos<0){
                    return null;
                }
                //如果文件名带引号
                if(cdis[0].charAt(cdis[0].length()-1)=='"'){
                    return cdis[0].substring(pos+6,cdis[0].length()-1);
                }
                return cdis[0].substring(pos+5,cdis[0].length());
            }
        }
    这个方法,已经经多人验证了,我也在用这个方法,你试通后,马上揭贴吧。
      

  3.   

    javax.mail.internet.MimeUtiltiy.decodeText(part.getFileName())
      

  4.   

    wjmmml(笑着悲伤):非常感谢,前段时间你还帮我解决了另一个问题的,我今天上午揭的帖子。看来你对javamail很熟悉,能否提供一些你常去的关于javamail的资源?有些问题我搜索半天都找不到,只好到这里请教各位。另外,有个疑问:java号称对国际化支持的很好,为什么自己很多的内库都硬编码成ISO的编码?难道是他们程序员的疏忽?真是搞不懂。
      

  5.   

    另外 谁有j2ee1.3.1的源码吗?里面有javamail1.2的源程序。我去sun的网站下载太慢了,给我邮一份行吗?谢谢!
    [email protected]
      

  6.   

    wjmmml(笑着悲伤) :你提供的方法没有效啊。
    倒是用
    new String(part.getFileName().getBytes("iso-8859-1"),"gb2312")
    就可以了