Java下載項(xiàng)目中靜態(tài)文件方式
Java下載項(xiàng)目中靜態(tài)文件
廢話不多說(shuō),直接上代碼,拷貝即可用~~~
項(xiàng)目結(jié)構(gòu)

下載工具類
/**
* @program: myutil
* @description: 從本地項(xiàng)目(本地磁盤上)下載靜態(tài)文件
* @author: lsy
* @create: 2020-08-13 16:58
**/
public class LocalFileUtils {
/**
* @param response
* @param fileName
* @description 根據(jù)指定項(xiàng)目路徑下的某個(gè)excel, 下載文件
*/
public static void exportFile(HttpServletResponse response, String fileName) {
// 第一種獲取靜態(tài)資源
ClassPathResource classPathResource = new ClassPathResource("static/excleTemplate/" + fileName);// "static/excleTemplate/ImportModel.xlsx"
// 第二種獲取靜態(tài)資源
// InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/excleTemplate/" + fileName);
// 第三種獲取靜態(tài)資源
// InputStream inputStream = this.getClass().getResourceAsStream("static/excleTemplate/" + fileName);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = classPathResource.getInputStream();
outputStream = response.getOutputStream();
int BUFFER_SIZE = 1024 * 4;
byte[] buffer = new byte[BUFFER_SIZE];
int reader = 0;
while ((reader = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, reader);
}
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
String newFileName = URLEncoder.encode(classPathResource.getFilename(), "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + newFileName);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
/**flush():僅僅刷新緩沖區(qū)(一般寫(xiě)字符時(shí)要用,因?yàn)樽址麜r(shí)先進(jìn)入緩沖區(qū)),然后將內(nèi)存中的數(shù)據(jù)立刻寫(xiě)出(因?yàn)榫彌_區(qū)是裝滿之后才會(huì)寫(xiě)出
,用flush()就不必等到緩沖區(qū)滿,立刻寫(xiě)出,流對(duì)象還可以繼續(xù)使用) */
outputStream.flush();
/**close():關(guān)閉流對(duì)象. 也會(huì)先刷新一次緩沖區(qū),再關(guān)閉. 關(guān)閉之后,流對(duì)象不可以繼續(xù)使用 */
outputStream.close();
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}控制器
@ApiOperation(value = "獲取resource下附件")
@GetMapping(value = "/exportFile")
public void exportFile(String fileName, HttpServletResponse response) {
// fileName = "ImportModel.xlsx";
fileName = "labixiaoxin.jpg";
LocalFileUtils.exportFile(response, fileName);
}Java把靜態(tài)資源文件下載到本地
場(chǎng)景
springboot項(xiàng)目中下載resources/static 下面的靜態(tài)文件(或者本地文件)
@RequestMapping("/doLoad")
public void doLoad(HttpServletRequest request, HttpServletResponse response){
String filename = "×××模版";
try {
// 清空輸出流
response.reset();
String resultFileName = filename + ".xlsx";
resultFileName = URLEncoder.encode(resultFileName,"UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment; filename=" + resultFileName);// 設(shè)定輸出文件頭
response.setContentType("application/msexcel");// 定義輸出類型
//輸入流:文件路徑 // 本地路徑:E:\\java\\demo\\導(dǎo)入模板.xlsx
DataInputStream in = new DataInputStream(
new FileInputStream(new File("src/main/resources/static/file/導(dǎo)入模版.xlsx")));
//輸出流
OutputStream out = response.getOutputStream();
//輸出文件
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
// 清空輸出流
response.reset();
}
}
效果:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud Ribbon實(shí)現(xiàn)客戶端負(fù)載均衡的方法
本篇文章主要介紹了Spring Cloud Ribbon實(shí)現(xiàn)客戶端負(fù)載均衡的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
詳解SpringBoot如何優(yōu)雅的進(jìn)行全局異常處理
在SpringBoot的開(kāi)發(fā)中,為了提高程序運(yùn)行的魯棒性,我們經(jīng)常需要對(duì)各種程序異常進(jìn)行處理,但是如果在每個(gè)出異常的地方進(jìn)行單獨(dú)處理的話,這會(huì)引入大量業(yè)務(wù)不相關(guān)的異常處理代碼,這篇文章帶大家了解一下如何優(yōu)雅的進(jìn)行全局異常處理2023-07-07
Java中反射reflect的基礎(chǔ)知識(shí)講解
這篇文章主要介紹了Java中反射reflect的基礎(chǔ)知識(shí)講解,Java中的反射,它算是Java當(dāng)中非常底層的一個(gè)技術(shù),平時(shí)我們我們用得不多,實(shí)際上它也的確非常復(fù)雜同時(shí)也難以理解,但是涉及到底層的東西Java都給我們封裝好了,我們直接拿來(lái)調(diào)用即可,需要的朋友可以參考下2023-10-10
dubbo如何設(shè)置連接zookeeper權(quán)限
這篇文章主要介紹了dubbo如何設(shè)置連接zookeeper權(quán)限問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Spring?Boot?實(shí)現(xiàn)?IP?限流(保障系統(tǒng)穩(wěn)定性的關(guān)鍵技術(shù))
在Spring?Boot中實(shí)現(xiàn)IP限流是一種簡(jiǎn)單而有效的方式來(lái)保障系統(tǒng)的穩(wěn)定性和可用性,本文通過(guò)實(shí)例代碼講解Spring?Boot實(shí)現(xiàn)IP限流的相關(guān)操作,感興趣的朋友一起看看吧2025-06-06
SpringBoot集成內(nèi)存數(shù)據(jù)庫(kù)Derby的實(shí)踐
像H2、hsqldb、derby、sqlite這樣的內(nèi)存數(shù)據(jù)庫(kù),小巧可愛(ài),做小型服務(wù)端演示程序,非常好用。最大特點(diǎn)就是不需要你另外安裝一個(gè)數(shù)據(jù)庫(kù)。本文主要介紹了SpringBoot集成內(nèi)存數(shù)據(jù)庫(kù)Derby,感興趣的可以了解一下2021-09-09
Java特性?Lambda?表達(dá)式和函數(shù)式接口
這篇文章主要介紹了Java特性?Lambda?表達(dá)式和函數(shù)式接口,Lambda表達(dá)式基于函數(shù)式編程思想,也可以稱為閉包,是Java?8引入的重要新特性,?Lambda允許把函數(shù)作為一個(gè)方法的參數(shù)2022-06-06

