国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Java實現在線預覽的示例代碼(openOffice實現)

Java實現在線預覽的示例代碼(openOffice實現)

2021-02-23 11:24yjclsx JAVA教程

本篇文章主要介紹了Java實現在線預覽的示例代碼(openOffice實現),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

簡介

之前有寫了poi實現在線預覽的文章,里面也說到了使用openOffice也可以做到,這里就詳細介紹一下。

我的實現邏輯有兩種:

一、利用jodconverter(基于OpenOffice服務)將文件(.doc、.docx、.xls、.ppt)轉化為html格式。

二、利用jodconverter(基于OpenOffice服務)將文件(.doc、.docx、.xls、.ppt)轉化為pdf格式。

轉換成html格式大家都能理解,這樣就可以直接在瀏覽器上查看了,也就實現了在線預覽的功能;轉換成pdf格式這點,需要用戶安裝了Adobe Reader XI,這樣你會發現把pdf直接拖到瀏覽器頁面可以直接打開預覽,這樣也就實現了在線預覽的功能。

將文件轉化為html格式或者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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.pdfPreview.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/**
 * 利用jodconverter(基于OpenOffice服務)將文件(*.doc、*.docx、*.xls、*.ppt)轉化為html格式或者pdf格式,
 * 使用前請檢查OpenOffice服務是否已經開啟, OpenOffice進程名稱:soffice.exe | soffice.bin
 *
 * @author yjclsx
 */
public class Doc2HtmlUtil {
 
  private static Doc2HtmlUtil doc2HtmlUtil;
 
  /**
   * 獲取Doc2HtmlUtil實例
   */
  public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
    if (doc2HtmlUtil == null) {
      doc2HtmlUtil = new Doc2HtmlUtil();
    }
    return doc2HtmlUtil;
  }
 
  /**
   * 轉換文件成html
   *
   * @param fromFileInputStream:
   * @throws IOException
   */
  public String file2Html(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String timesuffix = sdf.format(date);
    String docFileName = null;
    String htmFileName = null;
    if("doc".equals(type)){
      docFileName = "doc_" + timesuffix + ".doc";
      htmFileName = "doc_" + timesuffix + ".html";
    }else if("docx".equals(type)){
      docFileName = "docx_" + timesuffix + ".docx";
      htmFileName = "docx_" + timesuffix + ".html";
    }else if("xls".equals(type)){
      docFileName = "xls_" + timesuffix + ".xls";
      htmFileName = "xls_" + timesuffix + ".html";
    }else if("ppt".equals(type)){
      docFileName = "ppt_" + timesuffix + ".ppt";
      htmFileName = "ppt_" + timesuffix + ".html";
    }else{
      return null;
    }
 
    File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
    File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
    if (htmlOutputFile.exists())
      htmlOutputFile.delete();
    htmlOutputFile.createNewFile();
    if (docInputFile.exists())
      docInputFile.delete();
    docInputFile.createNewFile();
    /**
     * 由fromFileInputStream構建輸入文件
     */
    try {
      OutputStream os = new FileOutputStream(docInputFile);
      int bytesRead = 0;
      byte[] buffer = new byte[1024 * 8];
      while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
      }
 
      os.close();
      fromFileInputStream.close();
    } catch (IOException e) {
    }
 
    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
    try {
      connection.connect();
    } catch (ConnectException e) {
      System.err.println("文件轉換出錯,請檢查OpenOffice服務是否啟動。");
    }
    // convert
    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    converter.convert(docInputFile, htmlOutputFile);
    connection.disconnect();
    // 轉換完之后刪除word文件
    docInputFile.delete();
    return htmFileName;
  }
 
  /**
   * 轉換文件成pdf
   *
   * @param fromFileInputStream:
   * @throws IOException
   */
  public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String timesuffix = sdf.format(date);
    String docFileName = null;
    String htmFileName = null;
    if("doc".equals(type)){
      docFileName = "doc_" + timesuffix + ".doc";
      htmFileName = "doc_" + timesuffix + ".pdf";
    }else if("docx".equals(type)){
      docFileName = "docx_" + timesuffix + ".docx";
      htmFileName = "docx_" + timesuffix + ".pdf";
    }else if("xls".equals(type)){
      docFileName = "xls_" + timesuffix + ".xls";
      htmFileName = "xls_" + timesuffix + ".pdf";
    }else if("ppt".equals(type)){
      docFileName = "ppt_" + timesuffix + ".ppt";
      htmFileName = "ppt_" + timesuffix + ".pdf";
    }else{
      return null;
    }
 
    File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
    File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
    if (htmlOutputFile.exists())
      htmlOutputFile.delete();
    htmlOutputFile.createNewFile();
    if (docInputFile.exists())
      docInputFile.delete();
    docInputFile.createNewFile();
    /**
     * 由fromFileInputStream構建輸入文件
     */
    try {
      OutputStream os = new FileOutputStream(docInputFile);
      int bytesRead = 0;
      byte[] buffer = new byte[1024 * 8];
      while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
      }
 
      os.close();
      fromFileInputStream.close();
    } catch (IOException e) {
    }
 
    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
    try {
      connection.connect();
    } catch (ConnectException e) {
      System.err.println("文件轉換出錯,請檢查OpenOffice服務是否啟動。");
    }
    // convert
    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    converter.convert(docInputFile, htmlOutputFile);
    connection.disconnect();
    // 轉換完之后刪除word文件
    docInputFile.delete();
    return htmFileName;
  }
 
  public static void main(String[] args) throws IOException {
    Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance();
    File file = null;
    FileInputStream fileInputStream = null;
 
    file = new File("D:/poi-test/exportExcel.xls");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/xls","xls");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/xls","xls");
 
    file = new File("D:/poi-test/test.doc");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/doc","doc");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/doc","doc");
 
    file = new File("D:/poi-test/周報模版.ppt");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
 
    file = new File("D:/poi-test/test.docx");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/docx","docx");
 
  }
 
}

