如何利用javamail发送中文名字的附件?我是用jsp做的,tomcat提示找不到文件,因为把附件路径中的中文字符变成了一些不可识别的字符,应该怎么对附件路径进行转换,或者有其他的解决方法吗?谢谢!

解决方案 »

  1.   

    Unicode好像也不行,下面是我写的转换算法,但不管用,请大家看看,该怎么办?谢谢!
    public String trans2uni(String attachName)//把非ASCII码字符转换为unicode编码
    {
         int length=attachName.length();
         String newAttachName="";
         String temp="";
         int first=0;//指示非ASCII字符的位置
         int last=0;//指示ASCII字符的位置
         boolean flag=true;//当前字符类型标志。ASCII字符为true,非ASCII字符为false
         for(int i=0;i<length;i++)
         {
             if( !isASCII( attachName.charAt(0) ) )  flag=false;
             if( isASCII( attachName.charAt(i) ) & (!flag) )
             {
              temp=attachName.substring(last,i);//
    try
    {
    newAttachName+=new String( temp.getBytes(), "unicode" );
    }
    catch(java.io.UnsupportedEncodingException ex)
    {}          last=i;
              flag=true;
             }
             if( !isASCII( attachName.charAt(i) ) & (flag) )
    {
        newAttachName+=attachName.substring(first,i);//
        first=i;
        flag=false;
             }
         }     return newAttachName;
    }public boolean isASCII(char ch)
    {
    if(ch<0||ch>127) return false;
    return true;
    }
      

  2.   

    你这样再试试讷? 
    package redtroy.test;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2005</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */public class String2UTF8 {
      public String2UTF8() {
      }  public static String toUtf8String(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
          char c = s.charAt(i);
          if (c >= 0 && c <= 255) {
            sb.append(c);
          }
          else {
            byte[] b;
            try {
              b = Character.toString(c).getBytes("utf-8");
            }
            catch (Exception ex) {
              System.out.println(ex);
              b = new byte[0];
            }
            for (int j = 0; j < b.length; j++) {
              int k = b[j];
              if (k < 0) {
                k += 256;
              }
              sb.append("%" + Integer.toHexString(k).
                        toUpperCase());
            }
          }
        }
        return sb.toString();
      }  public static void main(String args[]) {
        System.out.println(String2UTF8.toUtf8String("S0507010002热血秒月互换1x0.0.txt"));
      }}
      

  3.   

    大学时 copy网上一人写的
    ////////////////////////
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    import javax.swing.*;   //添加附件
       public void affix() {
      
       String fileAffix;
       Enumeration enum=files.elements();
       while(enum.hasMoreElements()){
      
       try{
        MimeBodyPart mbp=new MimeBodyPart();
        fileAffix=enum.nextElement().toString();
        FileDataSource fs=new FileDataSource(fileAffix);
        mbp.setDataHandler(new DataHandler(fs));
        mbp.setFileName(MimeUtility.encodeWord(fs.getName(),"GB2312",null)); 
                 //防止文件名字中文乱码
        mp.addBodyPart(mbp);
      
                }
             catch(Exception e){
             JOptionPane.showMessageDialog(this,"附件增加出错"+e);
             }
       }
       files.removeAllElements();
       }