前段时间,在基于springboot开发过程中,遇到一个问题:程序需要读取resource下的某个目录的全部文件,而且需要能以File的方式读取。但是, 打包后,springboot项目就成了jar包了,读取文件,会报错: ... cannot be resolved to absolute file path because it does not reside in the file system:jar:file: .... 。
一般情况下,我们读取resource下的某个文件,可以这样通过IO流的方式读取:
Resource resource =new ClassPathResource(fileName);
InputStream is = resource.getInputStream();
当时,有些时候,需要使用File:resource.getFile() ,这时,在jar包下就会报上面的错误。可是,如果是文件夹怎么办呢?
经过网上的一番查询,确定了一个方案:把jar包中的文件,先存到一个临时文件夹下,然后通过临时文件夹,可以实现以File的方式读取,文件读取完成后,删除临时文件目录。
文件复制代码如下:
import lombok.extern.log4j.Log4j2;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.*;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@Log4j2
public class FileUtils {
/**
* 复制文件到目标目录
* @param resourcePath resource的文件夹路径
* @param tmpDir 临时目录
* @param fileType 文件类型
*/
public static void copyJavaResourceDir2TmpDir(String resourcePath, String tmpDir, FileType fileType) {
Map<String, Object> fileMap = new HashMap<>();
if (resourcePath.endsWith("/")) {
resourcePath = resourcePath.substring(0, resourcePath.lastIndexOf("/"));
}
try {
Enumeration resources = null;
try {
resources = Thread.currentThread().getContextClassLoader().getResources(resourcePath);
} catch (Exception ex) {
ex.printStackTrace();
}
if (resources == null || !resources.hasMoreElements()) {
resources = FileUtils.class.getClassLoader().getResources(resourcePath);
}
while (resources.hasMoreElements()) {
URL resource = (URL) resources.nextElement();
if (resource.getProtocol().equals("file")) { // resource是文件
continue;
}
String[] split = resource.toString().split(":");
String filepath = split[2];
if (OperatingSystem.isWindows()) {
filepath = filepath + ":" + split[3];
}
String[] split2 = filepath.split("!");
String zipFileName = split2[0];
ZipFile zipFile = new ZipFile(zipFileName);
Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String entryName = entry.getName();
if (entry.isDirectory()) {
continue;
}
if (entryName.contains(resourcePath) && entryName.endsWith(fileType.toString().toLowerCase())) {
String dir = entryName.substring(0, entryName.lastIndexOf("/"));
if (!dir.endsWith(resourcePath)) { // 目标路径含有子目录
dir = dir.substring(dir.indexOf(resourcePath) + resourcePath.length() + 1);
if (dir.contains("/")) { // 多级子目录
String[] subDir = dir.split("/");
Map<String, Object> map = fileMap;
for (String d : subDir) {
map = makeMapDir(map, d);
}
map.putAll(readOneFromJar(zipFile.getInputStream(entry), entryName));
} else { //一级子目录
if (fileMap.get(dir) == null) {
fileMap.put(dir, new HashMap<String, Object>());
}
((Map<String, Object>) fileMap.get(dir))
.putAll(readOneFromJar(zipFile.getInputStream(entry), entryName));
}
} else { // 目标路径不含子目录
fileMap.putAll(readOneFromJar(zipFile.getInputStream(entry), entryName));
}
}
}
}
} catch (Exception e) {
log.error("读取resource文件异常:", e);
} finally {
try {
// 写到目标缓存路径
createFile(fileMap, tmpDir);
} catch (Exception e) {
log.error("创建临时文件异常:", e);
}
}
}
private static void createFile(Map<String, Object> fileMap, String targetDir) {
fileMap.forEach((key, value) -> {
if (value instanceof Map) {
createFile((Map<String, Object>) value, targetDir + File.separator + key);
} else {
createNewFile(targetDir + File.separator + key, value.toString());
}
});
}
public static void createNewFile(String filePath, String value) {
try {
File file = new File(filePath);
if (!file.exists()) {
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
file.createNewFile();
}
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8")));
out.write(value);
out.flush();
out.close();
} catch (Exception e) {
log.error("创建文件异常:", e);
}
}
private static Map<String, Object> makeMapDir(Map<String, Object> fileMap, String d) {
if (fileMap.get(d) == null) {
fileMap.put(d, new HashMap<String, Object>());
}
return (Map<String, Object>) fileMap.get(d);
}
public static Map<String, String> readOneFromJar(InputStream inputStream, String entryName) {
Map<String, String> fileMap = new HashMap<>();
try (Reader reader = new InputStreamReader(inputStream, "utf-8")) {
StringBuilder builder = new StringBuilder();
int ch = 0;
while ((ch = reader.read()) != -1) {
builder.append((char) ch);
}
String filename = entryName.substring(entryName.lastIndexOf("/") + 1);
fileMap.put(filename, builder.toString());
} catch (Exception ex) {
log.error("读取文件异常:", ex);
}
return fileMap;
}
public enum FileType {
XSD, TXT, XLS, XLSX, DOC, DOCX, XML, SQL, PROPERTIES, SH
}
}
程序调用代码如下:
String tmpDir = System.getProperty("user.dir") + File.separator +"tmp";
try {
FileUtils.copyJavaResourceDir2TmpDir("xsd", tmpDir, FileUtils.FileType.XSD);
loadXsd(new File(tmpDir));
} catch (Exception e) {
logger.error("加载xsd文件异常:", e);
} finally {
FileUtils.delete(tmpDir);
}
这样,就不用每次部署实例的时候,都把需要文件和jar一起部署了,方便多了。
OperatingSystem是Filnk中copy过来的代码
网友评论