我直接贴代码了。不懂的可以留言或私信
package com.nsight.controller;
import com.nsight.util.DocToHtml;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
@Controller
public class DocToHtmlController {
@RequestMapping("/doc/docToHtml")
public String docToHtml(String id) throws Exception {
// System.out.println("开始转换,现在是Controller层---");
DocToHtml.`docToHtml`();
return "/doc.jsp";
}
@RequestMapping(value = "/doc/docDownload",method = RequestMethod.POST)
public ResponseEntity<byte[]> docDownload(@RequestParam("fileName") String fileName) throws Exception {
//下载文件的路径(这里绝对路径)
String filepath= "/usr/local/tomcat/files/"+fileName;
File file =new File(filepath);
//创建字节输入流,这里不用Buffer类
InputStream in = new FileInputStream(file);
//available:获取输入流所读取的文件的最大字节数
byte[] body = new byte[in.available()];
//把字节读取到数组中
in.read(body);
//设置请求头
MultiValueMap<String, String> headers = new HttpHeaders();
// headers.add("Content-Disposition", "attchement;filename=" + file.getName());
headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
//设置响应状态
HttpStatus statusCode = HttpStatus.OK;
in.close();
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
return entity;//返回
}
@RequestMapping(value = "/doc/CdocDownload" )
public ResponseEntity<byte[]> CdocDownload() throws Exception {
//下载文件的路径(这里绝对路径)
String filepath= "/usr/local/tomcat/files/qcvalueaddproapi.7z";
File file =new File(filepath);
//创建字节输入流,这里不实用Buffer类
InputStream in = new FileInputStream(file);
//available:获取输入流所读取的文件的最大字节数
byte[] body = new byte[in.available()];
//把字节读取到数组中
in.read(body);
//设置请求头
MultiValueMap<String, String> headers = new HttpHeaders();
// headers.add("Content-Disposition", "attchement;filename=" + file.getName());
headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
//设置响应状态
HttpStatus statusCode = HttpStatus.OK;
in.close();
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
return entity;//返回
}
@RequestMapping(value = "/doc/JavaDocDownload" )
public ResponseEntity<byte[]> JavaDocDownload() throws Exception {
//下载文件的路径(这里绝对路径)
String filepath= "/usr/local/tomcat/files/Java版本接口说明文档.doc";
File file =new File(filepath);
//创建字节输入流,这里不实用Buffer类
InputStream in = new FileInputStream(file);
//available:获取输入流所读取的文件的最大字节数
byte[] body = new byte[in.available()];
//把字节读取到数组中
in.read(body);
//设置请求头
MultiValueMap<String, String> headers = new HttpHeaders();
// headers.add("Content-Disposition", "attchement;filename=" + file.getName());
headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
//设置响应状态
HttpStatus statusCode = HttpStatus.OK;
in.close();
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
return entity;//返回
}
}
package com.nsight.util;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
public class DocToHtml {
public DocToHtml() {
}
public static void main(String[] args) throws Exception {
// (new DocToHtml()).docxToHtml();
(new DocToHtml()).docToHtml();
// getWordTitles("");
}
public static void docToHtml() throws Exception {
// String sourceFileName = "src/main/webapp/Java版本接口说明文档.doc";
String targetFileName = "/usr/local/tomcat/webapps/valueAdd/doc.jsp";
// String imagePathStr = "D:/doc2htmltest/image/";
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream("/usr/local/tomcat/files/Java版本接口说明文档.doc"));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.setPicturesManager((a, b, suggestedName, d, e) -> {
return "image" + File.separator + suggestedName;
});
wordToHtmlConverter.processDocument(wordDocument);
List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
Iterator var8 = pics.iterator();
/* while(var8.hasNext()) {
Picture pic = (Picture)var8.next();
pic.writeImageContent(new FileOutputStream(imagePathStr + pic.suggestFullFileName()));
}*/
Document htmlDocument = wordToHtmlConverter.getDocument();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(new File(targetFileName));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty("encoding", "utf-8");
serializer.setOutputProperty("indent", "yes");
serializer.setOutputProperty("method", "html");
serializer.transform(domSource, streamResult);
System.out.println("doc转换完毕!");
//给jsp加个head,解决乱码问题
HtmlTransJspUtils.writeFile(targetFileName);
}
}