java 乱码 找不到文件_springboot-项目获取resources下文件碰到的问题(classPath下找不到文件和文件名乱码)...

项目是spring-boot + spring-cloud 并使用maven 管理依赖。在springboot+maven项目下怎么读取resources下的文件实现文件下载?

怎么获取resources目录下的文件?(相对路径)

方法一:File sourceFile = ResourceUtils.getFile("classpath:templateFile/test.xlsx"); //这种方法在linux下无法工作

方法二:

Resource resource = new ClassPathResource("templateFile/test.xlsx"); File sourceFile = resource.getFile();

我使用的是第二种。

1 @PostMapping("/downloadTemplateFile")2 publicJSONData downloadTemplateFile(HttpServletResponse response) {3 String filePath = "templateFile/test.xlsx";4 Resource resource = new ClassPathResource(filePath);//用来读取resources下的文件

5 InputStream is = null;6 BufferedInputStream bis = null;7 OutputStream os = null;8 try{9 File file =resource.getFile();10 if (!file.exists()) {11 return new JSONData(false,"模板文件不存在");12 }13 is = newFileInputStream(file);14 os =response.getOutputStream();15 bis = newBufferedInputStream(is);16 //设置响应头信息

17 response.setCharacterEncoding("UTF-8");18 this.response.setContentType("application/octet-stream; charset=UTF-8");19 StringBuffer contentDisposition = new StringBuffer("attachment; filename=\"");20 String fileName = new String(file.getName().getBytes(), "utf-8");21 contentDisposition.append(fileName).append("\"");22 this.response.set

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值