您好,登錄后才能下訂單哦!
springboot2中怎么實(shí)現(xiàn)在線文檔預(yù)覽,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
第一步,需要引入相應(yīng)的jar:
<!--在線文件預(yù)覽================================================================--> <!--jodconverter 核心包--> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-core</artifactId> <version>4.2.2</version> </dependency> <!--springboot支持包,里面包括了自動(dòng)配置類 --> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.2.2</version> </dependency> <!--jodconverter 本地支持包 --> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local</artifactId> <version>4.2.2</version> </dependency>
第二步,在配置文件中加入關(guān)鍵配置:
第三步:核心類
package com.yunji.kwxt.document; import com.yunji.kwxt.common.enums.ResultEnum; import com.yunji.kwxt.common.model.ResultJson; import org.apache.commons.io.IOUtils; import org.jodconverter.DocumentConverter; import org.jodconverter.office.OfficeException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; /** * @author :LX * 創(chuàng)建時(shí)間: 2019/11/4. 17:42 * 地點(diǎn):廣州 * 目的: 在線文檔預(yù)覽 * * 總的來(lái)說(shuō),這部分的代碼是可以使用的,但是效果并沒(méi)有預(yù)期的好,第一,中文亂碼問(wèn)題沒(méi)解決,第二,格式并沒(méi)有預(yù)期的好。 * 業(yè)務(wù)邏輯這一塊,不推薦這么弄,建議從源頭控制上傳文件,然后來(lái)預(yù)覽操作。 * * 如果后續(xù)需要使用,1 放開(kāi)maven中的 jodconverter 包 * 2 將 application-config.properties 文件相應(yīng) jodconverter 的配置放開(kāi) * 3 將該類下面的 view 方法放開(kāi),調(diào)用 view 即可。 * * 備注說(shuō)明: */ @Controller @RequestMapping("/doc") public class DocumentController { private static Logger log = LoggerFactory.getLogger(DocumentController.class); @Resource private DocumentConverter documentConverter; /** * 在線預(yù)覽 * @param response * @return */ @RequestMapping(value = "/view", method = RequestMethod.GET) @ResponseBody public ResultJson view(HttpServletResponse response){ //需要轉(zhuǎn)換的文件 File file = new File("E:\\下載\\kd.xlsx"); //文件轉(zhuǎn)換后的地址 File toFile = new File("E:\\temp"); if (!toFile.exists()){ toFile.mkdirs(); } ServletOutputStream outputStream = null; InputStream in = null; //關(guān)鍵方法,轉(zhuǎn)換為PDF try { documentConverter.convert(file).to(new File("E:/temp/1.pdf")).execute(); outputStream = response.getOutputStream(); in = new FileInputStream(new File("E:/temp/1.pdf")); //將文件轉(zhuǎn)換復(fù)制到流 IOUtils.copy(in, outputStream); } catch (OfficeException e) { e.printStackTrace(); log.error("轉(zhuǎn)換文件失敗"); } catch (IOException e) { e.printStackTrace(); log.error("獲取流失敗"); } finally { if (in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return new ResultJson(null, ResultEnum.SUCCESS.getStatus(), "成功", null); } }
看完上述內(nèi)容,你們掌握springboot2中怎么實(shí)現(xiàn)在線文檔預(yù)覽的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。