源码:
XWPFDocument document = new XWPFDocument( new FileInputStream(“D:\crm.docx”) );
XHTMLOptions options = XHTMLOptions.create().indent( 4 );
// Extract image
List<XWPFPictureData> pics=document.getAllPictures();
for (XWPFPictureData pic : pics) {
System.out.println(pic.getPictureType() + file.separator + pic.suggestFileExtension()
+file.separator+pic.getFileName());
byte[] bytev = pic.getData();
FileOutputStream fos = new FileOutputStream(“d:\”+pic.getFileName());
fos.write(bytev);
}
File imageFolder = new File( “D:/vfsroot/1000000/ueditor_upload/images” + fileInName );
options.setExtractor( new FileImageExtractor( imageFolder ) );
// URI resolver
options.URIResolver( new FileURIResolver( imageFolder ) );
File outFile = new File( fileOutName );
outFile.getParentFile().mkdirs();
OutputStream out = new FileOutputStream( outFile );
XHTMLConverter.getInstance().convert( document, out, options );
通过 for (XWPFPictureData pic : pics) {
System.out.println(pic.getPictureType() + file.separator + pic.suggestFileExtension()
+file.separator+pic.getFileName());
byte[] bytev = pic.getData();
FileOutputStream fos = new FileOutputStream(“d:\”+pic.getFileName());
fos.write(bytev);
InputStream is = null; try { is = new FileInputStream(src); XWPFDocument document = new XWPFDocument(is); XHTMLOptions options = XHTMLOptions.create().indent(4); //img的src属性 后面会自动添加/word/media //这里就是images/word/media/ + 图片名字 options.URIResolver(new BasicURIResolver("images")); //>> 文件的保存路径 之后自动会添加 word\media子路径 FileImageExtractor extractor = new FileImageExtractor(new File( htmlImagesPath)); options.setExtractor(extractor); XHTMLConverter.getInstance().convert(document, new FileOutputStream(des), options); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
不需要手动添加代码保存图片