springboot利用aspose預(yù)覽office文件的實(shí)現(xiàn)過(guò)程
springboot項(xiàng)目使用aspose預(yù)覽office文件,運(yùn)行實(shí)現(xiàn)預(yù)覽效果:





主要實(shí)現(xiàn)原理是:瀏覽器可以直接預(yù)覽pdf,所以使用aspose把office文件轉(zhuǎn)換為pdf文件,進(jìn)行預(yù)覽。
1.主要寫了個(gè)簡(jiǎn)單的demo,項(xiàng)目目錄:

2.pom.xml添加aspose的依賴包
(目前maven倉(cāng)庫(kù)不提供以下aspose的依賴包,可以自行下載添加進(jìn)maven倉(cāng)庫(kù),或者直接拉到最下面下載本人demo,demo提供了相應(yīng)的jar包)
<!--aspose預(yù)覽office文件-->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>19.3</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
</dependency>
<!--end-->

把jar包放在d盤,然后cmd,執(zhí)行命令把jar包加進(jìn)maven倉(cāng)庫(kù)
mvn install:install-file -Dfile=D:\jar\aspose-words-15.8.0.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar mvn install:install-file -Dfile=D:\jar\aspose-cells-8.5.2.jar -DgroupId=com.aspose -DartifactId=aspose-cells -Dversion=8.5.2 -Dpackaging=jar mvn install:install-file -Dfile=D:\jar\aspose.slides-19.3.jar -DgroupId=com.aspose -DartifactId=aspose-slides -Dversion=19.3 -Dpackaging=jar
3.后端主要代碼:
@Controller
public class FileController {
@Autowired
private FileToPdfComUtils fileToPdfComUtils;
/**
* index頁(yè)面
* @return
*/
@GetMapping("/index")
public String index(){
return "index";
}
/**
* 文件預(yù)覽
* @param filePath
* @param request
* @param response
* @throws IOException
*/
@GetMapping("/showFile")
@ResponseBody
public void showFile(String filePath, HttpServletRequest request, HttpServletResponse response) throws IOException {
//源文件路徑
String sourcePath = filePath;
//pdf文件路徑
String pdfPath = null;
//獲取文件后綴判斷是否轉(zhuǎn)換pdf
int index = filePath.lastIndexOf(".");
String fileType = filePath.substring(index + 1);
String toPdfSuffix = "doc,docx,xls,xlsx,ppt,pptx";
try {
if(toPdfSuffix.indexOf(fileType) >= 0){
pdfPath = sourcePath.substring(0, index) + ".pdf";
//源文件轉(zhuǎn)換pdf
fileToPdfComUtils.officeToPdf(sourcePath, pdfPath);
File pdfFile = new File(pdfPath);
InputStream is = new FileInputStream(pdfFile);
showPdf(is, pdfPath, request, response);
} else {
//不用轉(zhuǎn)換,直接預(yù)覽
File pdfFile = new File(filePath);
InputStream is = new FileInputStream(pdfFile);
showPdf(is,filePath, request, response);
}
}catch (Exception e){
e.printStackTrace();
} finally {
//最后刪除生成的pdf文件
FileUtils.deleteFile(pdfPath);
}
}
/**
* 文件預(yù)覽
* @param is
* @param fileKey
* @param request
* @param response
* @throws IOException
*/
public void showPdf(InputStream is, String fileKey, HttpServletRequest request, HttpServletResponse response) throws IOException {
//根據(jù)文件名獲取 MIME 類型
int idx = fileKey.lastIndexOf(".");
String suffix = fileKey.substring(idx);
String[] fileKeys = fileKey.split("/");
String fileName = fileKeys[fileKeys.length - 1];
String contentType = FileContentType.SUFFIX_TYPE.get(suffix);
//inline表示直接預(yù)覽
String userAgent = request.getHeader("USER-AGENT");
String contentDisposition = "";
if(StringUtils.contains(userAgent, "MSIE")||StringUtils.contains(userAgent, "Trident") || StringUtils.contains(userAgent,"Edge")){
//IE 瀏覽器
contentDisposition = "inline;filename=" + URLEncoder.encode(fileName,"UTF8");
}else {
//其他瀏覽器
contentDisposition = "inline;filename=" + new String(fileName.getBytes("UTF-8"),"ISO8859-1");
}
// 設(shè)置頭
response.setHeader("Content-Disposition",contentDisposition);
response.setContentType(contentType);
// 獲取綁定了客戶端的流
ServletOutputStream output = response.getOutputStream();
// 把輸入流中的數(shù)據(jù)寫入到輸出流中
IOUtils.copy(is,output);
is.close();
}
}
4.前端代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>預(yù)覽文件</title>
</head>
<body>
<button target="_blank" type="button"
onclick="showFile('d:/file/測(cè)試.doc')">預(yù)覽doc </button>
<button target="_blank" type="button"
onclick="showFile('d:/file/測(cè)試.docx')">預(yù)覽docx </button>
<button target="_blank" type="button"
onclick="showFile('d:/file/測(cè)試.xls')">預(yù)覽xls </button>
<button target="_blank" type="button"
onclick="showFile('d:/file/測(cè)試.xlsx')">預(yù)覽xlsx </button>
<button target="_blank" type="button"
onclick="showFile('d:/file/測(cè)試.pptx')">預(yù)覽pptx </button>
<button target="_blank" type="button"
onclick="showFile('d:/file/數(shù)據(jù)庫(kù)原理(第5版)(樣章).pdf')">預(yù)覽pdf </button>
<script>
//預(yù)覽
function showFile (filePath){
var url = "/showFile" + "?filePath=" + filePath;
window.open(url);
};
</script>
</body>
</html>
5.本人文件目錄:

6.下載demo:
https://download.csdn.net/download/weixin_39220472/19418676
總結(jié)
到此這篇關(guān)于springboot利用aspose預(yù)覽office文件的文章就介紹到這了,更多相關(guān)springboot預(yù)覽office文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決mybatis用Map返回的字段全變大寫的問(wèn)題
使用Java和WebSocket實(shí)現(xiàn)網(wǎng)頁(yè)聊天室實(shí)例代碼
解決Error:(5,55)java:程序包org.springframework.cloud.netflix.eure
Java實(shí)現(xiàn)二叉樹(shù)的基本操作詳解
springboot+vue實(shí)現(xiàn)頁(yè)面下載文件
解析Neatbeans(常見(jiàn)錯(cuò)誤) build-impl.xml:305: Compile failed
Mybatis通過(guò)Mapper代理連接數(shù)據(jù)庫(kù)的方法

