Skip to content

Commit

Permalink
feature: 读取 "jar包/classpath" 目录下的资源文件
Browse files Browse the repository at this point in the history
  • Loading branch information
myoss committed Sep 6, 2020
1 parent 82ac7c4 commit 03e388e
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -37,6 +38,8 @@
import java.util.jar.JarFile;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;

import app.myoss.cloud.core.exception.BizRuntimeException;
import lombok.AccessLevel;
Expand Down Expand Up @@ -305,4 +308,35 @@ public static Map<String, InputStream> getFilesFromDirectory(String directory, b
public static Map<String, InputStream> getFilesFromDirectory(String directory) {
return getFilesFromDirectory(directory, false, false);
}

/**
* 读取 "jar包/classpath" 目录下的资源文件
*
* @param location 资源文件路径
* @return InputStream
*/
public static InputStream getResource(String location) {
ResourceLoader resourceLoader = new DefaultResourceLoader();
try {
return resourceLoader.getResource(location).getInputStream();
} catch (IOException e) {
throw new BizRuntimeException("get resource failed: " + location, e);
}
}

/**
* 读取 "jar包/classpath" 目录下的资源文件
*
* @param location 资源文件路径
* @return String
*/
public static String readFileContent(String location) {
ResourceLoader resourceLoader = new DefaultResourceLoader();
try {
InputStream inputStream = resourceLoader.getResource(location).getInputStream();
return StreamUtil.copyToString(inputStream, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new BizRuntimeException("get resource failed: " + location, e);
}
}
}

0 comments on commit 03e388e

Please sign in to comment.