Java下載項(xiàng)目中靜態(tài)文件方式
Java下載項(xiàng)目中靜態(tài)文件
廢話不多說,直接上代碼,拷貝即可用~~~
項(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ū)(一般寫字符時(shí)要用,因?yàn)樽址麜r(shí)先進(jìn)入緩沖區(qū)),然后將內(nèi)存中的數(shù)據(jù)立刻寫出(因?yàn)榫彌_區(qū)是裝滿之后才會(huì)寫出 ,用flush()就不必等到緩沖區(qū)滿,立刻寫出,流對(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)文章
SpringBoot中@ConfigurationProperties注解實(shí)現(xiàn)配置綁定的三種方法
這篇文章主要介紹了SpringBoot中@ConfigurationProperties注解實(shí)現(xiàn)配置綁定的三種方法,文章內(nèi)容介紹詳細(xì)需要的小伙伴可以參考一下2022-04-04SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控的基本操作
SpringBoot中的 redistemplate 是一個(gè)用于操作 Redis 數(shù)據(jù)庫的高級(jí)模板類,它提供了一組方法,可以方便地執(zhí)行常見的 Redis 操作,如存儲(chǔ)、檢索和刪除數(shù)據(jù),本文給大家介紹了SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控的基本操作,需要的朋友可以參考下2025-02-02Lombok使用@Tolerate實(shí)現(xiàn)沖突兼容問題
這篇文章主要介紹了Lombok使用@Tolerate實(shí)現(xiàn)沖突兼容問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Spring MVC獲取參數(shù)和自定義參數(shù)類型轉(zhuǎn)換器及編碼過濾器
這篇文章主要為大家詳細(xì)介紹了Spring MVC獲取參數(shù)和自定義參數(shù)類型轉(zhuǎn)換器及編碼過濾器,文中通過代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-06-06Java實(shí)現(xiàn)Elasticsearch查詢當(dāng)前索引全部數(shù)據(jù)的完整代碼
這篇文章主要介紹了如何在Java中實(shí)現(xiàn)查詢Elasticsearch索引中指定條件下的全部數(shù)據(jù),通過設(shè)置滾動(dòng)查詢參數(shù)(scroll),可以一次性獲取滿足條件的數(shù)據(jù),而不需要限制每頁的查詢條數(shù)大小,這樣可以避免因數(shù)據(jù)量過大而引發(fā)的性能問題,需要的朋友可以參考下2025-02-02JavaWeb禁止瀏覽器緩存當(dāng)前Web頁面的方法
所謂瀏覽器緩存,是指當(dāng)?shù)谝淮卧L問網(wǎng)頁時(shí),瀏覽器會(huì)將這些網(wǎng)頁緩存到本地,當(dāng)下一次再訪問這些被緩存的網(wǎng)頁時(shí),瀏覽器就會(huì)直接從本地讀取這些網(wǎng)頁的內(nèi)容,而無需再從網(wǎng)絡(luò)上獲取2017-11-11阿里資深技術(shù)專家:在各階段中3年經(jīng)驗(yàn)的java程序員應(yīng)該具備哪些技術(shù)能力
這篇文章主要介紹了阿里資深技術(shù)專家:在各階段中3年經(jīng)驗(yàn)的java程序員應(yīng)該具備哪些技術(shù)能力,本文給大家列舉了一些內(nèi)容,大家可以根據(jù)自己需要有方法的掌握,感興趣的朋友跟隨小編一起看看吧2020-07-07