Java apachepoi中的图像转换
我需要在浏览器上显示来自Word文档的图像。我正在使用ApachePOI提取图像,但无法在Broswer中打开它。我正在使用下面的代码保存图像Java apachepoi中的图像转换,java,spring,apache-poi,Java,Spring,Apache Poi,我需要在浏览器上显示来自Word文档的图像。我正在使用ApachePOI提取图像,但无法在Broswer中打开它。我正在使用下面的代码保存图像 picTable = doc.getPicturesTable(); List pictures = picTable.getAllPictures(); if(pictures){ println("pictures::"+pictures.size()) Picture pic = (Picture)pictures.g
picTable = doc.getPicturesTable();
List pictures = picTable.getAllPictures();
if(pictures){
println("pictures::"+pictures.size())
Picture pic = (Picture)pictures.get(0);
file = new File("test.gif")
FileOutputStream out = new FileOutputStream(file)
out.write(pic.getContent())
out.close()
}
我看到了下面的接口方法:
在POI api中,`PicturesManager接口方法savePicture
存储映像(可能在磁盘上)。请注意,不同的输出
格式支持不同的文件类型,因此可能需要进行图像转换
必修的。例如,HTML浏览器通常支持PictureType.GIF,
PictureType.JPEG、PictureType.PNG、PictureType.TIFF,但很少
PictureType.EMF或PictureType.WMF。FO(Apache FOP)至少支持
PNG和SVG类型
任何人都已经实现了这一点或任何在浏览器中显示此图像的想法。
非常感谢。我有xls文档的示例,我的代码:
HSSFPatriarch patriarch = sheet.getDrawingPatriarch();
if (patriarch != null) {
// Loop through the objects
for (HSSFShape shape : patriarch.getChildren()) {
if (shape instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture) shape;
if (picture.getShapeType() == HSSFSimpleShape.OBJECT_TYPE_PICTURE) {
if (picture.getImageDimension() != null) {
HSSFPictureData pictureData = picture.getPictureData();
byte[] data = pictureData.getData();
String fileName = PATH + picture.getFileName() + "." + pictureData.suggestFileExtension();
File file = new File(fileName);
try (FileOutputStream fop = new FileOutputStream(file)) {
if (!file.exists()) {
file.createNewFile();
}
fop.write(data);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
在此示例中,服务器上文件夹的路径是可变的,例如win的“c:/temp/img/”或nix的“www/images/”
如果需要,可以将pictureData.suggestFileExtension()检查为等于“png”、“jpg”等
运行这样的代码后,您可以直接在html页面中使用文件名,或者,如果您使用代理,则可能需要执行一些附加操作以获取保存图片上的链接 将其另存为服务器中的图像,然后为其提供服务。如果确实需要图像类型,可以使用类似ImageMagick的功能converted@Alfabravo-到目前为止,我正在尝试用代码保存图像。但嵌入的图像是visio或其他,即使我以这种方式保存,它也不会在浏览器中打开。只嵌入一个gif,那么只有它在工作。步骤1:你检查过你保存的是图像吗?我的意思是,导航到文件夹并打开图像。