ws183
2025-04-28 b306d1106b915bb13fd7a02217ae9c65de2fd03d
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.ld.igds.util;
 
import com.ld.igds.data.ConfigData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.*;
import java.net.URL;
 
/**
 * @author chen
 */
@Slf4j
@Component
public class FilesUtil {
 
    @Resource
    private ConfigData configData;
 
    /**
     * 根据url地址下载保存图片
     *
     * @param imgUrl  图片地址,如:https://ip:port/imgUrl
     * @param path    图片保存路径,如:F:/IGDS/COMMON/
     * @param imgName 图片名称,如:img1.png
     */
    public static void downLoadImg(String imgUrl, String path, String imgName) {
        URL url = null;
        //创建一个输入流
        InputStream in = null;
        //创建输出流
        OutputStream os = null;
        try {
            url = new URL(imgUrl);
            in = new DataInputStream(url.openStream());
            os = new FileOutputStream(new File(path + imgName));
 
            ByteArrayOutputStream output = new ByteArrayOutputStream();
 
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            os.write(output.toByteArray());
            in.close();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 验证static/img/下有没有当前文件 有返回true,没有返回false
     *
     * @param fileName
     * @return
     */
    public static boolean checkImgFile(String fileName) {
        if (null == fileName) return false;
 
        try {
            ClassPathResource classPathResource = new ClassPathResource("static/img/" + fileName);
            log.debug("---获取IMG下面文件路径-{}", classPathResource.getURL().getPath());
            return classPathResource.exists();
        } catch (Exception e) {
            log.warn("---获取IMG下面文件路径失败:{}", e.getMessage());
            return false;
        }
    }
 
 
    /**
     * 根据文件路径+文件名创建
     *
     * @param path
     * @return
     */
    public static boolean createFile(String path) {
        if (path.contains(".")) {
            int partition1 = path.lastIndexOf("/");
            if (partition1 == -1) {
                partition1 = path.lastIndexOf("\\");
            }
            // 纯文件名
            String fileName = path.substring(partition1 + 1);
            String filePath = path.substring(0, partition1);
            return createNewFile(fileName, filePath);
        } else {
            return createCatalog(path);
        }
    }
 
    /**
     * 根据路径和文件名称创建
     *
     * @param fileName
     * @param filePath
     * @return
     */
    public static boolean createNewFile(String fileName, String filePath) {
        if (createCatalog(filePath)) {
            File f = new File(filePath, fileName);
            try {
                return f.createNewFile();
            } catch (IOException e) {
                new RuntimeException("创建文件失败");
                e.printStackTrace();
            }
        }
        return false;
 
    }
 
    /**
     * 创建文件夹
     *
     * @param path
     * @return
     */
    private static boolean createCatalog(String path) {
        File f = new File(path);
        if (!f.exists()) {
            return f.mkdirs();
        }
        return true;
    }
 
 
    /**
     * 根据组织编码获取当前用户的企业LOGO
     *
     * @param companyId
     * @return
     */
    public static String getLogoByCompanyId(String companyId) {
        String logoName = "logo-" + companyId + ".png";
        boolean isExists = FilesUtil.checkImgFile(logoName);
        if (isExists) return logoName;
        return "logo-default.png";
    }
 
    /**
     * 根据用户所在组织获取系统首页系统名称
     *
     * @param companyId
     * @return
     */
    public static String getLogoTitleByCompanyId(String companyId) {
        String logoName = "logo-title-" + companyId + ".png";
        boolean isExists = FilesUtil.checkImgFile(logoName);
        if (isExists) return logoName;
        return "logo-title-default.png";
    }
 
 
    /**
     * 根据组织编码获取当前用户的企业库区鸟瞰图
     *
     * @param companyId
     * @return
     */
    public static String getAerialByCompanyId(String companyId) {
        String logoName = "aerial-" + companyId + ".png";
        boolean isExists = FilesUtil.checkImgFile(logoName);
        if (isExists) return logoName;
        return "aerial-default.png";
    }
 
    /**
     * 根据当前人所在分库ID获取鸟瞰图
     *
     * @param deptId
     * @return
     */
    public static String getAerialByDeptId(String deptId) {
        String logoName = "aerial-" + deptId + ".png";
        boolean isExists = FilesUtil.checkImgFile(logoName);
        if (isExists) return logoName;
        return "aerial-default.png";
    }
 
 
    /**
     * 返回项目临时目录
     *
     * @param companyId
     * @return
     */
    public String getTempPath(String companyId) {
        String path = configData.getFilePath() + "TEMP/" + companyId;
 
        createCatalog(path);
 
        return path;
    }
 
    /**
     * 获取系统配置文件夹
     *
     * @return
     */
    public String getConfPath() {
        return configData.getFilePath() + "CONF";
    }
}