guohongjin
2024-05-15 5b7639f0bd9e056738ec15100ed0532e965c6cd5
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
package cn.stylefeng.guns.utils;
 
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import org.apache.commons.io.FilenameUtils;
 
import java.io.File;
import java.io.IOException;
 
 
public class UploadUtil {
 
    //服务器存储地址
//    private static String rootPath  = "/www/wwwroot/upload";
    private static String rootPath  = "/file/";
 
    //类型
    private static String type = "/guns" ;
 
 
    //模块
//    private static String modelPath = "/store";
    private static String modelPath = "/public";
 
    //扩展名
    private static String extStr = "png,jpg";
 
    //文件大小上限
    private static int size = 2;
 
    public static String getRootPath() {
        return rootPath;
    }
 
    public static void setRootPath(String rootPath) {
        UploadUtil.rootPath = (rootPath + "/").replace(" ", "").replace("//", "/");
    }
 
    public static String getType() {
        return type;
    }
 
    public static void setType(String type) {
        UploadUtil.type = type + "/";
    }
 
    public static String getModelPath() {
        return modelPath;
    }
 
    public static void setModelPath(String modelPath) {
        UploadUtil.modelPath = modelPath + "/";
    }
 
    public static String getExtStr() {
        return extStr;
    }
 
    public static void setExtStr(String extStr) {
        UploadUtil.extStr = extStr;
    }
 
    public static int getSize() {
        return size;
    }
 
    public static void setSize(int size) {
        UploadUtil.size = size;
    }
 
    /**
     * 根据文件的绝对路径创建一个文件对象.
     * @return 返回创建的这个文件对象
     * @author Mr.Zhang
     * @since 2020-05-08
     */
    public static File createFile(String filePath) throws IOException,Exception {
        // 获取文件的完整目录
        String fileDir = FilenameUtils.getFullPath(filePath);
        // 判断目录是否存在,不存在就创建一个目录
        File file = new File(fileDir);
        if (!file.isDirectory()) {
            //创建失败返回null
            if (!file.mkdirs()) {
                throw new Exception("文件目录创建失败...");
            }
        }
        // 判断这个文件是否存在,不存在就创建
        file = new File(filePath);
        if (!file.exists()) {
            if (!file.createNewFile()) {
                throw new Exception("目标文件创建失败...");
            }
        }
        return file;
    }
 
    /**
     * 生成文件文件名
     * @param fileName 文件名
     * @author Mr.Zhang
     * @since 2020-05-08
     */
    public static String getDestPath(String fileName) {
        //规则:  子目录/年/月/日.后缀名
        return getServerPath() + fileName;
    }
 
    public static String fileName(String extName){
        return UUID.randomUUID().toString() + RandomUtil.randomString(10) + "." + extName;
    }
 
    /**
     * 生成文件在的实际的路径
     * @author Mr.Zhang
     * @since 2020-05-08
     */
    public static String getServerPath() {
        // 文件分隔符转化为当前系统的格式
        return FilenameUtils.separatorsToSystem( getRootPath() + getWebPath());
    }
 
    /**
     * web目录可访问的路径
     * @author Mr.Zhang
     * @since 2020-05-08
     */
    public static String getWebPath() {
        // 文件分隔符转化为当前系统的格式
        return getModelPath() + DateUtil.today().replace("-", "/") + "/";
//        return getType() + getModelPath() + DateUtil.nowDate(Constants.DATE_FORMAT_DATE).replace("-", "/") + "/";
    }
}