管道基础大数据平台系统开发-【后端】-Server
13693261870
2024-02-29 36b2f13fff583e0dbc94ee40e9fe4ae6e75af4e0
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.lf.server.helper;
 
import com.lf.server.config.PropertiesConfig;
import com.lf.server.entity.all.SettingData;
import com.lf.server.entity.all.StaticData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.net.URL;
import java.util.Date;
 
/**
 * 路径帮助类
 * @author WWW
 */
@Component
public class PathHelper {
    @Autowired
    private PropertiesConfig config;
 
    private static int downloadPath = 1;
 
    private static int uploadPath = 1;
 
    private final static Log log = LogFactory.getLog(PathHelper.class);
 
    public PropertiesConfig getConfig() {
        return config;
    }
 
    public static int getDownloadPath() {
        return downloadPath;
    }
 
    public static int getUploadPath() {
        return uploadPath;
    }
 
    /**
     * 初始化
     */
    public void init() {
        downloadPath = getSubPath(config.getDownloadPath(), downloadPath);
        uploadPath = getSubPath(config.getUploadPath(), uploadPath);
    }
 
    private static int getSubPath(String parentPath, int subPath) {
        while (true) {
            String path = parentPath + File.separator + subPath;
 
            File file = new File(path);
            if (!file.exists() && !file.isDirectory()) {
                file.mkdirs();
                return subPath;
            }
 
            File[] files = file.listFiles();
            if (null == files || files.length < SettingData.MAX_FILES) {
                return subPath;
            }
 
            subPath++;
        }
    }
 
    /**
     * 获取下载完整目录
     */
    public String getDownloadFullPath() {
        downloadPath = getSubPath(config.getDownloadPath(), downloadPath);
 
        return config.getDownloadPath() + File.separator + downloadPath;
    }
 
    /**
     * 获取上传完整目录
     */
    public String getUploadFullPath() {
        uploadPath = getSubPath(config.getUploadPath(), uploadPath);
 
        return config.getUploadPath() + File.separator + uploadPath;
    }
 
    /**
     * 获取临时路径
     */
    public String getTempPath() {
        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
        String path = config.getTempPath() + File.separator + tempName;
 
        File file = new File(path);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
 
        deleteOldPath(config.getTempPath());
 
        return path;
    }
 
    /**
     * 获取临时路径
     */
    public String getTempPath(String subPath) {
        if (!StringHelper.isEmpty(subPath)) {
            String path = config.getTempPath() + File.separator + subPath;
 
            File file = new File(path);
            if (file.exists() && file.isDirectory()) {
                return path;
            }
        }
 
        deleteOldPath(config.getTempPath());
 
        return getTempPath();
    }
 
    /**
     * 获取临时路径名称
     */
    public String getTempPathName() {
        String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date());
        String path = config.getTempPath() + File.separator + tempName;
 
        File file = new File(path);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
 
        deleteOldPath(config.getTempPath());
 
        return tempName;
    }
 
    /**
     * 删除旧路径
     */
    public void deleteOldPath(String tempPath) {
        try {
            double ran = Math.random() * 99;
            if (ran < StaticData.D90) {
                return;
            }
 
            File file = new File(tempPath);
            String str = StringHelper.YMD2_FORMAT.format(new Date());
 
            File[] files = file.listFiles();
            if (null != files) {
                for (File f : files) {
                    if (f.getPath().contains(str)) {
                        continue;
                    }
 
                    FileHelper.deleteFiles(f);
                }
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
 
    /**
     * 获取static目录
     */
    public static String getStaticPath() {
        ClassLoader classLoader = PathHelper.class.getClassLoader();
        URL url = classLoader.getResource("");
        String path = url.getPath();
 
        return path.substring(0, path.indexOf("/target/classes")) + "/src/main/resources/static/";
    }
}