Java實(shí)現(xiàn)PDF在線預(yù)覽功能(四種方式)
Java實(shí)現(xiàn)PDF在線預(yù)覽
@RequestMapping("/preview1")
public void er(HttpServletResponse response){
File file = new File("G:\\桌面\\Thymeleaf3.0中文翻譯文檔@www.java1234.com.pdf");
if (file.exists()){
byte[] data = null;
try {
FileInputStream input = new FileInputStream(file);
data = new byte[input.available()];
input.read(data);
response.getOutputStream().write(data);
input.close();
} catch (Exception e) {
System.out.println(e);
}
}else{
return;
}
}
@ResponseBody
@RequestMapping("/preview2")
public void findPdf( HttpServletResponse response) throws IOException{
response.setContentType("application/pdf");
FileInputStream in = new FileInputStream(new File("G:\\桌面\\Thymeleaf3.0中文翻譯文檔@www.java1234.com.pdf"));
OutputStream out = response.getOutputStream();
byte[] b = new byte[512];
while ((in.read(b))!=-1) {
out.write(b);
}
out.flush();
in.close();
out.close();
}
@ResponseBody
@RequestMapping("/preview3")
public void devDoc(HttpServletRequest request, HttpServletResponse response, String storeName) throws Exception {
request.setCharacterEncoding("UTF-8");
String ctxPath = request.getSession().getServletContext().getRealPath("");
String downLoadPath = "G:\\桌面\\Thymeleaf3.0中文翻譯文檔@www.java1234.com.pdf";
response.setContentType("application/pdf");
FileInputStream in = new FileInputStream(new File(downLoadPath));
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
while ((in.read(b))!=-1) {
out.write(b);
}
out.flush();
in.close();
out.close();
}
@ResponseBody
@RequestMapping("/preview")
public void download( HttpServletResponse response
) throws IOException {
String filePath = "G:\\桌面\\Thymeleaf3.0中文翻譯文檔@www.java1234.com.pdf";
System.out.println("filePath:" + filePath);
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] bs = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (true) { // 在線打開方式
URL u = new URL("file:///" + filePath);
String contentType = u.openConnection().getContentType();
response.setContentType(contentType);
response.setHeader("Content-Disposition", "inline;filename="
+ "2019年上半年英語四級(jí)筆試準(zhǔn)考證(戴林峰).pdf");
// 文件名應(yīng)該編碼成utf-8,注意:使用時(shí),我們可忽略這句
} else {
// 純下載方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename="
+ "2019年上半年英語四級(jí)筆試準(zhǔn)考證(戴林峰).pdf");
}
OutputStream out = response.getOutputStream();
while ((len = br.read(bs)) > 0) {
out.write(bs, 0, len);
}
out.flush();
out.close();
br.close();
}
Java快捷實(shí)現(xiàn)PDF在線預(yù)覽
本文章利用各個(gè)瀏覽器的內(nèi)核支持PDF預(yù)覽功能來做,PDF的預(yù)覽網(wǎng)上說的最多的是PDF.JS實(shí)現(xiàn)預(yù)覽,也找了好多文章,死活最后實(shí)現(xiàn)不了,通過這個(gè)方法解決了,如果有PDF.js解決的可以大家溝通交流下。
這里提一下,其他word和excel可以通過aspose代碼轉(zhuǎn)換成PDF,也很快有效??梢跃W(wǎng)上搜下。
方法很簡(jiǎn)單,首先引入的都是IO的包。
import java.io.File; import java.io.FileInputStream; import java.io.OutputStream; import org.apache.commons.io.IOUtils;
接下來就是在自己的后臺(tái)controller里面寫個(gè)方法。里面file的文件可以通過參數(shù)或是封裝方式獲取,這個(gè)里面的流不需要關(guān)閉,瀏覽器會(huì)自動(dòng)獲取然后展示。關(guān)閉的話瀏覽器就無法加載!
@RequestMapping(value = "/showpdf")
public void showpdf(HttpServletRequest request, HttpServletResponse response, Model model) {
try {
File file = new File("D:/xyptFile/java.pdf");
FileInputStream fileInputStream = new FileInputStream(file);
response.setHeader("Content-Type", "application/pdf");
OutputStream outputStream = response.getOutputStream();
IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
} catch(Exception e) {
e.printStackTrace();
}
}
文章來源:從其他各個(gè)文章中測(cè)試驗(yàn)證,取得最優(yōu)方案。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)雙端鏈表LinkedList
本文主要介紹了Java實(shí)現(xiàn)雙端鏈表LinkedList,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Java switch case數(shù)據(jù)類型原理解析
這篇文章主要介紹了Java switch case數(shù)據(jù)類型原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Spring?boot詳解fastjson過濾字段為null值如何解決
這篇文章主要介紹了解決Spring?boot中fastjson過濾字段為null值的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
一文看懂springboot實(shí)現(xiàn)短信服務(wù)功能
項(xiàng)目中的短信服務(wù)基本上上都會(huì)用到,簡(jiǎn)單的注冊(cè)驗(yàn)證碼,消息通知等等都會(huì)用到。這篇文章主要介紹了springboot 實(shí)現(xiàn)短信服務(wù)功能,需要的朋友可以參考下2019-10-10
SpringBoot中自定義注解實(shí)現(xiàn)控制器訪問次數(shù)限制實(shí)例
本篇文章主要介紹了SpringBoot中自定義注解實(shí)現(xiàn)控制器訪問次數(shù)限制實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
在SpringBoot項(xiàng)目中解決依賴沖突問題的方法
在SpringBoot項(xiàng)目中,依賴沖突是一個(gè)常見的問題,特別是當(dāng)項(xiàng)目引入多個(gè)第三方庫(kù)或框架時(shí),依賴沖突可能導(dǎo)致編譯錯(cuò)誤、運(yùn)行時(shí)異常或不可預(yù)測(cè)的行為,本文給大家介紹了如何在SpringBoot項(xiàng)目中解決以來沖突問題的方法,需要的朋友可以參考下2024-01-01
給Java文件打成獨(dú)立JAR包的詳細(xì)步驟記錄
這篇文章主要介紹了給Java文件打成獨(dú)立JAR包的相關(guān)資料,文中將Java文件打包成獨(dú)立的JAR包,包括非Maven和Maven項(xiàng)目的打包步驟,需要的朋友可以參考下2024-12-12

