文章目錄
一、官網(wǎng)下載openOffice 安裝包,運(yùn)行安裝(不同系統(tǒng)的安裝請自行百度,這里不做過多描述)
二、pom中引入依賴
三、office文件轉(zhuǎn)為pdf流的工具類
四、service層代碼?
五、controller層代碼
office辦公文檔,如doc、docx、xls、xlsx、ppt、pptx是無法直接在瀏覽器中打開的,但很多OA辦公軟件都要求office文檔能直接在線預(yù)覽功能,解決方法如下:
1、office文檔轉(zhuǎn)為html,使用POI將文檔轉(zhuǎn)為html文件,直接瀏覽器打開預(yù)覽
優(yōu)點:簡單,方便不需要安裝其他插件
缺點:對拓展名為docx、xlsx、pptx格式文檔,最終轉(zhuǎn)換輸出的格式樣式會出錯,影響客戶閱讀,對于客戶需求度不高的可以使用該方法處理
2、office文檔轉(zhuǎn)為pdf,使用POI和fr.opensagres.xdocreport將文檔轉(zhuǎn)為pdf文件,讓瀏覽器內(nèi)置pdf閱讀器瀏覽
優(yōu)點:簡單,方便不需要安裝其他插件
缺點:doc、xls、ppt輸出格式問題不是很大,docx、xlsx、pptx格式文檔輸出樣式錯誤,并且會出現(xiàn)文字丟失等情況,影響客戶閱讀
??fr.opensagres.xdocreport 依賴地址:
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
3、office文檔轉(zhuǎn)為pdf,使用Apache提供的openOffice將文件轉(zhuǎn)為pdf文件;保證文檔格式、文件轉(zhuǎn)換輸出穩(wěn)定,滿足在線預(yù)覽條件。【推薦】
優(yōu)點:免費(fèi),完美解決轉(zhuǎn)換格式出錯問題
缺點:需要下載安裝第三方工具openOffice
本地電腦如果裝了Adobe Reader XI,那把pdf直接拖到瀏覽器頁面就可以直接打開預(yù)覽,前提就是瀏覽器要支持pdf文件瀏覽。
這篇博客主要介紹第三種方法,通過poi實現(xiàn)word、excel、ppt轉(zhuǎn)pdf流,這樣就可以在瀏覽器上實現(xiàn)預(yù)覽了。
一、官網(wǎng)下載openOffice 安裝包,運(yùn)行安裝(不同系統(tǒng)的安裝請自行百度,這里不做過多描述)
去官網(wǎng)下載:點擊去官網(wǎng)下載
文章來源:http://www.zghlxwxcb.cn/news/detail-512875.html
?二、pom中引入依賴
<!-- openoffice -->
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
?三、office文件轉(zhuǎn)為pdf流的工具類
import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
/**
* 文件格式轉(zhuǎn)換工具類
*/
public class FileConvertUtil {
/**
* 默認(rèn)轉(zhuǎn)換后文件后綴
*/
private static final String DEFAULT_SUFFIX = "pdf";
/**
* 端口
*/
private static final Integer OPENOFFICE_PORT = 8100;
/**
* office文檔轉(zhuǎn)換為PDF(處理本地文件)
*
* @param sourcePath 源文件路徑
* @param suffix 源文件后綴
* @return InputStream 轉(zhuǎn)換后文件輸入流
*/
public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception {
File inputFile = new File(sourcePath);
InputStream inputStream = Files.newInputStream(inputFile.toPath());
return covertCommonByStream(inputStream, suffix);
}
/**
* office文檔轉(zhuǎn)換為PDF(處理網(wǎng)絡(luò)文件)
*
* @param netFileUrl 網(wǎng)絡(luò)文件路徑
* @param suffix 文件后綴
* @return InputStream 轉(zhuǎn)換后文件輸入流
*/
public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception {
// 創(chuàng)建URL
URL url = new URL(netFileUrl);
// 試圖連接并取得返回狀態(tài)碼
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
int httpResult = httpUrlConnection.getResponseCode();
if (httpResult == HttpURLConnection.HTTP_OK) {
InputStream inputStream = urlConnection.getInputStream();
return covertCommonByStream(inputStream, suffix);
}
return null;
}
/**
* 將文件以流的形式轉(zhuǎn)換
*
* @param inputStream 源文件輸入流
* @param suffix 源文件后綴
* @return InputStream 轉(zhuǎn)換后文件輸入流
*/
public static InputStream covertCommonByStream(InputStream inputStream, String suffix) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_PORT);
connection.connect();
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
DocumentFormat targetFormat = formatReg.getFormatByFileExtension(DEFAULT_SUFFIX);
DocumentFormat sourceFormat = formatReg.getFormatByFileExtension(suffix);
converter.convert(inputStream, sourceFormat, out, targetFormat);
connection.disconnect();
return outputStreamConvertInputStream(out);
}
/**
* outputStream轉(zhuǎn)inputStream
*/
public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception {
ByteArrayOutputStream outputStream = (ByteArrayOutputStream) out;
return new ByteArrayInputStream(outputStream.toByteArray());
}
}
四、service層代碼?
@Override
public void onlinePreview(String url, HttpServletResponse response) {
// 獲取文件類型
String[] str = url.split("\\.");
if (str.length == 0) {
throw new RuntimeException("文件格式不正確");
}
String suffix = str[str.length - 1];
if (!"txt".equals(suffix) && !"doc".equals(suffix) && !"docx".equals(suffix) && !"xls".equals(suffix)
&& !"xlsx".equals(suffix) && !"ppt".equals(suffix) && !"pptx".equals(suffix)) {
throw new RuntimeException("文件格式不支持預(yù)覽");
}
try {
InputStream in = FileConvertUtil.convertNetFile(url, suffix);
OutputStream outputStream = response.getOutputStream();
// 創(chuàng)建存放文件內(nèi)容的數(shù)組
byte[] buff = new byte[1024];
// 所讀取的內(nèi)容使用n來接收
int n;
// 當(dāng)沒有讀取完時,繼續(xù)讀取,循環(huán)
while ((n = in.read(buff)) != -1) {
// 將字節(jié)數(shù)組的數(shù)據(jù)全部寫入到輸出流中
outputStream.write(buff, 0, n);
}
// 強(qiáng)制將緩存區(qū)的數(shù)據(jù)進(jìn)行輸出
outputStream.flush();
// 關(guān)閉流
outputStream.close();
in.close();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
五、controller層代碼
@PostMapping("/file/onlinePreview")
public void onlinePreview(@RequestParam("url") String url, HttpServletResponse response) throws Exception{
fileService.onlinePreview(url,response);
}
如果這篇文章對您有所幫助,或者有所啟發(fā)的話,求一鍵三連:點贊、評論、收藏?關(guān)注,您的支持是我堅持寫作最大的動力。?文章來源地址http://www.zghlxwxcb.cn/news/detail-512875.html
到了這里,關(guān)于Java實現(xiàn)office辦公文檔在線預(yù)覽(word、excel、ppt、txt等)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!