SpringBoot读取resources下的文件以及resources的资源路径

简介: SpringBoot读取resources下的文件以及resources的资源路径

1.这种可以 但是在容器中获取不到(以下几种都可以只要不在容器)。

InputStream  inputStream = this.getClass().getResourceAsStream("/static/imgs/aha.png");

 

Properties pps = new Properties();
File file = ResourceUtils.getFile("classpath:defult.properties");
pps.load(new FileReader(file));

 

Properties pps = new Properties();
InputStream stream = getClass()
                    .getClassLoader()
                    .getResourceAsStream("defult.properties"); 
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
pps.load(br);
//获取resources下文件夹路径
File directory = new File("../项目名/src/main/resources");
String reportPath = directory.getCanonicalPath();
String resource =reportPath+"\\files\\**";
//resource就是所需要的路径 eg: resource="D:\项目名\src\main\resources\files\****"

 

2.容器和服务器中都可以获取

package com.bme.shed.service;
 
 
 
import com.bme.shed.BmeShedSimulateServiceApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
 
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class RcvCentInfoParse {
 
    @javax.annotation.Resource
    ResourceLoader resourceLoader;
 
    @Test
    public void testReaderFile() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:dsp.json");
        InputStream is = resource.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        while((data = br.readLine()) != null) {
            System.out.println(data);
        }
 
        br.close();
        isr.close();
        is.close();
    }
 
 
}


相关文章
|
22天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的高校实验室资源综合管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的高校实验室资源综合管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1天前
|
前端开发 安全 Java
实现Spring Boot中的文件分片上传通常涉及到以下几个步骤和考虑的关键点
实现Spring Boot中的文件分片上传通常涉及到以下几个步骤和考虑的关键点
12 2
|
1天前
|
Java
基于SpringBoot的餐厅会员管理信息系统【程序资源下载】
基于SpringBoot的餐厅会员管理信息系统【程序资源下载】
6 1
|
8天前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp小程序的线上学习资源智能推荐系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp小程序的线上学习资源智能推荐系统附带文章源码部署视频讲解等
17 5
|
1月前
|
安全 JavaScript Java
springboot实现文件防盗链设计
`shigen`,一位专注于Java、Python、Vue和Shell的博主,分享成长和技术。近期将探讨SpringBoot实现图片防盗链,通过限制`Referer`防止资源被盗用。基础版通过`WebMvcConfigurer`配置静态资源,升级版添加拦截器检查`Referer`,确保请求来源合法性。详细代码实现和案例可在文中链接找到。一起学习,每天进步!
64 14
springboot实现文件防盗链设计
|
27天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的教学资源库附带文章和源代码
基于SpringBoot+Vue的教学资源库附带文章和源代码
20 2
|
22天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的小码创客教育教学资源库的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的小码创客教育教学资源库的详细设计和实现(源码+lw+部署文档+讲解等)
|
22天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的幼儿资源互助共享平台的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的幼儿资源互助共享平台的详细设计和实现(源码+lw+部署文档+讲解等)
|
29天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的教学资源库的设计与实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue的教学资源库的设计与实现(源码+lw+部署文档+讲解等)
|
缓存 前端开发 Java
SpringBoot 缓存&资源优化
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36367789/article/details/81711179 页面缓存 1.
1389 0