欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot利用aspose預(yù)覽office文件的實(shí)現(xiàn)過程

 更新時間:2021年06月08日 09:12:10   作者:灰太狼_cxh  
這篇文章主要給大家介紹了關(guān)于springboot利用aspose預(yù)覽office文件的相關(guān)資料,文中通過示例代碼以及圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考價值,需要的朋友可以參考下

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

主要實(shí)現(xiàn)原理是:瀏覽器可以直接預(yù)覽pdf,所以使用aspose把office文件轉(zhuǎn)換為pdf文件,進(jìn)行預(yù)覽。

1.主要寫了個簡單的demo,項目目錄:

2.pom.xml添加aspose的依賴包

(目前maven倉庫不提供以下aspose的依賴包,可以自行下載添加進(jìn)maven倉庫,或者直接拉到最下面下載本人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倉庫

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頁面
     * @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/測試.doc')">預(yù)覽doc </button>
<button  target="_blank" type="button"
        onclick="showFile('d:/file/測試.docx')">預(yù)覽docx </button>
<button  target="_blank" type="button"
        onclick="showFile('d:/file/測試.xls')">預(yù)覽xls </button>
<button target="_blank" type="button"
        onclick="showFile('d:/file/測試.xlsx')">預(yù)覽xlsx </button>
<button  target="_blank" type="button"
        onclick="showFile('d:/file/測試.pptx')">預(yù)覽pptx </button>
<button  target="_blank" type="button"
        onclick="showFile('d:/file/數(shù)據(jù)庫原理(第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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java Swing JButton按鈕的實(shí)現(xiàn)示例

    Java Swing JButton按鈕的實(shí)現(xiàn)示例

    這篇文章主要介紹了Java Swing JButton按鈕的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • java poi導(dǎo)出excel時如何設(shè)置手動換行

    java poi導(dǎo)出excel時如何設(shè)置手動換行

    這篇文章主要介紹了java poi導(dǎo)出excel時如何設(shè)置手動換行,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 淺談java的接口和C++虛類的相同和不同之處

    淺談java的接口和C++虛類的相同和不同之處

    下面小編就為大家?guī)硪黄獪\談java的接口和C++虛類的相同和不同之處。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦
    2016-12-12
  • Java中的clone方法實(shí)例詳解

    Java中的clone方法實(shí)例詳解

    這篇文章主要介紹了Java中的clone方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • servlet之session簡介_動力節(jié)點(diǎn)Java學(xué)院整理

    servlet之session簡介_動力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了servlet之session簡介,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • java中多線程的超詳細(xì)介紹

    java中多線程的超詳細(xì)介紹

    這篇文章主要給大家介紹了關(guān)于java中多線程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java基于Swing和netty實(shí)現(xiàn)仿QQ界面聊天小項目

    Java基于Swing和netty實(shí)現(xiàn)仿QQ界面聊天小項目

    這篇文章主要為大家詳細(xì)介紹了Java如何利用Swing和netty實(shí)現(xiàn)仿QQ界面聊天小項目,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-09-09
  • Java應(yīng)用層協(xié)議WebSocket實(shí)現(xiàn)消息推送

    Java應(yīng)用層協(xié)議WebSocket實(shí)現(xiàn)消息推送

    后端向前端推送消息就需要長連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關(guān)于java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程,需要的朋友可以參考下
    2023-02-02
  • Java抽象類和抽象方法定義與用法實(shí)例詳解

    Java抽象類和抽象方法定義與用法實(shí)例詳解

    這篇文章主要介紹了Java抽象類和抽象方法定義與用法,結(jié)合實(shí)例形式詳細(xì)分析了Java抽象類和抽象方法相關(guān)原理、定義、使用方法及操作注意事項,需要的朋友可以參考下
    2019-11-11
  • 深入理解JSON及其在Java中的應(yīng)用小結(jié)

    深入理解JSON及其在Java中的應(yīng)用小結(jié)

    json它是一種輕量級的數(shù)據(jù)交換格式,由于其易于閱讀和編寫,同時也易于機(jī)器解析和生成,因此廣泛應(yīng)用于網(wǎng)絡(luò)數(shù)據(jù)交換和配置文件,這篇文章主要介紹了深入理解JSON及其在Java中的應(yīng)用,需要的朋友可以參考下
    2023-12-12

最新評論