当前位置: 首页 > news >正文

鲜花商城网站建设西安房产网站大全

鲜花商城网站建设,西安房产网站大全,wordpress教程文档,无为网站定制前言#xff1a;在很多时候我们都需要到项目中导入word文档#xff0c;但是后期再次在前段显示这个文档的时候前端往往需要的是html格式的#xff0c;所以这个时候就会提出一个需求#xff1a; 你们存文档的时候能不能存成html格式的#xff1f; 于是这篇文章的内容就可以…前言在很多时候我们都需要到项目中导入word文档但是后期再次在前段显示这个文档的时候前端往往需要的是html格式的所以这个时候就会提出一个需求 你们存文档的时候能不能存成html格式的  于是这篇文章的内容就可以满足这个需求我是通过MultiPartFile 类来实现的上代码一、首先导入需要的依赖包org.apache.poipoi-scratchpad3.17org.apache.poipoi-ooxml3.17fr.opensagres.xdocreportfr.opensagres.xdocreport.converter.docx.xwpf2.0.1二、编写代码package com.lmt.service.file;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.util.UUID;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.apache.poi.hwpf.HWPFDocument;import org.apache.poi.hwpf.converter.PicturesManager;import org.apache.poi.hwpf.converter.WordToHtmlConverter;import org.apache.poi.hwpf.usermodel.PictureType;import org.apache.poi.util.IOUtils;import org.apache.poi.xwpf.usermodel.XWPFDocument;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.multipart.MultipartFile;import org.w3c.dom.Document;import fr.opensagres.poi.xwpf.converter.core.ImageManager;import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;Componentpublic class WordToHtml {private static final Logger logger LoggerFactory.getLogger(WordToHtml.class);//转换的方法public File convert(MultipartFile file) {//获得文件的名字String filename file.getOriginalFilename();//获得文件的扩展名String suffixfilename.substring(filename.lastIndexOf(.));String newNameUUID.randomUUID().toString();// TODO 需要保存在一个新的位置//将文件保存在D:/test/文件下File convFile new File(D:/test/ newName suffix);FileOutputStream fos null;try {//创建文件convFile.createNewFile();fos new FileOutputStream(convFile);fos.write(file.getBytes());} catch (IOException ex) {logger.error(上传文件出错, ex);return null;} finally {IOUtils.closeQuietly(fos);}// 输入文件名的所在文件夹// 加上反斜杠String parentDirectory convFile.getParent();if (!parentDirectory.endsWith(\\)) {parentDirectory parentDirectory \\;}if (filename.endsWith(.docx)) {return docxConvert(parentDirectory, convFile.getAbsolutePath(),newName);} else if (filename.endsWith(.doc)) {return docConvert(parentDirectory, convFile.getAbsolutePath(),newName);} else {logger.error(不支持的文件格式);return null;}}private File docxConvert(String parentDirectory, String filename,String newName) {try {XWPFDocument document new XWPFDocument(new FileInputStream(filename));XHTMLOptions options XHTMLOptions.create().setImageManager(new ImageManager(new File(parentDirectory), UUID.randomUUID().toString())).indent(4);FileOutputStream out new FileOutputStream(new File(parentDirectory newName .html));XHTMLConverter.getInstance().convert(document, out, options);return new File(parentDirectory newName .html);} catch (IOException ex) {logger.error(word转化出错, ex);return null;}}private File docConvert(String parentDirectory, String filename,String newName) {try {HWPFDocument document new HWPFDocument(new FileInputStream(filename));WordToHtmlConverter wordToHtmlConverter new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());// converter默认对图片不作处理需要手动下载图片并嵌入到html中wordToHtmlConverter.setPicturesManager(new PicturesManager() {Overridepublic String savePicture(byte[] bytes, PictureType pictureType, String s, float v, float v1) {String imageFilename parentDirectory ;String identityUUID.randomUUID().toString();File imageFile new File(imageFilename, identitys);imageFile.getParentFile().mkdirs();InputStream in null;FileOutputStream out null;try {in new ByteArrayInputStream(bytes);out new FileOutputStream(imageFile);IOUtils.copy(in, out);} catch (IOException ex) {logger.error(word转化出错, ex);} finally {if (in ! null) {IOUtils.closeQuietly(in);}if (out ! null) {IOUtils.closeQuietly(out);}}return imageFile.getName();}});wordToHtmlConverter.processDocument(document);Document htmlDocument wordToHtmlConverter.getDocument();ByteArrayOutputStream out new ByteArrayOutputStream();DOMSource domSource new DOMSource(htmlDocument);StreamResult streamResult new StreamResult(out);TransformerFactory tf TransformerFactory.newInstance();Transformer serializer tf.newTransformer();serializer.setOutputProperty(OutputKeys.ENCODING, UTF-8);serializer.setOutputProperty(OutputKeys.INDENT, yes);serializer.setOutputProperty(OutputKeys.METHOD, html);serializer.transform(domSource, streamResult);out.close();String result new String(out.toByteArray());FileWriter writer new FileWriter(parentDirectory newName .html);writer.write(result);writer.close();} catch (IOException | TransformerException | ParserConfigurationException ex) {logger.error(word转化出错, ex);}return new File(parentDirectory newName .html);}/*** 将上传的Word文档转化成HTML字符串* param attachfile* return*/public String convertToHtml(MultipartFile attachfile) {String wordContent ;// 将Word文件转换为htmlFile file convert(attachfile);// 读取html文件if (file ! null) {return 文件转换成功}return 文件转换失败;}代码的含义已经在代码行的注释上有了哪里有问题欢迎大家随时在评论下方留言
http://www.pierceye.com/news/766625/

相关文章:

  • 没有相应营业执照怎么做网站重庆网站设计公司排名
  • 企业网站手机版商城网站有什么好处
  • 推荐一本学做网站的书温州阀门网站建设
  • 用户要承担暖气费的税吗太原优化排名推广
  • Wordpress外贸网站搭建公司宿迁建设网站
  • 学校网站建设的意义和应用山东淄博网站建设
  • 莱芜网站建设价格低网站vps
  • 长治做网站哪家好赣州注册公司
  • 网站开发从入门到精通做h5的网站哪个好
  • 免费公司网站如何建立设计个人网站好备案吗
  • 建网站和做微信哪个好在线识别图片百度识图
  • php网站开发如何实现删除功能大连大连建设工程信息网站
  • 表格模板免费下载网站wordpress 插件位置
  • wordpress小白能学会吗汕头做网站优化公司
  • 军队营房基础建设网站重庆做网站个人
  • 网站建设怎样中英文网站备案是空间备案还是域名备案
  • 陕西网站制作人力资源服务外包
  • 成都网站建设哪家售后好网站建设费可以计业务费吗
  • 做服装到哪个网站拿货品质好自己制作的网页别人如何访问
  • 榆林哪里做网站网页游戏网站那个好
  • 泰安口碑好的企业建站公司wordpress验证码无效
  • 圣矢网络重庆网站建设优化推广公司好听好记的网站域名
  • 如何做旅游小视频网站比较好的外贸公司
  • 图书馆建设投稿网站使用 ahrefs 进行 seo 分析
  • 校园网站建设 德育免费换ip软件
  • 排行网站模板凡科代理千万不要做
  • 贵州省冶金建设有限公司网站网站好玩新功能
  • 怎么让客户做网站惠州关键词排名提升
  • 创建公司网站需要什么国外的智慧城市建设网站
  • 阿里云服务器做网站django高清无版权网站