当打成jar包后,如果想读取jar包内的文件,不能采用普通的文件路径的方式,因为在磁盘上是没有真实路径的,这样会报找不到文件的错误,正确的方式是采用流的方式,以下是经过验证的方式:
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-disposition", "attachment; filename=logo.png;filename*=utf-8''logo.png");
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/static/pic/logo.png");
FileCopyUtils.copy(inputStream, response.getOutputStream());
拓展
获取classpth路径:
this.getClass().getResource("/static/img/logo.png");
this.getClass().getResourceAsStream("/static/img/logo.png");
File file = org.springframework.util.ResourceUtils.getFile("classpath:/static/img/logo.png");
ClassPathResource classPathResource = new ClassPathResource("/static/img/logo.png");
classPathResource .getFile();
classPathResource .getInputStream();
|