java將文件轉(zhuǎn)成流文件返回給前端詳細(xì)代碼實(shí)例
環(huán)境:jdk1.8,springboot2.5.3,項(xiàng)目端口號(hào):9100
1.待轉(zhuǎn)換的文件
一、路徑

二、文件內(nèi)容

2.controller中代碼
package com.example.pdf.controller;
import com.example.pdf.service.GetFileStreamService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
/**
* @author
* @date 2024/3/29 16:28
* @describe
*/
@RestController
@RequestMapping(value = "test")
public class GetFileStreamController {
@Resource
private GetFileStreamService getFileStreamService;
/**
* 獲取文件流
*/
@GetMapping("getFileStream")
public void getFileStream(HttpServletResponse response) {
getFileStreamService.getFileStream(response);
}
}
3.service中代碼
package com.example.pdf.service;
import javax.servlet.http.HttpServletResponse;
/**
* @author
* @date 2024/3/29 16:30
* @describe
*/
public interface GetFileStreamService {
/**
* 獲取文件流
* @param response
*/
void getFileStream(HttpServletResponse response);
}4.實(shí)現(xiàn)類代碼
package com.example.pdf.service.impl;
import com.example.pdf.service.GetFileStreamService;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
/**
* @author
* @date 2024/3/29 16:31
* @describe
*/
@Service
public class GetFileStreamServiceImpl implements GetFileStreamService {
/**
* 獲取文件流
*/
@Override
public void getFileStream(HttpServletResponse response) {
// 指定文件路徑,獲取file文件
File file = new File("E:\\Desktop\\temps\\test.pdf");
try {
// 將文件轉(zhuǎn)為文件輸入流
FileInputStream fileInputStream = new FileInputStream(file);
// 獲取響應(yīng)的輸出流
OutputStream outputStream = response.getOutputStream();
// 將文件轉(zhuǎn)成字節(jié)數(shù)組,再將數(shù)組寫(xiě)入響應(yīng)的輸出流
byte[] buffer = new byte[1024];
int bytesRead = -1;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
// 刷新輸出流
outputStream.flush();
// 關(guān)閉流
fileInputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}5.postman中訪問(wèn)結(jié)果示例

6.瀏覽器中訪問(wèn)結(jié)果示例

總結(jié)
到此這篇關(guān)于java將文件轉(zhuǎn)成流文件返回給前端的文章就介紹到這了,更多相關(guān)java文件轉(zhuǎn)成流文件返回前端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JVM入門(mén)之JVM內(nèi)存結(jié)構(gòu)內(nèi)容詳解
這篇文章主要介紹了JVM入門(mén)之JVM內(nèi)存結(jié)構(gòu)內(nèi)容詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Java利用條件運(yùn)算符的嵌套來(lái)完成學(xué)習(xí)成績(jī)的劃分
這篇文章主要介紹了Java利用條件運(yùn)算符的嵌套來(lái)完成學(xué)習(xí)成績(jī)的劃分,需要的朋友可以參考下2017-02-02
Redis內(nèi)存數(shù)據(jù)庫(kù)示例分析
Redis本身的內(nèi)容比較復(fù)雜。如果你上來(lái),你應(yīng)該研究一個(gè)細(xì)節(jié)點(diǎn),比如連接池和數(shù)據(jù)結(jié)構(gòu)。雖然可以直接了解某一點(diǎn)的詳細(xì)來(lái)源內(nèi)容,甚至盡快解決一些意外,但是容易淹沒(méi)在失眠的細(xì)節(jié)中,整體控制不了Redis2022-12-12
詳解Maven項(xiàng)目Dependencies常見(jiàn)報(bào)錯(cuò)及解決方案
這篇文章主要介紹了詳解Maven項(xiàng)目Dependencies常見(jiàn)報(bào)錯(cuò)及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
mybatis-plus getOne和邏輯刪除問(wèn)題詳解
這篇文章主要介紹了mybatis-plus getOne和邏輯刪除,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
SpringBoot實(shí)現(xiàn)聯(lián)表查詢的代碼詳解
這篇文章主要介紹了SpringBoot中如何實(shí)現(xiàn)聯(lián)表查詢,文中通過(guò)代碼示例和圖文結(jié)合的方式講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-05-05

