java_resource_dirs_列出JAR的resources文件夹中所有子目录的名称

我使用maven将我的Spring Boot项目打包到一个JAR中。我想读取resources文件夹中“static/foo”下所有目录的名称。

使用Apache Commons库尝试了以下操作:

String fooPath = "static/foo/";

List fooFolders = IOUtils.readLines(this.getClass().getClassLoader().getResourceAsStream(fooPath), Charsets.UTF_8);

// The fooFolders list is empty ...

更新

String fooPath = "static/foo";

URL url = Thread.currentThread().getContextClassLoader().getResource(fooPath);

// Not running from JAR

if (url.getProtocol().equals("file"))

{

try {

// Get list of subdirectories' folder names

List fooFolders = IOUtils.readLines(this.getClass().getClassLoader().getResourceAsStream(fooPath), Charsets.UTF_8);

// Loop subdirectories

for (String fooFolder : fooFolders) {

// The current subdirectory path

String fooFolderPath = fooPath + "/" + fooFolder;

// Loop all files in this subdirectory, if needed

List fooFiles = IOUtils.readLines(this.getClass().getClassLoader().getResourceAsStream(fooFolderPath), Charsets.UTF_8);

for (String fooFile : fooFiles) {

// The updated path of the file

String fooFilePath = fooFolderPath + "/" + fooFile;

// Read the file's content

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fooFilePath);

StringWriter writer = new StringWriter();

IOUtils.copy(inputStream, writer, Charsets.UTF_8);

String fileContent = writer.toString();

}

}

}

catch (IOException e) {

e.printStackTrace();

}

}

// Running from JAR

else if (url.getProtocol().equals("jar")) {

String dirname = fooPath + "/";

String path = url.getPath();

String jarPath = path.substring(5, path.indexOf("!"));

List fooFolders = new ArrayList();

HashMap> fooFiles = new HashMap>();

try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name()))) {

Enumeration entries = jar.entries();

while (entries.hasMoreElements()) {

JarEntry entry = entries.nextElement();

String jarEntryName = entry.getName();

String updated_dir_name = "BOOT-INF/classes/" + dirname;

// Only get files that are in the directory we require (fooPath)

if (jarEntryName.startsWith(updated_dir_name) && !dirname.equals(updated_dir_name)) {

// Get the resource URL

URL resourceURL = Thread.currentThread().getContextClassLoader().getResource(jarEntryName);

// Files only

if (!jarEntryName.endsWith("/")) {

// Split the foo number and the file name

String[] split = jarEntryName.split("/");

// First level subdirectories inside fooPath

// BOOT-INF/classes/static/foo/1/myfile.html

// We want to read the folder name "1"

String folderName = split[split.length - 2];

// If you want to read this file

// Read the file's content

// InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceURL);

}

}

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值