读取doc和下载文件

我直接贴代码了。不懂的可以留言或私信

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);


    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值