easyPoi 导出word文档内存变大
easypoi导出word

前言,其实这个功能已经实现好久了,但是由于现场图片太大一张差不多4M,一个word一共5张图片,搞得导出的word120多M,下载缓慢,且占用内存大!到现在也不知道为为什么图片被导入到word中会变成17M。但总归找到了解决的办法!也很nice
贴上我的代码,主要是在构造ImageEntity实例的时候做了一下处理,之前是直接给url去获取图片,现在先获取图片,然后通过流的形式构造ImageEntity
public ImageEntity getImageEntity(String type, Map<String, Object> photoes) {
ImageEntity image = new ImageEntity();
float height = 0;
if (photoes.get(type) != null) {
//拿到图片,并且获取图片大小
String url = (String) photoes.get(type);
File picture = new File(url);
InputStream in = null;
try {
BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));
float proportion = (float) sourceImg.getHeight() / sourceImg.getWidth();
height = proportion * 85;
//将图片转化为流
in = new BufferedInputStream(new FileInputStream(new File(url)));
int len = 0;
byte[] buffer = new byte[1024];
ByteArrayOutputStream output = new ByteArrayOutputStream();
while((len = in.read(buffer)) > 0) {
output.write(buffer, 0, len);
}
image.setData(output.toByteArray());
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
image.setHeight((int) height);
image.setWidth(85);
image.setUrl((String) photoes.get(type));
image.setType(ImageEntity.Data);
return image;
}
下面是demo
Map<String, Object> map = new HashMap<String, Object>();
map.put("department", "Easypoi");
map.put("person", "JueYue");
ImageEntity image = new ImageEntity();
image.setHeight(119);
image.setWidth(89);
image.setUrl("C:\\Users\\ThinkPad\\Pictures\\1_20191008_014856_35b6ebca8c85472f82f4f0b2354e3107.png");
image.setType(ImageEntity.URL);
map.put("testCode", image);
map.put("testCode1", image);
map.put("testCode2", image);
map.put("testCode3", image);
map.put("testCode4", image);
try {
XWPFDocument doc = WordExportUtil.exportWord07(
"E:\\dsmFile/111.docx", map);// 模板相对路径,可放到resource下
FileOutputStream fos = new FileOutputStream("E:\\dsmFile\\image.docx");
doc.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
后续 ,谁知道这是为啥啊