我的程序要把图片的路径替换为cid
程序如下:public MimeMultipart CreatHtmlMultipart(List<String> aContentpic) throws IOException, MessagingException{
   logger.info("创建带图片html MimeMultipart");
   MimeMultipart contentMultipart = new MimeMultipart("related");
   //创建保存图片的MimeBodyPart并保存
   //图片cid为图片编号,0开始
   List<String> Contentpic = aContentpic;
   logger.info("List contentpic.size() == "+ new Integer(Contentpic.size()).toString());
   
   for(int i=0;i<Contentpic.size();i++)
   {
   MimeBodyPart tempBodyPart = new MimeBodyPart();
   String filename = Contentpic.get(i);
   FileDataSource fileDataSorce = new FileDataSource(filename);
   tempBodyPart.setDataHandler(new DataHandler(fileDataSorce));
   tempBodyPart.setContentID(new Integer(i).toString());
   String newPicName ="cid:"+new Integer(i).toString();
   text.replaceAll(filename, newPicName);
   contentMultipart.addBodyPart(tempBodyPart);    
   }
   
  //创建代表MIME消息的MimeMultipart对象 
 
   
   //创建保存html正文的MimeBodyPart对象,保存到MimeMultipart中
   MimeBodyPart htmlBodyPart = new MimeBodyPart();
   htmlBodyPart.setContent(text,"text/html;charset=gb2312");
   contentMultipart.addBodyPart(htmlBodyPart);            
   return contentMultipart;
   }
异常如下:Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 3
I:/my doc/work/email/test.gif
   ^其中/ 是windows那种反斜杠,这里不能打反斜杠

解决方案 »

  1.   

    问题代码应该是这行
      text.replaceAll(filename, newPicName); 
      

  2.   


      String filename = Contentpic.get(i)这句改为:
      String filename = Contentpic.get(i).toString();
    不行的话用try..catch捕获下这个异常
      

  3.   

    text.replaceAll(filename, newPicName);filename 中含有正则表达式的特殊字符,正则表达式引擎将其解释为语法错误,改成:text.replace(filename, newPicName);这样就行了,按照普通字符来工作。replaceAll 方法的第一个参数是一个正则表达式。
    replace 方法虽然采用采用正则表达式工具工作的,但在其内部已经进行了字面模式处理,
    所有的字符都视为普通字符处理。不要被 replaceAll 中的 All 欺骗了,这两种方法都替换所有的字符串,只是采用的形式
    不同已。
      

  4.   

    恩,看懂了楼上的,问题是,我用replace替换不了2008-5-30 8:43:08 Ctrl.Editer CreatHtmlMultipart
    信息: Editer ; text == <html>
      <head>  </head>
      <body>
        <p style="margin-top: 0">
          <img src="I:\my doc\work\email\test.gif">
          
        </p>
      </body>
    </html>
    本来要替换
    I:\my doc\work\email\test.gif
    为cid:0的
    应该怎么搞?
      

  5.   

    String text = "<img src='I:\\my doc\\work\\email\\test.gif'>";
    text = text.replaceAll("I:\\\\my doc\\\\work\\\\email\\\\test.gif", "cid:0"); 
    System.out.print(text);
      

  6.   

    用replace还是出问题
    用replace的代码如下:
       public MimeMultipart CreatHtmlMultipart(List<String> aContentpic) throws IOException, MessagingException{
       logger.info("创建带图片html MimeMultipart");
       MimeMultipart contentMultipart = new MimeMultipart("related");    List<String> Contentpic = aContentpic;
       logger.info("List contentpic.size() == "+ new Integer(Contentpic.size()).toString());
      //创建代表MIME消息的MimeMultipart对象 
       //创建保存html正文的MimeBodyPart对象,保存到MimeMultipart中
       MimeBodyPart htmlBodyPart = new MimeBodyPart();
       htmlBodyPart.setContent(text,"text/html;charset=gb2312");
       contentMultipart.addBodyPart(htmlBodyPart);    
       
       //创建保存图片的MimeBodyPart并保存
       //图片cid为图片编号,0开始
       for(int i=0;i<Contentpic.size();i++)
       {
       MimeBodyPart tempBodyPart = new MimeBodyPart();
       String filename = Contentpic.get(i);
       FileDataSource fileDataSorce = new FileDataSource(filename);
       tempBodyPart.setDataHandler(new DataHandler(fileDataSorce));
       tempBodyPart.setContentID(new Integer(i).toString());
       String newPicName ="cid:".concat(new Integer(i).toString());
       logger.info("Editer ; newPicName == "+ newPicName);
       logger.info("Editer ; replace 前 text == "+ text);
       text.replace(filename,newPicName);//不工作???
       logger.info("Editer ; replace 后 text == "+ text);
       contentMultipart.addBodyPart(tempBodyPart);    
       }    return contentMultipart;
       }然后运行结果是:
    2008-5-30 9:05:38 Ctrl.Editer CreatHtmlMultipart
    信息: Editer ; replace 前 text == <html>
      <head>  </head>
      <body>
        <p style="margin-top: 0">
          <img src="I:\my doc\work\email\test.gif">
          
        </p>
      </body>
    </html>2008-5-30 9:05:38 Ctrl.Editer CreatHtmlMultipart
    信息: Editer ; replace 后 text == <html>
      <head>  </head>
      <body>
        <p style="margin-top: 0">
          <img src="I:\my doc\work\email\test.gif">
          
        </p>
      </body>
    </html>
      

  7.   

    replace("I:/my doc/work/email/test.gif","cid:0");
      

  8.   

    我路径已经改成用/来做分隔符了。
    然后再用replace,
    replace("I:/my doc/work/email/test.gif","cid:0");
    的执行效果如下:

    2008-5-30 10:02:26 Ctrl.Editer CreatHtmlMultipart
    信息: Editer ; replace 前 text == <html>
      <head>  </head>
      <body>
        <p style="margin-top: 0">
          <img src="I:/my doc/work/email/test.gif">
          
        </p>
      </body>
    </html>
    2008-5-30 10:02:26 Ctrl.Editer CreatHtmlMultipart
    信息: Editer ; replace 后 text == <html>
      <head>  </head>
      <body>
        <p style="margin-top: 0">
          <img src="I:/my doc/work/email/test.gif">
          
        </p>
      </body>
    </html>