Java实现PDF在线预览功能(四种方式)

时间:2022-11-29 07:48:48

Java实现PDF在线预览

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@RequestMapping("/preview1")
public void er(HttpServletResponse response){
    File file = new File("G:\\桌面\\Thymeleaf3.0中文翻译文档@www.java1234.com.pdf");
    if (file.exists()){
        byte[] data = null;
        try {
            FileInputStream input = new FileInputStream(file);
            data = new byte[input.available()];
            input.read(data);
            response.getOutputStream().write(data);
            input.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }else{
        return;
    }
}
@ResponseBody
@RequestMapping("/preview2")
public void findPdf( HttpServletResponse response) throws IOException{
    response.setContentType("application/pdf");
    FileInputStream in = new FileInputStream(new File("G:\\桌面\\Thymeleaf3.0中文翻译文档@www.java1234.com.pdf"));
    OutputStream out = response.getOutputStream();
    byte[] b = new byte[512];
    while ((in.read(b))!=-1) {
        out.write(b);
    }
    out.flush();
    in.close();
    out.close();
}
@ResponseBody
@RequestMapping("/preview3")
public void devDoc(HttpServletRequest request, HttpServletResponse response, String storeName) throws Exception {
    request.setCharacterEncoding("UTF-8");
    String ctxPath = request.getSession().getServletContext().getRealPath("");
    String downLoadPath = "G:\\桌面\\Thymeleaf3.0中文翻译文档@www.java1234.com.pdf";
    response.setContentType("application/pdf");
    FileInputStream in = new FileInputStream(new File(downLoadPath));
    OutputStream out = response.getOutputStream();
    byte[] b = new byte[1024];
    while ((in.read(b))!=-1) {
        out.write(b);
    }
    out.flush();
    in.close();
    out.close();
}
@ResponseBody
@RequestMapping("/preview")
public void download( HttpServletResponse response
                    ) throws IOException {
    String filePath = "G:\\桌面\\Thymeleaf3.0中文翻译文档@www.java1234.com.pdf";
    System.out.println("filePath:" + filePath);
    File f = new File(filePath);
    if (!f.exists()) {
        response.sendError(404, "File not found!");
        return;
    }
    BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
    byte[] bs = new byte[1024];
    int len = 0;
    response.reset(); // 非常重要
    if (true) { // 在线打开方式
        URL u = new URL("file:///" + filePath);
        String contentType = u.openConnection().getContentType();
        response.setContentType(contentType);
        response.setHeader("Content-Disposition", "inline;filename="
                + "2019年上半年英语四级笔试准考证(戴林峰).pdf");
        // 文件名应该编码成utf-8,注意:使用时,我们可忽略这句
    } else {
        // 纯下载方式
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment;filename="
                + "2019年上半年英语四级笔试准考证(戴林峰).pdf");
    }
    OutputStream out = response.getOutputStream();
    while ((len = br.read(bs)) > 0) {
        out.write(bs, 0, len);
    }
    out.flush();
    out.close();
    br.close();
}

Java快捷实现PDF在线预览

本文章利用各个浏览器的内核支持PDF预览功能来做,PDF的预览网上说的最多的是PDF.JS实现预览,也找了好多文章,死活最后实现不了,通过这个方法解决了,如果有PDF.js解决的可以大家沟通交流下。

这里提一下,其他word和excel可以通过aspose代码转换成PDF,也很快有效。可以网上搜下。

方法很简单,首先引入的都是IO的包。

?
1
2
3
4
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;

接下来就是在自己的后台controller里面写个方法。里面file的文件可以通过参数或是封装方式获取,这个里面的流不需要关闭,浏览器会自动获取然后展示。关闭的话浏览器就无法加载!

?
1
2
3
4
5
6
7
8
9
10
11
12
@RequestMapping(value = "/showpdf")
    public void showpdf(HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            File file = new File("D:/xyptFile/java.pdf");
            FileInputStream fileInputStream = new FileInputStream(file);
            response.setHeader("Content-Type", "application/pdf");
            OutputStream outputStream = response.getOutputStream();
            IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

文章来源:从其他各个文章中测试验证,取得最优方案。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_39992641/article/details/96881552