MCPcopy Create free account
hub / github.com/diyhi/bbs / copyResourceDirectory

Method copyResourceDirectory

src/main/java/cms/utils/FileUtil.java:469–545  ·  view source on GitHub ↗

复制资源目录(spring boot 打包的资源目录下的文件 复制到外部目录) @param resDirPath 源目录路径 WEB-INF/foregroundView/templates @param distFolder 目标文件夹 WEB-INF/foregroundView/templates

(String resDirPath, String distFolder)

Source from the content-addressed store, hash-verified

467 * @param distFolder 目标文件夹 WEB-INF/foregroundView/templates
468 */
469 public static boolean copyResourceDirectory(String resDirPath, String distFolder){
470 boolean falg = true;
471 try {
472 Resource[] resources = new PathMatchingResourcePatternResolver().
473 getResources(ResourceUtils.CLASSPATH_URL_PREFIX + resDirPath+"/**");
474
475 String formatRootPath = FileUtil.toLeftSlant(PathUtil.rootPath());
476
477 // 遍历文件内容
478 for(Resource resource : resources) {
479 if("jar".equals(resource.getURL().getProtocol())){//jar:开头
480 //jar:file:/D:/test/test.jar!/BOOT-INF/classes!/WEB-INF/foregroundView/templates/default/wap/userLoginLogList.html
481 String decodePath = URLDecoder.decode(resource.getURL().getPath(), StandardCharsets.UTF_8);//解决中文文件名乱码
482
483 //源相对路径
484 String sourceRelativePath = StringUtils.substringAfter(StringUtils.substringAfterLast(decodePath, "!/"), resDirPath);//default/pc/addPrivateMessage.html
485 //目标路径
486 String distPath = PathUtil.defaultExternalDirectory()+File.separator+FileUtil.toSystemPath(distFolder+"/"+sourceRelativePath);
487
488 if (resource.getURL().getPath().endsWith("/")) { //如果是文件夹
489 Path path = Paths.get(distPath);
490 if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {//目录不存在
491 try {
492 Files.createDirectories(path);
493 } catch (IOException e) {
494 falg = false;
495 if (logger.isErrorEnabled()) {
496 logger.error("生成文件夹异常 "+distPath,e);
497 }
498 }
499 }
500
501 }else{//如果是文件
502 org.apache.commons.io.FileUtils.copyInputStreamToFile(resource.getInputStream(), Paths.get(distPath).toFile());
503
504 }
505 }else{//file:开头
506 String decodePath = URLDecoder.decode(resource.getURL().getPath(), StandardCharsets.UTF_8);//解决中文文件名乱码
507
508 //源相对路径
509 String sourceRelativePath = StringUtils.substringAfter(decodePath, formatRootPath+"/"+resDirPath+"/");//default/pc/addPrivateMessage.html
510 //目标路径
511 String distPath = PathUtil.defaultExternalDirectory()+File.separator+FileUtil.toSystemPath(distFolder+"/"+sourceRelativePath);
512
513 if (resource.getURL().getPath().endsWith("/")) { //如果是文件夹
514 Path path = Paths.get(distPath);
515 if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {//目录不存在
516 try {
517 Files.createDirectories(path);
518 } catch (IOException e) {
519 falg = false;
520 if (logger.isErrorEnabled()) {
521 logger.error("生成文件夹异常 "+distPath,e);
522 }
523 }
524 }
525
526 }else{//如果是文件

Callers 1

continueUpgradeMethod · 0.95

Calls 8

toLeftSlantMethod · 0.95
rootPathMethod · 0.95
toSystemPathMethod · 0.95
getMethod · 0.80
equalsMethod · 0.45
decodeMethod · 0.45
getPathMethod · 0.45

Tested by

no test coverage detected