张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.thhy.filectrl.utils;
 
import org.apache.commons.io.IOUtils;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
 
public class ConvertUtils {
 
    public void conversionFile(HttpServletResponse response, String fileStoragePath, String beforeConversion) throws Exception {
        //文件存储路径
        //fileStoragePath ="/app/";
        //转换前的文件名
        //beforeConversion ="20191009133209lis_chgrpt.docx";
        String fileNamePath = fileStoragePath + beforeConversion;
        File file = new File(fileNamePath);
        if (!file.exists()) {
            System.err.println("库存中没有指定文件。。。。");
            return;
        }
        //获取到文件名
        String interceptFileName = beforeConversion.substring(0, beforeConversion.lastIndexOf("."));
        //截取文件后缀名
        String fileNameSuffix = beforeConversion.substring(beforeConversion.lastIndexOf(".") + 1);
        String command = null;
        if ("pdf".equals(fileNameSuffix)) {
            /**
             * 在线预览方法
             */
            openPdf(response, fileStoragePath + interceptFileName + ".pdf");
        } else if ("doc".equals(fileNameSuffix) || "docx".equals(fileNameSuffix) || "xls".equals(fileNameSuffix) || "xlsx".equals(fileNameSuffix)
                || "ppt".equals(fileNameSuffix) || "pptx".equals(fileNameSuffix)) {
            //文件格式转换命令 unoconv插件实现
            command = "/opt/libreoffice6.3/program/soffice  --headless --invisible --convert-to pdf " + fileNamePath;
            //格式转换+在线预览
            formatConverAndPreview(command, response, fileStoragePath, interceptFileName);
        } else {
            System.err.println("暂不支持该类型文件在线预览!!!");
            return;
        }
    }
 
    /**
     * 格式转换+在线预览 方法
     *
     * @param command           文件格式转换命令         例:/usr/bin/unoconv -f pdf  /app/1.pptx
     * @param response          http响应网页,实现在线预览
     * @param fileStoragePath   准备文件存放路径         例:/app/
     * @param interceptFileName 文件名                  例: 1.pptx
     * @throws Exception
     */
    public void formatConverAndPreview(String command,
                                       HttpServletResponse response,
                                       String fileStoragePath,
                                       String interceptFileName) throws Exception {
        /**
         * 格式转换方法
         */
        //String temp ="/usr/bin/unoconv -f pdf " + command;
        executeCommand(command);
        /**
         * 在线预览方法
         */
        openPdf(response, fileStoragePath + interceptFileName + ".pdf");
    }
 
    /**
     * 在线预览方法
     * 把转换后的pdf文件在网页上进行预览
     *
     * @param response    http响应
     * @param previewFile 文件的決定路径  例:/app/20191009133209_chgrpt.pdf
     * @throws Exception 格式转换过程中的异常
     */
    private static void openPdf(HttpServletResponse response, String previewFile) throws Exception {
        InputStream inputStream = null;
        OutputStream outputStream = null;
 
        //String path ="/app/20191009133209_chgrpt.pdf";
        inputStream = new FileInputStream(previewFile);
        //响应文件的类型
        response.setContentType("application/pdf");
        outputStream = response.getOutputStream();
        int a = 0;
        byte[] b = new byte[1024];
        while ((a = inputStream.read(b)) != -1) {
            outputStream.write(b, 0, a);
        }
        if (outputStream != null) {
            outputStream.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }
 
    /**
     * 格式转换方法
     * <p>
     * 統一把文件转换成pdf文件
     *
     * @param command 文件格式转换命令   例:/usr/bin/unoconv -f pdf  /app/1.pptx
     * @throws Exception 格式转换过程中的异常
     */
    private static void executeCommand(String command) throws Exception {
 
        StringBuffer output = new StringBuffer();
        Process process;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        try {
            process = Runtime.getRuntime().exec(command);
            process.waitFor();
            inputStreamReader = new InputStreamReader(process.getInputStream(), "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }
            //p.destroy();//这个一般不需要
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(inputStreamReader);
        }
    }
 
 
}