广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot打成jar后获取classpath下文件失败的解决方案
  • 785
分享到

springboot打成jar后获取classpath下文件失败的解决方案

2024-04-02 19:04:59 785人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

SpringBoot打成jar后获取classpath下文件 代码如下: ClassPathResource resource = new ClassPathResource("

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 →true
Access-Control-Allow-Headers →accessToken,Access-Control-Allow-Origin,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers
Access-Control-Allow-Methods →POST,GET,PUT,PATCH,DELETE,OPTIONS,HEAD
Access-Control-Max-Age →360
Content-disposition →attachment; filename="????????-??????????.xlsx"
Content-Type →application/octet-stream;charset=UTF-8
Transfer-Encoding →chunked
Date →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);

到此全部结束!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: springboot打成jar后获取classpath下文件失败的解决方案

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

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

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

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

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

  • 微信公众号

  • 商务合作