java 读取jar包中的文件
文章目录项目resource中文件路径和jar包中文件路径的区别正常读取jar包读取完整代码:项目resource中文件路径和jar包中文件路径的区别打成jar包后,是一个整体的文件。正常读取InputStream inputStream = new FileInputStream("src/main/resources/invoiceTemplate.xlsx");jar包读取InputStre
·
项目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);
}
更多推荐
所有评论(0)