FileManangerBackup.java
2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.jflyfox.system.file.service.impl;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.jflyfox.system.config.ConfigCache;
import com.jflyfox.system.file.model.FileUploadBean;
import com.jflyfox.system.file.service.IFileBackup;
import com.jflyfox.system.file.util.FileUploadUtils;
import com.jflyfox.util.StrUtils;
/**
* 文件系统备份
*
* 2017年4月5日 上午3:06:20 flyfox 369191470@qq.com
*/
public class FileManangerBackup implements IFileBackup {
public boolean backup(FileUploadBean fileBean) {
String backupPath = ConfigCache.getValue("backup.filemanger.path"); // 备份路径
String webRootPath = FileUploadUtils.getRootPath(); // 项目路径
// 文件路径处理
String newFilePath = FileUploadUtils.rebuild(fileBean.getFactpath()).replace(webRootPath, "");
newFilePath = (newFilePath.startsWith("/") ? "" : "/") + newFilePath;
File srcFile = new File(fileBean.getFactpath());
String destPath = FileUploadUtils.rebuild(backupPath + newFilePath);
File destFile = new File(destPath);
try {
if (destFile.exists()) {
destFile.delete();
}
FileUtils.copyFile(srcFile, destFile);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public boolean restore(String projectPath) {
if (StrUtils.isEmpty(projectPath)) {
return false;
}
String srcPath = (projectPath.startsWith("/") ? "" : "/") + projectPath;
String webRootPath = FileUploadUtils.getRootPath(); // 项目路径
String projectFactPath = FileUploadUtils.rebuild(webRootPath + srcPath);
File destFile = new File(projectFactPath);
String backupPath = ConfigCache.getValue("backup.filemanger.path"); // 备份路径
String srcFactPath = FileUploadUtils.rebuild(backupPath + srcPath);
File srcFile = new File(srcFactPath);
// 已经存在不用恢复
if (destFile.exists()) {
return true;
}
// 备份文件都不存在,还恢复个P啊
if (!srcFile.exists()) {
return false;
}
try {
// 复制恢复
FileUtils.copyFile(srcFile, destFile);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}