java 读取jar包中的文件
生活随笔
收集整理的這篇文章主要介紹了
java 读取jar包中的文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 項(xiàng)目resource中文件路徑和jar包中文件路徑的區(qū)別
- 正常讀取
- jar包讀取
- 完整代碼:
項(xiàng)目resource中文件路徑和jar包中文件路徑的區(qū)別
打成jar包后,是一個(gè)整體的文件。
正常讀取
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基礎(chǔ)] 深入jar包:從jar包中讀取資源文件
總結(jié)
以上是生活随笔為你收集整理的java 读取jar包中的文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Guns 代码生成演示_入门试炼02
- 下一篇: 如何写一个bat批处理自动上传文件到FT