在Java中经常会碰到富文本使用场景,有些需求则需要将富文本内容转换为Word,并包含图片,没接触过的同学可能一开始会感觉头大,其实可以使用Apache POI库来进行实现。以下是一个简单的例子。
引入本次需要的Maven依赖包
<!--富文本转word begin-->
<!-- jsoup依赖 主要是解析图片标签,然后缩放图片大小-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
<!-- poi依赖-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<!--富文本转word end-->
一:创建工具类
1.1、新增图片处理工具类
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
import java.net.URL;
/**
* @ClassName ImageUtils
* @Description: 图片处理工具类
* @Author bigearchart
* @Date 2024/5/20
* @Version V1.0
**/
@Component
@Slf4j
public class ImageUtils {
/**
* 通过BufferedImage图片流调整图片大小
*/
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_AREA_AVERAGING);
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
return outputImage;
}
/**
* 返回base64图片
* @param data
* @return
*/
public static String imageToBase64(byte[] data) {
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
return encoder.encode(data);
}
/**
* base64转换成byte数组
* @param base64
* @return
* @throws IOException
*/
public static byte[] base64ToByte(String base64) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
// 返回Base64编码过的字节数组字符串
return decoder.decodeBuffer(base64);
}
/**
* BufferedImage图片流转byte[]数组
*/
public static byte[] imageToBytes(BufferedImage bImage) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(bImage, "png", out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
/**
* byte[]数组转BufferedImage图片流
*/
public static BufferedImage bytesToBufferedImage(byte[] ImageByte) {
ByteArrayInputStream in = new ByteArrayInputStream(ImageByte);
BufferedImage image = null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
/**
* 在线图片资源转base
* @param imageUrl
* @return
* @throws IOException
*/
public static String convertToBase64(String imageUrl) throws IOException {
URL url = new URL(imageUrl);
String fileType = imageUrl.substring(imageUrl.length()-3);
String base64Str = "data:" + fileType + ";base64,";
InputStream inputStream = url.openStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
byte[] imageBytes = outputStream.toByteArray();
String base64String = base64Str + Base64.getEncoder().encodeToString(imageBytes);
return base64String;
}
//图片转化成base64字符串
public static String getImageStr(String imgPath) throws IOException {
File file = new File(imgPath);
String fileContentBase64