POM引用:
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<!-- struts2依赖包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.14</version>
</dependency>
首先工具类代码:
public class ImageUtil {
/**
* @param html
* @return 获得图片列表
*/
public static List<String> getImgList(String html)
{
String regex;
List<String> list = new ArrayList<String>();
Document doc = Jsoup.parse(html, "utf-8");
Elements imgs = doc.getElementsByTag("img");
for(Element img :imgs){
String src = img.attr("src");
list.add(src);
}
return list;
}
/**
* 检测图片路径是否为真实有效的路径方法
* @param src
* @return
*/
public static boolean checkImage(String src){
//使用正则表达式,排除img标签src属性值已经是base64编码的情况
Pattern pattern = Pattern.compile("^data:image/(png|gif|jpg|jpeg|bmp|tif|psd|ICO);base64,.*");
Matcher matcher = pattern.matcher(src);
if(matcher.matches())
return false;
//排除src路径并非图片格式的情况
pattern=Pattern.compile("^.*[.](png|gif|jpg|jpeg|bmp|tif|psd|ICO)$");
matcher = pattern.matcher(src);
if(!matcher.matches())
return false;
//排除图片路径不存在的情况
String path = ServletActionContext.getServletContext().getRealPath(src.substring(5, src.length()));
File file = new File(path);
if(!file.exists())
return false;
return true;
}
/**
* 将html中图片路径转换为base64编码路径
* @param html
* @return
*/
public static String html_ImgToBase64(String html){
Document doc = Jsoup.parse(html, "utf-8");
//Document doc = Jsoup.parseBodyFragment(html,"utf-8");
Elements imgs = doc.getElementsByTag("img");//获取<img>/<a>/<input>...
for(Element img :imgs){
String src = img.attr("src");
if(!checkImage(src))
continue;
//将有效的路径传入base64编码的方法
img.attr("src", ImageBase64.imageToBase64Head(src));
}
//返回base64编码后的html文档
return doc.getElementsByTag("body").html();
}
/**
* 去除html中图片路径带有?---的部分(苹果手机显示存在问题)
* @param html
* @return
*/
public static String html_ImgToNormalUrl(String html){
Document doc = Jsoup.parse(html, "utf-8");
Elements imgs = doc.getElementsByTag("img");
for(Element img :imgs){
String src = img.attr("src");
if(!checkImage(src)){
String newString = src;
if(src.contains("?")){
String[] strings = src.split("\\?wx_fmt=");
newString = strings[0];
}
//将有效的路径传入base64编码的方法
img.attr("src", newString);
}
}
//返回base64编码后的html文档
return doc.getElementsByTag("body").html().replaceAll("\r|\n*","");//去除回行
}
}
base64工具类:
public class ImageBase64 {
/**
* 将图片转换成Base64编码 ,带头文件
* @param imgFile 待处理图片
* @return
*/
public static String imageToBase64Head(String imgFile){
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String type = imgFile.substring(imgFile.length()-3,imgFile.length());
//为编码添加头文件字符串
String head = "data:image/"+type+";base64,";
return head + imageToBase64(imgFile);
}
/**
* 将图片转换成Base64编码
* @param imgFile 待处理图片
* @return
*/
public static String imageToBase64(String imgFile){
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
//拿到上传图片后,图片所在的项目中真实路径
String path = ServletActionContext.getServletContext().getRealPath(imgFile.substring(5, imgFile.length()));
InputStream in = null;
byte[] data = null;
try
{
//读取图片字节数组
in = new FileInputStream(path);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return new String(Base64.encodeBase64(data));
}
//图片转化成base64字符串
public static String GetImageStr()
{//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String imgFile = "d://test.jpg";//待处理的图片
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64编码过的字节数组字符串
}
//base64字符串转化成图片
public static boolean GenerateImage(String imgStr)
{ //对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
String imgFilePath = "d://222.jpg";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
}
catch (Exception e)
{
return false;
}
}
}