欢迎来到天天文库
浏览记录
ID:6052073
大小:49.50 KB
页数:5页
时间:2018-01-01
《解析docx以及doc格式的word文档中的图片》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、上海傲蓝科技用Java从Doc和Docx格式的Word文档中提取图片Doc和Docx的文档格式因为存储格式不一样,所以需要用不通的方法提取图片。Docx格式的文档实际上是压缩文件,用程序提取图片相对实际上只需要对文章进行解压缩,然后到指定文件夹找到图片即可。Doc文档则需要把图片数据读出来,再生成文件。下面代码均可直接运行。从Docx文件中提取图片的代码:packagecom.onlan.docx;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importja
2、va.io.FileOutputStream;importjava.util.Enumeration;importjava.util.zip.ZipEntry;importjava.util.zip.ZipFile;publicclassImgExt4Docx{/***@paramargs*/staticfinalintBUFFER=2048;publicstaticvoidmain(String[]args){StringinputFilename="D:/work/test.docx";StringunZipPathname="D:/work/unZipDir/";ImgExt4Do
3、cxied=newImgExt4Docx();System.out.println(ied.unZipDocx(inputFilename,unZipPathname));}/**returnthefoldoftheimages*/publicStringunZipDocx(Stringdocxfile,StringdestDir)5上海傲蓝科技{try{StringinputFilename=docxfile;StringunZipPathname=destDir;ZipFilezipFile=newZipFile(inputFilename);Enumerationenu=zipFi
4、le.entries();inti=0;while(enu.hasMoreElements()){ZipEntryzipEntry=(ZipEntry)enu.nextElement();if(zipEntry.isDirectory()){newFile(unZipPathname+zipEntry.getName()).mkdirs();continue;}BufferedInputStreambis=newBufferedInputStream(zipFile.getInputStream(zipEntry));Filefile=newFile(unZipPathname+zipE
5、ntry.getName());Fileparent=file.getParentFile();if(parent!=null&&!parent.exists()){parent.mkdirs();}FileOutputStreamfos=newFileOutputStream(file);BufferedOutputStreambos=newBufferedOutputStream(fos,BUFFER);intcount;byte[]array=newbyte[BUFFER];while((count=bis.read(array,0,BUFFER))!=-1){bos.write(
6、array,0,BUFFER);}bos.flush();bos.close();bis.close();}returndestDir+"word/media";}catch(Exceptione){e.printStackTrace();returnnull;}}5上海傲蓝科技}从Doc文件中提取图片的代码,该代码不仅可以从文档中提取图片,还可以把Doc文档中图片所在位置加上标签以及图片文件名,与提取的图片对应起来:packagecom.onlan;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOu
7、tputStream;importjava.io.InputStream;importjava.io.OutputStream;importorg.apache.poi.hwpf.HWPFDocument;importorg.apache.poi.hwpf.model.PicturesTable;importorg.apache.poi.hwpf.usermodel.CharacterRun;importorg.apache.poi
此文档下载收益归作者所有