Java后端返回PDF預(yù)覽給前端的實現(xiàn)
前端要預(yù)覽服務(wù)器PDF 可直接將要blob流返回給前端 即可用瀏覽器自帶pdf預(yù)覽功能打開,現(xiàn)有兩種方式
方式1 返回blob流給前端 代碼如下
@PostMapping(value = "/preview")
@ResponseBody
public void showPdf(HttpServletResponse response) {
try {
File file = new File("filePath");
OutputStream out = null;
try (BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
) {
byte[] bs = new byte[1024];
int len;
response.reset(); // 非常重要
URL u = new URL("file:///" + "filePath");
String contentType = u.openConnection().getContentType();
response.setContentType(contentType);
response.setHeader("Content-Disposition", "inline;filename="
+ "preview.pdf");
out = response.getOutputStream();
while ((len = br.read(bs)) > 0) {
out.write(bs, 0, len);
}
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}此時 前端解析可直接拿返回的文件流 例子如下
let blob = this.response;
const binaryData = [];
binaryData.push(blob);
const url = window.URL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' }));
window.open(url);但有的時候 不想返回文件流 可把文件返回為base64 (注意 base64可能超長)此時代碼修改如下
File file = new File("filePath");
OutputStream out = null;
try (BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
) {
int len;
URL u = new URL("file:///" + "filePath");
String contentType = u.openConnection().getContentType();
byte[] buffer = new byte[1024];
//每次讀取的字符串長度,如果為-1,代表全部讀取完畢
//使用輸入流從buffer里把數(shù)據(jù)讀取出來
while ((len = br.read(buffer)) != -1) {
//用輸出流往buffer里寫入數(shù)據(jù),中間參數(shù)代表從哪個位置開始讀,len代表讀取的長度
outStream.write(buffer, 0, len);
}
// 對字節(jié)數(shù)組Base64編碼
Base64Encoder base64 = new Base64Encoder();
String base64Str = replaceEnter(base64.encode(outStream.toByteArray()));
}
} catch (Exception e) {
}前端修改如下
let base64 = resBase64.data.base64;
base64 = base64.replace(/[\n\r]/g, '')
const raw = window.atob(base64)
const rawLength = raw.length
const uInt8Array = new Uint8Array(rawLength)
for (let i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i)
}
const url = window.URL.createObjectURL(new Blob([uInt8Array], { type: resBase64.data.contentType }));
window.open(url);到此這篇關(guān)于Java后端返回PDF預(yù)覽給前端的實現(xiàn)的文章就介紹到這了,更多相關(guān)Java 返回PDF預(yù)覽給前端內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java構(gòu)建樹形結(jié)構(gòu)的實現(xiàn)過程
文章介紹了五種構(gòu)建樹形結(jié)構(gòu)的方式,包括定義實體類、利用Map集合、使用Stream流、基于Hutool以及MyBatis-Plus的@TableName屬性,最后,還提供了一個基于Java實現(xiàn)樹結(jié)構(gòu)模糊搜索功能的示例2025-11-11
使用SpringBoot中web項目推薦目錄結(jié)構(gòu)的問題
這篇文章主要介紹了SpringBoot中web項目推薦目錄結(jié)構(gòu)的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
Spring?Boot整合Bootstrap的超詳細(xì)步驟
之前做前端開發(fā),在使用bootstrap的時候都是去官網(wǎng)下載,然后放到項目中,在頁面引用,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot整合Bootstrap的超詳細(xì)步驟,需要的朋友可以參考下2023-05-05
Java的JDBC編程使用之連接Mysql數(shù)據(jù)庫
這篇文章主要給大家介紹了關(guān)于Java的JDBC編程使用之連接Mysql數(shù)據(jù)庫的相關(guān)資料,JDBC是一種用于執(zhí)行SQL語句的Java?API,可以為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問,需要的朋友可以參考下2023-12-12
詳解Java時區(qū)處理之Date,Calendar,TimeZone,SimpleDateFormat
這篇文章主要介紹了Java時區(qū)處理之Date,Calendar,TimeZone,SimpleDateFormat的區(qū)別于用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

