ResourceUtils.java
2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
*/
package com.jeesite.common.io;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import com.jeesite.common.lang.ExceptionUtils;
/**
* 资源供给类
* @author ThinkGem
* @version 2016-9-16
*/
public class ResourceUtils extends org.springframework.util.ResourceUtils {
private static ResourceLoader resourceLoader;
private static ResourcePatternResolver resourceResolver;
static{
resourceLoader = new DefaultResourceLoader();
resourceResolver = new PathMatchingResourcePatternResolver(resourceLoader);
}
/**
* 获取资源加载器(可读取jar内的文件)
* @author ThinkGem
*/
public static ResourceLoader getResourceLoader() {
return resourceLoader;
}
/**
* 获取ClassLoader
*/
public static ClassLoader getClassLoader() {
return resourceLoader.getClassLoader();
}
/**
* 获取资源加载器(可读取jar内的文件)
*/
public static Resource getResource(String location) {
return resourceLoader.getResource(location);
}
/**
* 获取资源文件流(用后记得关闭)
* @param location
* @author ThinkGem
* @throws IOException
*/
public static InputStream getResourceFileStream(String location) throws IOException{
Resource resource = resourceLoader.getResource(location);
return resource.getInputStream();
}
/**
* 获取资源文件内容
* @param location
* @author ThinkGem
*/
public static String getResourceFileContent(String location){
try(InputStream is = ResourceUtils.getResourceFileStream(location)){
return IOUtils.toString(is, "UTF-8");
}catch (IOException e) {
throw ExceptionUtils.unchecked(e);
}
}
/**
* Spring 搜索资源文件
* @param locationPattern
* @author ThinkGem
*/
public static Resource[] getResources(String locationPattern){
try {
Resource[] resources = resourceResolver.getResources(locationPattern);
return resources;
} catch (IOException e) {
throw ExceptionUtils.unchecked(e);
}
}
}