java处理docx文件中的图片

public static void main(String[] args) throws Exception {
        InputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //目标文件地址
            File file = new File("d:\\Users\\jingege\\Desktop\\11.docx");
            fileInputStream = new FileInputStream(file);
            XWPFDocument document = new XWPFDocument(fileInputStream);
            List<XWPFPictureData> picList = document.getAllPictures();
            for (XWPFPictureData pic : picList) {
                System.out.println(pic.getFileName());
                byte[] bytev = pic.getData();
                //把图片存档到指定文件夹下
                fileOutputStream = new FileOutputStream("d:\\Users\\jingege\\Desktop\\22\\" + pic.getFileName());
                fileOutputStream.write(bytev);
                fileOutputStream.flush();
            }
        } finally {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
    }

注意

该方法只能处理docx结尾的文件,doc文件暂时无法处理

你可能感兴趣的:(JAVA知识,java,开发语言,html)