Spring中加载外部资源文件的几种方式
来源:互联网 发布:淘宝聘用店主 编辑:程序博客网 时间:2024/04/30 23:50
文件资源操作
Spring中定义了一个Org.SpringFramework.Core.Io.Resource接口,Resource接口是为了加载不同类型的资源文件。
1.通过FileSystemResource加载系统绝对文件路径进行访问。
2.通过ClassPathResource加载类文件的方式进行访问。
package com.baobaotao.io;import java.io.IOException;import java.io.InputStream;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.FileSystemResource;public static void main(String[] args) {import org.springframework.core.io.Resource;public class FileSourceExample {try {// ① 使用系统文件路径方式加载文件String filePath = "D:/Spring/the/web/WEB-INF/config/file.txt";
Resource res2 = new ClassPathResource("config/file.txt");Resource res1 = new FileSystemResource(filePath);// ② 使用类路径方式加载文件InputStream ins1 = res1.getInputStream();System.out.println("res2:"+res2.getFilename());InputStream ins2 = res2.getInputStream();System.out.println("res1:"+res1.getFilename());} catch (IOException e) {e.printStackTrace();}}}
3.通过ServletContextResource方式进行访问web应用根目录。
4.通过属性文件PropertiesLoaderUtils方式允许您直接通过基于类路径的文件 地址加载属性资源,并能通过
getProperty(属性名)获取配置文件中的内容。
package com.baobaotao.io;import java.util.Properties;import org.springframework.core.io.support.PropertiesLoaderUtils;public class PropertiesLoaderUtilsExample {
//jdbc.properties 是位于类路径下的文件//jdbc.properties 是位于类路径下的文件public static void main(String[] args) throws Throwable {
Properties props = PropertiesLoaderUtils.loadAllProperties("jdbc.properties");
System.out.println(props.getProperty("jdbc.driverClassName"));System.out.println(props.getProperty("jdbc.driverClassName"));}}
5.通过ResourceUtils加载以file或classpath开头的文件
File files = ResourceUtils.getFile("classpath:config/file.txt"); System.out.println(files.isFile()); String httpFilePath = "file:D:/Spring/app/src/config/file.txt"; File httpFile = ResourceUtils.getFile(httpFilePath);6.通过FileCopyUtils可以方便对文件进行copy等操作
阅读全文
0 2
- Spring中加载外部资源文件的几种方式
- 系统框架中加载资源文件几种方式
- spring 配置加载外部文件的两种方式
- java加载资源文件的几种方式
- Unity中资源动态加载的几种方式比较
- Unity中资源动态加载的几种方式比较
- Unity中资源动态加载的几种方式比较
- spring加载hibernate映射文件的几种方式
- Spring加载Hibernate映射文件的几种方式
- spring加载hibernate映射文件的几种方式
- spring加载hibernate映射文件的几种方式
- spring加载hibernate映射文件的几种方式
- spring加载hibernate映射文件的几种方式
- spring加载hibernate映射文件的几种方式
- spring加载hibernate映射文件的几种方式
- spring加载hibernate映射文件的几种方式
- spring加载hibernate映射文件的几种方式
- spring加载hibernate映射文件的几种方式
- 关于机器学习比较好的几个网站
- USB驱动程序(一)————USB主机控制器驱动——OHCI分析
- 配置背景色
- (37)网格物体Actor
- 音视频聊天开发: 7 Android下ffmpeg+x264编译
- Spring中加载外部资源文件的几种方式
- unity读取txt配置文件
- integer overflow to buffer overflow --- 例子
- NULL知识
- 中值定理的选择和应用
- Android webview使用总结
- C语言基础系列-用mac开发c语言前奏-01
- Makefile(一)
- mariadb root忘记密码修改