FreeMarkerUtil工具类
package org.jawa.common.utils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.*;
import java.util.ArrayList;
/**
* 复制resource文件夹
*
* @Author zheng
* @Date 2023/09/03 19:58:41
* @Version 1.0
*/
public class FreeMarkerUtil {
private static ArrayList paths=new ArrayList();
private static ArrayList noCoberpaths=new ArrayList();
/**
* 复制path目录下所有文件,覆盖
* @param path 文件目录 不能以/开头
* @param newpath 新文件目录
*/
public static void BatCopyFileFromJarCover(String path,String newpath) {
if (!new File(newpath).exists()){
new File(newpath).mkdir();
}
if(path.contains("\\")){
path=path.replace("\\","/");
}
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
//获取所有匹配的文件
Resource[] resources = resolver.getResources("classpath:"+path+"/**/");
//打印有多少文件
for(int i=0;i<resources.length;i++) {
Resource resource=resources[i];
String[] descriptions = resource.getDescription().substring(0,resource.getDescription().length()-1).split(path.substring(1,path.length())+"/");
if(descriptions.length>1){
if(descriptions[1].contains("/")){
if(descriptions[1].substring(descriptions[1].length()-1).equals("/")){
String[] afterDescriptions = descriptions[1].split("/");
String childPath=path;
for (int j=0;j<afterDescriptions.length;j++){
childPath=childPath+"/"+afterDescriptions[j];
if(!paths.contains(childPath)){
paths.add(childPath);
if (!new File(newpath+"/"+afterDescriptions[j]).exists()){
new File(newpath+"/"+afterDescriptions[j]).mkdir();
}
File fileFromClassPath01 = FreeMarkerUtil.getFileFromClassPath(path+"/"+afterDescriptions[j]); //复制目录
FreeMarkerUtil.BatCopyFileFromJarCover(fileFromClassPath01.toString(),newpath+"/"+afterDescriptions[j]);
}
}
}
}else {
makeFile(newpath+"/"+descriptions[1]);
InputStream stream = resource.getInputStream();
write2File(stream, newpath+"/"+descriptions[1]);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制path目录下所有文件,不覆盖
* @param path 文件目录 不能以/开头
* @param newpath 新文件目录
*/
public static void BatCopyFileFromJar(String path,String newpath) {
if (!new File(newpath).exists()){
new File(newpath).mkdir();
}
if(path.contains("\\")){
path=path.replace("\\","/");
}
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
//获取所有匹配的文件
Resource[] resources = resolver.getResources("classpath:"+path+"/**/");
//打印有多少文件
for(int i=0;i<resources.length;i++) {
Resource resource=resources[i];
String[] descriptions = resource.getDescription().substring(0,resource.getDescription().length()-1).split(path.substring(1,path.length())+"/");
if(descriptions.length>1){
if(descriptions[1].contains("/")){
if(descriptions[1].substring(descriptions[1].length()-1).equals("/")){
String[] afterDescriptions = descriptions[1].split("/");
String childPath=path;
for (int j=0;j<afterDescriptions.length;j++){
childPath=childPath+"/"+afterDescriptions[j];
if(!noCoberpaths.contains(childPath)){
noCoberpaths.add(childPath);
if (!new File(newpath+"/"+afterDescriptions[j]).exists()){
new File(newpath+"/"+afterDescriptions[j]).mkdir();
}
File fileFromClassPath01 = FreeMarkerUtil.getFileFromClassPath(path+"/"+afterDescriptions[j]); //复制目录
FreeMarkerUtil.BatCopyFileFromJar(fileFromClassPath01.toString(),newpath+"/"+afterDescriptions[j]);
}
}
}
}else {
File f = new File(newpath+"/"+descriptions[1]);
if(!f.exists()) {
makeFile(newpath+"/"+descriptions[1]);
InputStream stream = resource.getInputStream();
write2File(stream, newpath+"/"+descriptions[1]);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建文件
* @param path 全路径 指向文件
* @return
*/
public static boolean makeFile(String path) {
File file = new File(path);
if(file.exists()) {
return false;
}
if (path.endsWith(File.separator)) {
return false;
}
if(!file.getParentFile().exists()) {
if(!file.getParentFile().mkdirs()) {
return false;
}
}
try {
if (file.createNewFile()) {
return true;
} else {
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 输入流写入文件
* @param is 输入流
* @param filePath 文件保存目录路径
* @throws IOException
*/
public static void write2File(InputStream is, String filePath) throws IOException {
OutputStream os = new FileOutputStream(filePath);
int len = 8192;
byte[] buffer = new byte[len];
while ((len = is.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, len);
}
os.close();
is.close();
}
/**
*处理异常报错(springboot读取classpath里的文件,解决打jar包java.io.FileNotFoundException: class path resource cannot be opened)
**/
public static File getFileFromClassPath(String path){
File targetFile = new File(path);
if(!targetFile.exists()){
if(targetFile.getParent()!=null){
File parent=new File(targetFile.getParent());
if(!parent.exists()){
parent.mkdirs();
}
}
InputStream initialStream=null;
OutputStream outStream =null;
try {
Resource resource=new ClassPathResource(path);
//注意通过getInputStream,不能用getFile
initialStream=resource.getInputStream();
byte[] buffer = new byte[initialStream.available()];
initialStream.read(buffer);
outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
} catch (IOException e) {
} finally {
if (initialStream != null) {
try {
initialStream.close(); // 关闭流
} catch (IOException e) {
}
}
if (outStream != null) {
try {
outStream.close(); // 关闭流
} catch (IOException e) {
}
}
}
}
return targetFile;
}
}
子文件夹的名字试了很多种方法,只能通过getDescription()获取,然后对其进行处理!!
测试接口
/**
* 测试
*/
@GetMapping(value = "/test")
public AjaxResult taskList() {
File fileFromClassPath01 = FreeMarkerUtil.getFileFromClassPath("/software/htem/static_hcp/Volume_1/Dst_01"); //复制目录
FreeMarkerUtil.BatCopyFileFromJar(fileFromClassPath01.toString(),"F:\\htem_task\\test");
return AjaxResult.success();
}
该方法是在下面的博客基础上进行完善的,增加了支持包含子文件夹的功能!!
https://blog.csdn.net/weixin_44975322/article/details/121677386