iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >springboot打成jar后获取classpath下文件失败怎么办
  • 238
分享到

springboot打成jar后获取classpath下文件失败怎么办

2023-06-20 21:06:19 238人浏览 安东尼
摘要

这篇文章主要为大家展示了“SpringBoot打成jar后获取classpath下文件失败怎么办”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“springboot打成jar后获取classpat

这篇文章主要为大家展示了“SpringBoot打成jar后获取classpath下文件失败怎么办”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“springboot打成jar后获取classpath下文件失败怎么办”这篇文章吧。

springboot打成jar后获取classpath下文件

代码如下:

ClassPathResource resource = new ClassPathResource("app.keystore");File file = resource.getFile();FileUtils.readLines(file).forEach(System.out::println);

解决方式如下:

1. Spring framework
String data = "";ClassPathResource cpr = new ClassPathResource("static/file.txt");try {    byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());    data = new String(bdata, StandardCharsets.UTF_8);} catch (ioException e) {    LOG.warn("IOException", e);}
2.use a file
ClassPathResource classPathResource = new ClassPathResource("static/something.txt"); InputStream inputStream = classPathResource.getInputStream();File somethingFile = File.createTempFile("test", ".txt");try {    FileUtils.copyInputStreamToFile(inputStream, somethingFile);} finally {    IOUtils.closeQuietly(inputStream);}
Resource resource = new ClassPathResource("data.sql");BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));reader.lines().forEach(System.out::println);
String content = new ClassPathResourceReader("data.sql").getContent();
@Value("${resourceLoader.file.location}")    @Setter    private String location;     private final ResourceLoader resourceLoader; public void readallfilesfromresources() {       Resource[] resources;         try {            resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:" + location + "*.properties</include>          <include>***.properties</include>          <include>***.yml</include>          <include>**/Dockerfile</include>          <include>**/*.xlsx</include>        </includes>        <!--是否替换资源中的属性-->        <filtering>false</filtering>      </resource>  </resources></build>

重新启动,再到classPath下看,发现有了这个文件了,同时也能获取了。如果文件名为中文的话就会出现乱码的情况。

怎么解决文件名中文乱码?

Access-Control-Allow-Origin →*Access-Control-Allow-Credentials →trueAccess-Control-Allow-Headers →accessToken,Access-Control-Allow-Origin,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-HeadersAccess-Control-Allow-Methods →POST,GET,PUT,PATCH,DELETE,OPTIONS,HEADAccess-Control-Max-Age →360Content-disposition →attachment; filename="????????-??????????.xlsx"Content-Type →application/octet-stream;charset=UTF-8Transfer-Encoding →chunkedDate →Fri, 19 Apr 2019 06:47:34 GMT

上面是使用postman测试中文乱码response响应头相关信息。

后面怎么改都乱码,然后我就试着到浏览器中请求试试,结果发现只是postman的原因,只要文件名编码跟返回内容编码一致("Content-disposition"和“ContentType”)就行:

this.response.setContentType("application/octet-stream; charset=iso-8859-1");StringBuffer contentDisposition = new StringBuffer("attachment; filename=\"");String fileName = file.getName();contentDisposition.append(fileName).append("\"");//设置文件名编码String contentDispositionStr = new String(contentDisposition.toString().getBytes(), "iso-8859-1");this.response.setHeader("Content-disposition", contentDispositionStr);

以上是“springboot打成jar后获取classpath下文件失败怎么办”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!

--结束END--

本文标题: springboot打成jar后获取classpath下文件失败怎么办

本文链接: https://www.lsjlt.com/news/300201.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作