xiejun
2024-11-01 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.core.boot.file;
 
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.*;
 
/**
 * 文件工具类
 *
 * @author Chill
 */
public class BladeFileUtil {
 
    /**
     * 定义允许上传的文件扩展名
     */
    private static final HashMap<String, String> EXT_MAP = new HashMap<>();
    private static final String IS_DIR = "is_dir";
    private static final String FILE_NAME = "filename";
    private static final String FILE_SIZE = "filesize";
 
    /**
     * 图片扩展名
     */
    private static final String[] FILE_TYPES = new String[]{"gif", "jpg", "jpeg", "png", "bmp"};
 
    static {
        EXT_MAP.put("image", ".gif,.jpg,.jpeg,.png,.bmp,.JPG,.JPEG,.PNG");
        EXT_MAP.put("flash", ".swf,.flv");
        EXT_MAP.put("media", ".swf,.flv,.mp3,.mp4,.wav,.wma,.wmv,.mid,.avi,.mpg,.asf,.rm,.rmvb");
        EXT_MAP.put("file", ".doc,.docx,.xls,.xlsx,.ppt,.htm,.html,.txt,.zip,.rar,.gz,.bz2");
        EXT_MAP.put("allfile", ".gif,.jpg,.jpeg,.png,.bmp,.swf,.flv,.mp3,.mp4,.wav,.wma,.wmv,.mid,.avi,.mpg,.asf,.rm,.rmvb,.doc,.docx,.xls,.xlsx,.ppt,.htm,.html,.txt,.zip,.rar,.gz,.bz2");
    }
 
    /**
     * 获取文件后缀
     *
     * @param fileName 文件名
     * @return String 返回后缀
     */
    public static String getFileExt(String fileName) {
        return fileName.substring(fileName.lastIndexOf(StringPool.DOT));
    }
 
    /**
     * 测试文件后缀 只让指定后缀的文件上传,像jsp,war,sh等危险的后缀禁止
     *
     * @param dir      目录
     * @param fileName 文件名
     * @return 返回成功与否
     */
    public static boolean testExt(String dir, String fileName) {
        String fileExt = getFileExt(fileName);
        String ext = EXT_MAP.get(dir);
        return StringUtil.isNotBlank(ext) && (ext.contains(fileExt.toLowerCase()) || ext.contains(fileExt.toUpperCase()));
    }
 
    /**
     * 文件管理排序
     */
    public enum FileSort {
 
        /**
         * 大小
         */
        size,
 
        /**
         * 类型
         */
        type,
 
        /**
         * 名称
         */
        name;
 
        /**
         * 文本排序转换成枚举
         *
         * @param sort
         * @return
         */
        public static FileSort of(String sort) {
            try {
                return FileSort.valueOf(sort);
            } catch (Exception e) {
                return FileSort.name;
            }
        }
    }
 
    public static class NameComparator implements Comparator {
        @Override
        public int compare(Object a, Object b) {
            Hashtable hashA = (Hashtable) a;
            Hashtable hashB = (Hashtable) b;
            if (((Boolean) hashA.get(IS_DIR)) && !((Boolean) hashB.get(IS_DIR))) {
                return -1;
            } else if (!((Boolean) hashA.get(IS_DIR)) && ((Boolean) hashB.get(IS_DIR))) {
                return 1;
            } else {
                return ((String) hashA.get(FILE_NAME)).compareTo((String) hashB.get(FILE_NAME));
            }
        }
    }
 
    public static class SizeComparator implements Comparator {
        @Override
        public int compare(Object a, Object b) {
            Hashtable hashA = (Hashtable) a;
            Hashtable hashB = (Hashtable) b;
            if (((Boolean) hashA.get(IS_DIR)) && !((Boolean) hashB.get(IS_DIR))) {
                return -1;
            } else if (!((Boolean) hashA.get(IS_DIR)) && ((Boolean) hashB.get(IS_DIR))) {
                return 1;
            } else {
                if (((Long) hashA.get(FILE_SIZE)) > ((Long) hashB.get(FILE_SIZE))) {
                    return 1;
                } else if (((Long) hashA.get(FILE_SIZE)) < ((Long) hashB.get(FILE_SIZE))) {
                    return -1;
                } else {
                    return 0;
                }
            }
        }
    }
 
    public static class TypeComparator implements Comparator {
        @Override
        public int compare(Object a, Object b) {
            Hashtable hashA = (Hashtable) a;
            Hashtable hashB = (Hashtable) b;
            if (((Boolean) hashA.get(IS_DIR)) && !((Boolean) hashB.get(IS_DIR))) {
                return -1;
            } else if (!((Boolean) hashA.get(IS_DIR)) && ((Boolean) hashB.get(IS_DIR))) {
                return 1;
            } else {
                return ((String) hashA.get("filetype")).compareTo((String) hashB.get("filetype"));
            }
        }
    }
 
    public static String formatUrl(String url) {
        return url.replaceAll("\\\\", "/");
    }
 
 
    /********************************BladeFile封装********************************************************/
 
    /**
     * 获取BladeFile封装类
     *
     * @param file 文件
     * @return BladeFile
     */
    public static LocalFile getFile(MultipartFile file) {
        return getFile(file, "image", null, null);
    }
 
    /**
     * 获取BladeFile封装类
     *
     * @param file 文件
     * @param dir  目录
     * @return BladeFile
     */
    public static LocalFile getFile(MultipartFile file, String dir) {
        return getFile(file, dir, null, null);
    }
 
    /**
     * 获取BladeFile封装类
     *
     * @param file        文件
     * @param dir         目录
     * @param path        路径
     * @param virtualPath 虚拟路径
     * @return BladeFile
     */
    public static LocalFile getFile(MultipartFile file, String dir, String path, String virtualPath) {
        return new LocalFile(file, dir, path, virtualPath);
    }
 
    /**
     * 获取BladeFile封装类
     *
     * @param files 文件集合
     * @return BladeFile
     */
    public static List<LocalFile> getFiles(List<MultipartFile> files) {
        return getFiles(files, "image", null, null);
    }
 
    /**
     * 获取BladeFile封装类
     *
     * @param files 文件集合
     * @param dir   目录
     * @return BladeFile
     */
    public static List<LocalFile> getFiles(List<MultipartFile> files, String dir) {
        return getFiles(files, dir, null, null);
    }
 
    /**
     * 获取BladeFile封装类
     *
     * @param files       文件集合
     * @param path        路径
     * @param virtualPath 虚拟路径
     * @return BladeFile
     */
    public static List<LocalFile> getFiles(List<MultipartFile> files, String dir, String path, String virtualPath) {
        List<LocalFile> list = new ArrayList<>();
        for (MultipartFile file : files) {
            list.add(new LocalFile(file, dir, path, virtualPath));
        }
        return list;
    }
 
}