轉換成html和轉換成pdf的過程幾乎一樣,只是在創建輸出的File時前者命名為XXX.html,后者命名為XXX.pdf,在執行converter.convert(docInputFile, htmlOutputFile);時,jodconverter會自己根據文件類型名轉換成對應的文件。

注意,main方法里別file2Html和file2pdf都調用,會報錯的,要么轉html,要么轉pdf,只能選一個。還有就是在執行之前,需要啟動openOffice的服務:在openOffice目錄下的命令窗口中執行soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard即可啟動。

以上需要引入jodconverter的jar包。希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/yjclsx/article/details/51445546

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美精品久久久久久久久老牛影院 | 欧美精品在线一区二区三区 | 男女免费观看在线爽爽爽视频 | 毛片黄片| 三级黄色片在线免费观看 | 超碰97人人干 | 亚洲国产高清在线 | 中国久久久 | 91精品蜜臀在线一区尤物 | baoyu123成人免费看视频 | 成人在线网站 | 99亚洲伊人久久精品影院 | 女教师高潮叫床视频在线观看 | 精品国产黄a∨片高清在线 天天色天天色 | 日本精a在线观看 | 国产午夜在线 | 国产在线一区二区三区 | av网站免费观看 | 91久久精品国产91久久 | av永久| 天堂资源在线 | 欧洲成人午夜免费大片 | 亚洲欧美一区二区三区国产精品 | 国产福利视频在线观看 | www久久久久 | 午夜视频网 | 大胆一区 | 可以看av的网站 | 超碰人人操 | 亚洲精品不卡 | 国产一区二区三区视频 | 精品在线看 | 国产精品亚洲综合 | 国产中文一区二区三区 | av中文字幕在线 | 黄色毛片在线看 | 亚洲国产高清在线 | 中文在线一区二区三区 | 久久国产精品无码网站 | jizz中国女人高潮 | 亚洲三级在线 |