doc在线预览的效果,doc在线预览效果,通过apache的poi
分享于 点击 31768 次 点评:257
doc在线预览的效果,doc在线预览效果,通过apache的poi
通过apache的poi转换成html,类似于qq邮箱的在线预览.
代码下载地址: http://www.javaniu.com/explore/27.htm
package com.javaniu.servlet;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.net.URLEncoder;import java.util.List;import javax.servlet.http.HttpServletResponse;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.FactoryConfigurationError;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;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.Picture;import org.apache.poi.hwpf.usermodel.PictureType;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.commons.CommonsMultipartFile;import org.springframework.web.servlet.ModelAndView;import org.w3c.dom.Document;@Controller@RequestMapping({ "/demo" })public class DemoController { private static final String path = "/data/www/demo.javaniu.com/doc2html/"; @RequestMapping(value = { "doc2html" }, method = { RequestMethod.GET }) public ModelAndView doc2html( @RequestParam(value = "file", required = true) final String file, HttpServletResponse response) throws Exception, FactoryConfigurationError { ModelAndView modelAndView = new ModelAndView("doc2html/doc2html"); File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } InputStream input = new FileInputStream(path + file); HWPFDocument wordDocument = new HWPFDocument(input); WordToHtmlConverter wordToHtmlConverter; wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory .newInstance().newDocumentBuilder().newDocument()); wordToHtmlConverter.setPicturesManager(new PicturesManager() { public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) { return "http://demo.javaniu.com/doc2html/" + file + "_" + suggestedName; } });...//该片段来自于http://byrx.net
用户点评