邮件发送的时候添加cid:图片显示路径;显示的时候去掉.replaceAll("cid:","");不用下载到本地再替换而来。
1.
//处理带正文的图片的信息rex获取《img src》
public List filterImage(String content,HttpServletRequest request) throws MalformedURLException{
StringBuffer images = new StringBuffer();
String text = content;
String path= request.getSession().getServletContext().getRealPath("/");
int pos=path.lastIndexOf("\\", path.length()-2);
path=path.substring(0, pos+1);
/**过滤img,直取src的值*/
/**String regEx="<img\\s+(?:[^>]*)src\\s*=\\s*([^>]+)>";*/
//String regex = "<\\s*img[^>]*?>";
String regex = "<\\s*img[^>]+src\\s*=\\s*['\"]([^\"'>]+)['\"][^>]*>";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(text);
while(m.find()) {
images.append(m.group(1));
images.append(",");
}
List imgPart= new ArrayList();
String [] returnUrl = null;
if(images.toString().length()>0){
String urls=images.toString().substring(0,images.toString().length()-1);
String [] url = urls.split(",");
returnUrl = new String[url.length];
for(int i=0;i<url.length;i++){
BodyPart img= new MimeBodyPart();
DataHandler dh =new DataHandler(new FileDataSource(path+url[i]));
try {
img.setDataHandler(dh);
img.setHeader("Content-ID", url[i]);//绝对路径的问题
returnUrl[i]="cid:"+url[i];
imgPart.add(img);//将图片处理之后再作为发信的内容
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String [] imagesFlaseUrl = images.toString().split(",");
String sb = text;
for(int j=0;j<returnUrl.length;j++){
sb = sb.replace(imagesFlaseUrl[j], returnUrl[j]);
}
imgPart.add(sb);//最后将string处理《img src="cid:路径" 》
}else {
imgPart.add(content);//没有图片 》
}
return imgPart;
}
2.正文显示的时候去掉:MimeUtility.decodeText(content).replaceAll("cid:", "")
1.
//处理带正文的图片的信息rex获取《img src》
public List filterImage(String content,HttpServletRequest request) throws MalformedURLException{
StringBuffer images = new StringBuffer();
String text = content;
String path= request.getSession().getServletContext().getRealPath("/");
int pos=path.lastIndexOf("\\", path.length()-2);
path=path.substring(0, pos+1);
/**过滤img,直取src的值*/
/**String regEx="<img\\s+(?:[^>]*)src\\s*=\\s*([^>]+)>";*/
//String regex = "<\\s*img[^>]*?>";
String regex = "<\\s*img[^>]+src\\s*=\\s*['\"]([^\"'>]+)['\"][^>]*>";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(text);
while(m.find()) {
images.append(m.group(1));
images.append(",");
}
List imgPart= new ArrayList();
String [] returnUrl = null;
if(images.toString().length()>0){
String urls=images.toString().substring(0,images.toString().length()-1);
String [] url = urls.split(",");
returnUrl = new String[url.length];
for(int i=0;i<url.length;i++){
BodyPart img= new MimeBodyPart();
DataHandler dh =new DataHandler(new FileDataSource(path+url[i]));
try {
img.setDataHandler(dh);
img.setHeader("Content-ID", url[i]);//绝对路径的问题
returnUrl[i]="cid:"+url[i];
imgPart.add(img);//将图片处理之后再作为发信的内容
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String [] imagesFlaseUrl = images.toString().split(",");
String sb = text;
for(int j=0;j<returnUrl.length;j++){
sb = sb.replace(imagesFlaseUrl[j], returnUrl[j]);
}
imgPart.add(sb);//最后将string处理《img src="cid:路径" 》
}else {
imgPart.add(content);//没有图片 》
}
return imgPart;
}
2.正文显示的时候去掉:MimeUtility.decodeText(content).replaceAll("cid:", "")