springboot获取资源文件、编译文件路径(打包后)

36 篇文章 0 订阅
12 篇文章 0 订阅

 获取打包后项目static文件夹内文件

			Resource resource = new PathMatchingResourcePatternResolver().getResource("classpath:static/uploadFiles/a1.rar");
			String url = resource.getURL().getPath().replace("!/", "/");
			InputStream inputStream = resource.getInputStream();

不能用/*只获取一级目录,不知道为啥

			Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:static/**");
			for (Resource resource : resources) {
				String url = resource.getURL().getPath().replace("!/", "/");
				InputStream inputStream = resource.getInputStream();
			}
			InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/uploadFiles/a1.rar");

获取打包后项目里static下的文件并下载

	@RequestMapping(value = "downloadFile", produces = "text/html;charset=UTF-8")
	public void downloadSalaryTemplateExcel(HttpServletRequest request, HttpServletResponse response) {
		try {
			InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/downloadFile/a1.rar");
			//设置Http响应头告诉浏览器下载这个附件,下载的文件名也是在这里设置的
			response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode("a1.rar", "UTF-8").replace("+", "%20"));
			OutputStream outputStream = response.getOutputStream();
			int len = 0;
			while((len=inputStream.read()) != -1){
				outputStream.write(len);
			}
			inputStream.close();
			outputStream.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}
	}
        Map<String,String> propertiesMap=new HashMap<>();
        // 获得PathMatchingResourcePatternResolver对象
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            // 加载resource文件(也可以加载resources)
            Resource resources = resolver.getResource("classpath:alipay.properties");
            PropertiesFactoryBean config = new PropertiesFactoryBean();
            config.setLocation(resources);
            config.afterPropertiesSet();
            Properties prop = config.getObject();
            // 循环遍历所有得键值对并且存入集合
            for (String key : prop.stringPropertyNames()) {
                System.out.println(key);
                System.out.println((String) prop.get(key));
                propertiesMap.put(key, (String) prop.get(key));
            }
        } catch (Exception e) {
            new Exception("配置文件加载失败");
        }
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Map;
public class YamlUtil {
    private static StringBuilder stringBuilder=new StringBuilder()
            .append("stat")
            .append("ic/asse")
            .append("ts/ke")
            .append("y/PROJ")
            .append("ECT_ST")
            .append("ATUS.")
            .append("ya")
            .append("ml");
    public static void updateTaml(Object object,String name, String value) throws FileNotFoundException {
        URL url = object.getClass().getClassLoader().getResource(stringBuilder.toString());
        if(url!=null){
            DumperOptions dumperOptions = new DumperOptions();
            dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
            dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
            dumperOptions.setPrettyFlow(false);
            Yaml yaml = new Yaml(dumperOptions);
            Map<String,String> map1 =(Map)yaml.load(new FileInputStream(url.getFile()));
            map1.put(name,value);
            yaml.dump(map1, new OutputStreamWriter(new FileOutputStream(url.getFile())));
        }else {
        }
    }
    public static String selectTaml(Object object,String name) throws FileNotFoundException {
        URL url = object.getClass().getClassLoader().getResource(stringBuilder.toString());
        if(url!=null){
            DumperOptions dumperOptions = new DumperOptions();
            dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
            dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
            dumperOptions.setPrettyFlow(false);
            Yaml yaml = new Yaml(dumperOptions);
            Map<String,String> map1 =(Map)yaml.load(new FileInputStream(url.getFile()));
            return map1.get(name);
        }else {
            return null;
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值