Springboot如何通過流返回文件
如何通過流返回文件
本人的文件是放在resource/templates目錄下,截圖如下

controller類如下
@GetMapping(value = "/downfile")
public void download(HttpServletResponse response) throws IOException {
String fileName = "template.xlsx";
// 設(shè)置信息給客戶端不解析
String type = new MimetypesFileTypeMap().getContentType(fileName);
// 設(shè)置contenttype,即告訴客戶端所發(fā)送的數(shù)據(jù)屬于什么類型
response.setHeader("Content-type",type);
// 設(shè)置編碼
String code = new String(fileName.getBytes("utf-8"), "iso-8859-1");
// 設(shè)置擴(kuò)展頭,當(dāng)Content-Type 的類型為要下載的類型時(shí) , 這個(gè)信息頭會(huì)告訴瀏覽器這個(gè)文件的名字和類型。
response.setHeader("Content-Disposition", "attachment;filename=" + code);
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
DownLoadUtils.download(fileName, response);
}工具類DownLoadUtils如下
public class DownLoadUtils {
public static void download(String filename, HttpServletResponse res) throws IOException {
// 發(fā)送給客戶端的數(shù)據(jù)
// 讀取filename
ClassPathResource classPathResource = new ClassPathResource("templates/"+filename);
long length = classPathResource.getFile().length();
res.addHeader("Content-Length",String.valueOf(length));
OutputStream outputStream = res.getOutputStream();
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
InputStream inputStream =classPathResource.getInputStream();
bis = new BufferedInputStream(inputStream);
int i = bis.read(buff);
while (i != -1) {
outputStream.write(buff, 0, buff.length);
outputStream.flush();
i = bis.read(buff);
}
bis.close();
outputStream.close();
}
}注意點(diǎn)
response.addHeader("Content-Length",String.valueOf(file.length()));如果不加這句代碼,下載下來的文件會(huì) 在打開前提示修復(fù),文件格式或文件擴(kuò)展名無效。請確定文件未損壞,并且文件擴(kuò)展名與文件的格式匹配
以流的方式直接返回
import java.io.FileReader;
import java.io.InputStream;
import java.util.Properties;
/*
*/
public class Reflect {
public static void main(String[] args) throws Exception{
//獲取一個(gè)文件的絕對路徑!?。?
// 這種是先獲得絕對路徑然后在轉(zhuǎn)換成流。
// String path = Thread.currentThread().getContextClassLoader()
// .getResource("classinfo2.properties").getPath();
// FileReader reader = new FileReader(path);
//下面這種是直接用流的方式返回。
InputStream reader = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("classinfo2.properties");
Properties pro = new Properties();
pro.load(reader);
reader.close();
//通過key獲取value。
String className = pro.getProperty("className");
System.out.println(className);
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合高德地圖實(shí)現(xiàn)天氣預(yù)報(bào)功能
在當(dāng)今數(shù)字化時(shí)代,天氣預(yù)報(bào)功能在眾多應(yīng)用中扮演著重要角色,通過整合高德地圖提供的天氣API,我們可以輕松地在自己的SpringBoot項(xiàng)目中實(shí)現(xiàn)這一功能,本文將詳細(xì)介紹如何在SpringBoot項(xiàng)目中整合高德地圖的天氣預(yù)報(bào)功能,感興趣的小伙伴跟著小編一起來看看吧2025-03-03
Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式
這篇文章主要介紹了Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
JAVA 開發(fā)之用靜態(tài)方法返回類名的實(shí)例詳解
這篇文章主要介紹了JAVA 開發(fā)之用靜態(tài)方法返回類名的實(shí)例詳解的相關(guān)資料,這里主要說明使用異常來得到類名,希望能幫助到大家,需要的朋友可以參考下2017-08-08
Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之校園一卡通系統(tǒng)的實(shí)現(xiàn)
這是一個(gè)使用了java+Springboot+Maven+mybatis+Vue+mysql+wd開發(fā)的校園一卡通系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有校園一卡通系統(tǒng)該有的所有功能,感興趣的朋友快來看看吧2022-01-01
IDEA?Ui設(shè)計(jì)器JFormDesigner?永久激活插件+注冊機(jī)(親測一直在用)
這篇文章主要介紹了IDEA?Ui設(shè)計(jì)器JFormDesigner?永久激活----插件+注冊機(jī)?自己一直在用的版本和注冊機(jī),非常不錯(cuò),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
解決ApplicationContext獲取不到Bean的問題
這篇文章主要介紹了解決ApplicationContext獲取不到Bean的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

