Java實(shí)現(xiàn)辦公文檔在線預(yù)覽功能
java實(shí)現(xiàn)辦公文件在線預(yù)覽功能是一個(gè)大家在工作中也許會(huì)遇到的需求,網(wǎng)上些公司專門提供這樣的服務(wù),不過(guò)需要收費(fèi)
如果想要免費(fèi)的,可以用openoffice,實(shí)現(xiàn)原理就是:
通過(guò)第三方工具openoffice,將word、excel、ppt、txt等文件轉(zhuǎn)換為pdf文件流;
當(dāng)然如果裝了Adobe Reader XI,那把pdf直接拖到瀏覽器頁(yè)面就可以直接打開(kāi)預(yù)覽,前提就是瀏覽器支持pdf文件瀏覽。
我這里介紹通過(guò)poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)pdf流,這樣就可以在瀏覽器上實(shí)現(xiàn)預(yù)覽了。
1.到官網(wǎng)下載Apache OpenOffice 安裝包,安裝運(yùn)行。(不同系統(tǒng)的安裝方法,自行百度,這里不做過(guò)多說(shuō)明)

2.再項(xiàng)目的pom文件中引入依賴
<!--openoffice-->
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
3.將word、excel、ppt轉(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;
/**
* 文件格式轉(zhuǎn)換工具類
*
* @author tarzan
* @version 1.0
* @since JDK1.8
*/
public class FileConvertUtil {
/** 默認(rèn)轉(zhuǎn)換后文件后綴 */
private static final String DEFAULT_SUFFIX = "pdf";
/** openoffice_port */
private static final Integer OPENOFFICE_PORT = 8100;
/**
* 方法描述 office文檔轉(zhuǎn)換為PDF(處理本地文件)
*
* @param sourcePath 源文件路徑
* @param suffix 源文件后綴
* @return InputStream 轉(zhuǎn)換后文件輸入流
* @author tarzan
*/
public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception {
File inputFile = new File(sourcePath);
InputStream inputStream = new FileInputStream(inputFile);
return covertCommonByStream(inputStream, suffix);
}
/**
* 方法描述 office文檔轉(zhuǎn)換為PDF(處理網(wǎng)絡(luò)文件)
*
* @param netFileUrl 網(wǎng)絡(luò)文件路徑
* @param suffix 文件后綴
* @return InputStream 轉(zhuǎn)換后文件輸入流
* @author tarzan
*/
public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception {
// 創(chuàng)建URL
URL url = new URL(netFileUrl);
// 試圖連接并取得返回狀態(tài)碼
URLConnection urlconn = url.openConnection();
urlconn.connect();
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
int httpResult = httpconn.getResponseCode();
if (httpResult == HttpURLConnection.HTTP_OK) {
InputStream inputStream = urlconn.getInputStream();
return covertCommonByStream(inputStream, suffix);
}
return null;
}
/**
* 方法描述 將文件以流的形式轉(zhuǎn)換
*
* @param inputStream 源文件輸入流
* @param suffix 源文件后綴
* @return InputStream 轉(zhuǎn)換后文件輸入流
* @author tarzan
*/
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
*
* @author tarzan
*/
public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception {
ByteArrayOutputStream baos=(ByteArrayOutputStream) out;
return new ByteArrayInputStream(baos.toByteArray());
}
public static void main(String[] args) throws IOException {
//convertNetFile("http://172.16.10.21/files/home/upload/department/base/201912090541573c6abdf2394d4ae3b7049dcee456d4f7.doc", ".pdf");
//convert("c:/Users/admin/Desktop/2.pdf", "c:/Users/admin/Desktop/3.pdf");
}
}
4.serve層在線預(yù)覽方法代碼
/**
* @Description:系統(tǒng)文件在線預(yù)覽接口
* @Author: tarzan
*/
public void onlinePreview(String url, HttpServletResponse response) throws Exception {
//獲取文件類型
String[] str = SmartStringUtil.split(url,"\\.");
if(str.length==0){
throw new Exception("文件格式不正確");
}
String suffix = str[str.length-1];
if(!suffix.equals("txt") && !suffix.equals("doc") && !suffix.equals("docx") && !suffix.equals("xls")
&& !suffix.equals("xlsx") && !suffix.equals("ppt") && !suffix.equals("pptx")){
throw new Exception("文件格式不支持預(yù)覽");
}
InputStream in=FileConvertUtil.convertNetFile(url,suffix);
OutputStream outputStream = response.getOutputStream();
//創(chuàng)建存放文件內(nèi)容的數(shù)組
byte[] buff =new byte[1024];
//所讀取的內(nèi)容使用n來(lái)接收
int n;
//當(dāng)沒(méi)有讀取完時(shí),繼續(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();
}
5.controler層代碼?
@ApiOperation(value = "系統(tǒng)文件在線預(yù)覽接口 by tarzan")
@PostMapping("/api/file/onlinePreview")
public void onlinePreview(@RequestParam("url") String url, HttpServletResponse response) throws Exception{
fileService.onlinePreview(url,response);
}
到此這篇關(guān)于Java實(shí)現(xiàn)辦公文檔在線預(yù)覽功能的文章就介紹到這了,更多相關(guān)Java 文檔在線預(yù)覽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一些java二進(jìn)制的相關(guān)基礎(chǔ)知識(shí)
這篇文章主要介紹了一些java二進(jìn)制的相關(guān)基礎(chǔ)知識(shí),在Java語(yǔ)言中byte代表最小計(jì)量單位,byte由8位2進(jìn)制數(shù)組成。,需要的朋友可以參考下2019-06-06
使用kotlin集成springboot開(kāi)發(fā)的超詳細(xì)教程
目前大多數(shù)都在使用java集成 springboot進(jìn)行開(kāi)發(fā),本文演示僅僅將 java換成 kotlin,其他不變的情況下進(jìn)行開(kāi)發(fā),需要的朋友可以參考下2021-09-09
Java陷阱之a(chǎn)ssert關(guān)鍵字詳解
這篇文章詳細(xì)介紹了Java陷阱之a(chǎn)ssert關(guān)鍵字,有需要的朋友可以參考一下2013-09-09
java中的Io(input與output)操作總結(jié)(四)
前面已經(jīng)把java io的主要操作講完了,這一節(jié)我們來(lái)說(shuō)說(shuō)關(guān)于java io的其他內(nèi)容:Serializable序列化/DataOutputStream和DataInputStream類/管道流等等,感興趣的朋友可以了解下2013-01-01
Springboot非分布式定時(shí)任務(wù)實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot非分布式定時(shí)任務(wù)實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
SpringMVC 重定向參數(shù)RedirectAttributes實(shí)例
這篇文章主要介紹了SpringMVC 重定向參數(shù)RedirectAttributes實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot 中html的頁(yè)面間跳轉(zhuǎn)問(wèn)題小結(jié)
這篇文章主要介紹了SpringBoot 中html的頁(yè)面間跳轉(zhuǎn)問(wèn)題小結(jié),本文給大家分享兩種方法,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10

