java 读取jar包中的文件
生活随笔
收集整理的這篇文章主要介紹了
java 读取jar包中的文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 項目resource中文件路徑和jar包中文件路徑的區別
- 正常讀取
- jar包讀取
- 完整代碼:
項目resource中文件路徑和jar包中文件路徑的區別
打成jar包后,是一個整體的文件。
正常讀取
InputStream inputStream = new FileInputStream("src/main/resources/invoiceTemplate.xlsx");jar包讀取
InputStream inputStream = this.getClass().getResourceAsStream("/invoiceTemplate.xlsx");完整代碼:
@GetMapping("/invoiceTemplateDownload2")public void templateDownload2(HttpServletResponse response) throws IOException {response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");String fileName = URLEncoder.encode("模板", "UTF-8").replaceAll("\\+", "%20");response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");InputStream inputStream = this.getClass().getResourceAsStream("/template.xlsx");byte[] byteArray = IoUtils.toByteArray(inputStream);inputStream.close();response.getOutputStream().write(byteArray);}[Java基礎] 深入jar包:從jar包中讀取資源文件
總結
以上是生活随笔為你收集整理的java 读取jar包中的文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Guns 代码生成演示_入门试炼02
- 下一篇: Linux 启动mysql提示表不存在