java將文件轉(zhuǎn)成流文件返回給前端詳細(xì)代碼實例
更新時間:2024年07月15日 10:16:05 作者:愛北的琳兒
Java編程語言提供了強(qiáng)大的文件處理和壓縮能力,下面這篇文章主要給大家介紹了關(guān)于java將文件轉(zhuǎn)成流文件返回給前端的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
環(huán)境:jdk1.8,springboot2.5.3,項目端口號: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.實現(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ù)組寫入響應(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中訪問結(jié)果示例

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

總結(jié)
到此這篇關(guān)于java將文件轉(zhuǎn)成流文件返回給前端的文章就介紹到這了,更多相關(guān)java文件轉(zhuǎn)成流文件返回前端內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JVM入門之JVM內(nèi)存結(jié)構(gòu)內(nèi)容詳解
這篇文章主要介紹了JVM入門之JVM內(nèi)存結(jié)構(gòu)內(nèi)容詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Java利用條件運算符的嵌套來完成學(xué)習(xí)成績的劃分
這篇文章主要介紹了Java利用條件運算符的嵌套來完成學(xué)習(xí)成績的劃分,需要的朋友可以參考下2017-02-02
詳解Maven項目Dependencies常見報錯及解決方案
這篇文章主要介紹了詳解Maven項目Dependencies常見報錯及解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
SpringBoot實現(xiàn)聯(lián)表查詢的代碼詳解
這篇文章主要介紹了SpringBoot中如何實現(xiàn)聯(lián)表查詢,文中通過代碼示例和圖文結(jié)合的方式講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-05-05

