• java版linux文件上传


    工具类都放一个configss包了,方便使用
    CommonEnum

    package com.qhzx.mad.backstage.configss;
    
    import lombok.Getter;
    import lombok.Setter;
    
    /**
     * @Description:
     * @Author: x
     * @Date :
     */
    
    public enum CommonEnum {
    
        //成功
    
        SUCCESS_RESPONSE(200, "成功"),
        //失败
    
        FAILED_RESPONSE(400, "失败");
    
        @Setter
        @Getter
        private Integer code;
    
        @Setter
        @Getter
        private String msg;
    
    
        CommonEnum(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    }
    
    • 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

    DateUtils

    package com.qhzx.mad.backstage.configss;
    
    import io.swagger.models.auth.In;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    
    /**
     * @author: yc
     * @des: 时间工具类
     * @date: 2021/7/5 14:28
     */
    public class DateUtils {
    
        //年月日格式
        private static final String Y_M_D = "yyyy-MM-dd";
    
        //年月日 时分秒格式
        private static final String Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss";
    
        public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    
        /**
         * @author: yc
         * @des: 获取当前 年月日 时间字符串
         * @date: 2021/7/5 14:29
         */
        public static String getCurrentYMDStr() {
            SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D);
            return sdf.format(new Date());
        }
    
        /**
         * @author: yc
         * @des: 获取当前 年月日-时分秒 时间字符串
         * @date: 2021/7/5 14:34
         */
        public static String getCurrentYMDHMSStr() {
            SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D_H_M_S);
            return sdf.format(new Date());
        }
    
        /**
         * @author: yc
         * @des: 获取当前 年月日 date对象
         * @date: 2021/7/5 14:38
         */
        public static Date getCurrentYMDDate() {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D);
                return sdf.parse(getCurrentYMDStr());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * @author: yc
         * @des: 获取当前 年月日-时分秒 date对象
         * @date: 2021/7/5 14:41
         */
        public static Date getCurrentYMDHMSDate() {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D_H_M_S);
                return sdf.parse(getCurrentYMDHMSStr());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * @author: yc
         * @des: 根据某一时间字符串获取 年月日 date对象
         * @date: 2021/7/5 14:45
         */
        public static Date getCurrentYMDDate(String ymdTime) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D);
                return sdf.parse(ymdTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * @author: yc
         * @des: 根据某一时间获取 年月日-时分秒 date对象
         * @date: 2021/7/5 14:46
         */
        public static Date getCurrentYMDHMSDate(String ymdHmsTime) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D_H_M_S);
                return sdf.parse(ymdHmsTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * @author: yc
         * @des: 将date对象转换为 年月日字符串
         * @date: 2021/7/5 15:36
         */
        public static String date2ymdStr(Date date) {
            SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D);
            return null == date ? "" : sdf.format(date);
        }
    
        /**
         * @author: yc
         * @des: 将date对象转换为 年月日-时分秒 字符串
         * @date: 2021/7/5 15:37
         */
        public static String date2ymdHmsStr(Date date) {
            SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D_H_M_S);
            return null == date ? "" : sdf.format(date);
        }
    
        /**
         * @author: yc
         * @des: 将某一 年月日字符串时间 转换为 年月日时分秒字符串
         * @date: 2021/7/5 15:54
         */
        public static String ymdStr2ymdHmsStr(String ymdTime) {
            SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D_H_M_S);
            return null == ymdTime ? "" : sdf.format(getCurrentYMDDate(ymdTime));
        }
    
        /**
         * @author: yc
         * @des: 将某一 年月日时分秒字符串 转换为 年月日字符串时间
         * @date: 2021/7/5 15:58
         */
        public static String ymdHmsStr2ymdStr(String ymdHmsTime) {
            SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D);
            return null == ymdHmsTime ? "" : sdf.format(getCurrentYMDHMSDate(ymdHmsTime));
        }
    
        /**
         * @author: yc
         * @des: 时间移动 根据某一 年月日时间字符串(time) 及 移动天数(moveDays) 得到移动后的 年月日时间字符串
         * moveNum > 0 -> 向后移 反之 -> 向前移
         * moveType = "移动维度" ->
         * Calendar.SECOND = "秒"
         * Calendar.MINUTE = "分"
         * Calendar.HOUR_OF_DAY = "时"
         * Calendar.DATE = "日"
         * Calendar.WEEK_OF_MONTH = "周"
         * Calendar.MONTH = "月"
         * Calendar.YEAR = "年"
         * @date: 2021/7/5 14:48
         */
        public static String getMoveYMDStrByTime(Integer moveNum, Integer moveType, String ymdTime) {
            SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D);
            Date date = null;
            try {
                date = sdf.parse(ymdTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(moveNum, moveType);
                date = calendar.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return sdf.format(date);
        }
    
        /**
         * @author: yc
         * @des: 时间移动 根据某一 年月日-时分秒时间字符串(time) 及 移动天数(moveDays) 得到移动后的 年月日-时分秒时间字符串
         * moveNum > 0 -> 向后移 反之 -> 向前移
         * moveType = "移动维度" ->
         * Calendar.SECOND = "秒"
         * Calendar.MINUTE = "分"
         * Calendar.HOUR_OF_DAY = "时"
         * Calendar.DATE = "日"
         * Calendar.WEEK_OF_MONTH = "周"
         * Calendar.MONTH = "月"
         * Calendar.YEAR = "年"
         * @date: 2021/7/5 14:48
         */
        public static String getMoveYMDHMSStrByTime(Integer moveNum, Integer moveType, String ymdHmsTime) {
            SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D_H_M_S);
            Date date = null;
            try {
                date = sdf.parse(ymdHmsTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(moveType, moveNum);
                date = calendar.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return sdf.format(date);
        }
    
        /**
         * @author: yc
         * @des: 获取最近24小时List
         * @date: 2021/7/5 15:39
         */
        public static List<String> getLocal24Hour() {
            int mod = 24;
            String time = null;
            List<String> local24HourList = new ArrayList<>();
            String currentHour = getCurrentYMDHMSStr().substring(11, 13);
            for (int i = 1; i <= mod; i++) {
                time = String.valueOf((Integer.parseInt(currentHour) + i) % mod);
                String hourTime = Integer.parseInt(time) < 10 ? "0" + time + ":00" : time + ":00";
                String realDate = ymdHmsStr2ymdStr(getMoveYMDHMSStrByTime(i - mod, Calendar.HOUR_OF_DAY, getCurrentYMDHMSStr())) + " " + hourTime + ":00";
                local24HourList.add(realDate + "_" + hourTime);
            }
            return local24HourList;
        }
    
        /**
         * @author: yc
         * @des: 获取两个 年月日字符串时间 之间的 list 年月日事件对象
         * @date: 2021/7/5 16:01
         */
        public static List<String> getYMDListBetweenStartEndTime(String startTime, String endTime) {
            List<String> ymdList = new ArrayList<>();
            try {
                String currentTime = "";
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Calendar st = Calendar.getInstance();
                Calendar ed = Calendar.getInstance();
                st.setTime(sdf.parse(startTime));
                ed.setTime(sdf.parse(endTime));
                //按照天维度来拉取数据
                for (st.setTime(sdf.parse(startTime)); !st.after(ed); st.add(Calendar.DAY_OF_YEAR, 1)) {
                    currentTime = sdf.format(st.getTime());
                    ymdList.add(currentTime);
                    System.out.println(currentTime);
                }
                return ymdList;
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         *
         */
        /**
         * @author: yc
         * @des: 获取最近12个月
         * @date: 2021/7/5 16:03
         */
        public static List<String> getLast12Months() {
            List<String> last12MonthList = new ArrayList<>();
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
            cal.set(Calendar.DATE, 1);
            for (int i = 0; i < 12; i++) {
                cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
                last12MonthList.add(cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1 < 10 ? "0" + (cal.get(Calendar.MONTH) + 1) : String.valueOf(cal.get(Calendar.MONTH) + 1)));
            }
            return last12MonthList;
        }
    
        /**
         * @author: yc
         * @des: 转换unix时间格式
         * @date: 2021/7/28 16:49
         */
        public static String getTimestampDate(String timestamp) {
            String str;
            SimpleDateFormat unix_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            str = unix_time.format(new Date(Long.valueOf(timestamp + "000")));
            return str;
        }
    
        /**
         * @author: yc
         * @des: 判断一个时间 是否在一个时间区间内
         * @date: 2021/8/23 9:24
         */
        public static boolean isBetween(Date current, Date startTime, Date endTime) {
            if (current.getTime() == startTime.getTime()
                    || current.getTime() == endTime.getTime()) {
                return true;
            }
            Calendar date = Calendar.getInstance();
            date.setTime(current);
            Calendar begin = Calendar.getInstance();
            begin.setTime(startTime);
            Calendar end = Calendar.getInstance();
            end.setTime(endTime);
            if (date.after(begin) && date.before(end)) {
                return true;
            } else {
                return false;
            }
        }
    
        /**
         * @author: yc
         * @des: 判断 年月日 日期 在哪个季度
         * @date: 2021/7/9 11:16
         */
        public static Integer getQuarterByYMD(String ymdTime) {
            Integer currentMonth = Integer.parseInt(ymdTime.substring(5, 7));
            if (currentMonth >= 1 && currentMonth <= 3) {
                return 1;
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                return 2;
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                return 3;
            } else {
                return 4;
            }
        }
    
        public static final String dateTimeNow() {
            return dateTimeNow(YYYYMMDDHHMMSS);
        }
    
        public static final String dateTimeNow(final String format) {
            return parseDateToStr(format, new Date());
        }
    
        public static final String parseDateToStr(final String format, final Date date) {
            return new SimpleDateFormat(format).format(date);
        }
    
        /**
         //     * @author: yc
         //     * @des: 获取最近N个季度
         //     * @date: 2022/3/8 14:00
         //     */
    //    public static List<String> getLocalQuarter(Integer n,String sort){
    //        List<String> quarterList = new ArrayList<>();
    //        String currentYMD = getCurrentYMDStr();
    //        //当前季度
    //        Integer currentQuarter = getQuarterByYMD(currentYMD);
    //        //当前季度的最后一个年月日 如 2021年第二季度的最后一个月为 2021-06-01
    //        String maxYMD = currentYMD.substring(0,4) + "-" + (currentQuarter * 3 < 10 ? "0" + currentQuarter * 3 : currentQuarter * 3) + "-01";
    //        // 顺序
    //        ActionUtils.doIfTrueOrNot("asc".equals(sort),() -> {
    //            for(int i = n- 1; i >= 0 ;i--){
    //                quarterList.add(getMoveYMDStrByTime(-i * 3 * 30,Calendar.getInstance().DATE,maxYMD).substring(0,7));
    //            }
    //        }, () -> {
    //            for(int i = 0; i <= n-1; i++){
    //                quarterList.add(getMoveYMDStrByTime(-i * 3 * 30,Calendar.getInstance().DATE,maxYMD).substring(0,7));
    //            }
    //        });
    //        return quarterList;
    //    }
    }
    
    • 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
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360

    FileUploadUtils

    package com.qhzx.mad.backstage.configss;
    
    import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
    import org.springframework.util.StringUtils;
    import org.springframework.web.multipart.MultipartFile;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Paths;
    import java.util.Objects;
    
    /**
     * 文件上传工具类
     *
     * @author ruoyi
     */
    public class FileUploadUtils {
        /**
         * 默认大小 50M
         */
        public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
    
        /**
         * 默认的文件名最大长度 100
         */
        public static final int DEFAULT_FILE_NAME_LENGTH = 100;
    
        /**
         * 根据文件路径上传
         *
         * @param baseDir 相对应用的基目录
         * @param file    上传的文件
         * @return 文件名称
         * @throws IOException
         */
        public static final String upload(String baseDir, MultipartFile file) throws IOException {
            try {
                return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
            } catch (Exception e) {
                throw new IOException(e.getMessage(), e);
            }
        }
    
        /**
         * 文件上传
         *
         * @param baseDir          相对应用的基目录
         * @param file             上传的文件
         * @param allowedExtension 上传文件类型
         * @return 返回上传成功的文件名
         * @throws FileSizeLimitExceededException 如果超出最大大小
         * @throws IOException                    比如读写文件出错时
         */
        public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
                throws IOException {
    
            String fileName = extractFilename(file);
    
            String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
            file.transferTo(Paths.get(absPath));
            return getPathFileName(baseDir, fileName);
        }
    
        /**
         * 编码文件名
         */
        public static final String extractFilename(MultipartFile file) {
    
            return "2022/06/22"  + Seq.getId(Seq.uploadSeqType) + getExtension(file);
        }
    
        public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
            File desc = new File(uploadDir + File.separator + fileName);
    
            if (!desc.exists()) {
                if (!desc.getParentFile().exists()) {
                    desc.getParentFile().mkdirs();
                }
            }
            return desc;
        }
    
        public static final String getPathFileName(String uploadDir, String fileName) throws IOException {
            int dirLastIndex = ProjectConfig.getProfile().length() + 1;
            String currentDir = uploadDir.substring(dirLastIndex);
            return ProjectConfig.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
        }
    
    
        /**
         * 获取文件名的后缀
         *
         * @param file 表单文件
         * @return 后缀名
         */
        public static final String getExtension(MultipartFile file) {
            String extension = file.getOriginalFilename();
    
            if (StringUtils.isEmpty(extension)) {
                extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
            }
            return extension;
        }
    }
    
    • 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

    FileUtils

    package com.qhzx.mad.backstage.configss;
    
    
    import java.io.*;
    import java.net.URLEncoder;
    import java.nio.charset.StandardCharsets;
    
    /**
     * 文件处理工具类
     * 
     * @author ruoyi
     */
    public class FileUtils
    {
        public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
    
    
        /**
         * 百分号编码工具方法
         *
         * @param s 需要百分号编码的字符串
         * @return 百分号编码后的字符串
         */
        public static String percentEncode(String s) throws UnsupportedEncodingException
        {
            String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
            return encode.replaceAll("\\+", "%20");
        }
    
        /**
         * 获取图像后缀
         * 
         * @param photoByte 图像数据
         * @return 后缀名
         */
        public static String getFileExtendName(byte[] photoByte)
        {
            String strFileExtendName = "jpg";
            if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
                    && ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97))
            {
                strFileExtendName = "gif";
            }
            else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70))
            {
                strFileExtendName = "jpg";
            }
            else if ((photoByte[0] == 66) && (photoByte[1] == 77))
            {
                strFileExtendName = "bmp";
            }
            else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71))
            {
                strFileExtendName = "png";
            }
            return strFileExtendName;
        }
    
        /**
         * 获取文件名称 /profile/upload/2022/04/16/ruoyi.png -- ruoyi.png
         * 
         * @param fileName 路径名称
         * @return 没有文件路径的名称
         */
        public static String getName(String fileName)
        {
            if (fileName == null)
            {
                return null;
            }
            int lastUnixPos = fileName.lastIndexOf('/');
            int lastWindowsPos = fileName.lastIndexOf('\\');
            int index = Math.max(lastUnixPos, lastWindowsPos);
            return fileName.substring(index + 1);
        }
    }
    
    • 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

    MimeTypeUtils

    package com.qhzx.mad.backstage.configss;
    
    /**
     * 媒体类型工具类
     * 
     * @author ruoyi
     */
    public class MimeTypeUtils
    {
        public static final String IMAGE_PNG = "image/png";
    
        public static final String IMAGE_JPG = "image/jpg";
    
        public static final String IMAGE_JPEG = "image/jpeg";
    
        public static final String IMAGE_BMP = "image/bmp";
    
        public static final String IMAGE_GIF = "image/gif";
        
        public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png" };
    
        public static final String[] FLASH_EXTENSION = { "swf", "flv" };
    
        public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg",
                "asf", "rm", "rmvb" };
    
        public static final String[] VIDEO_EXTENSION = { "mp4", "avi", "rmvb" };
    
        public static final String[] DEFAULT_ALLOWED_EXTENSION = {
                // 图片
                "bmp", "gif", "jpg", "jpeg", "png",
                // word excel powerpoint
                "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
                // 压缩文件
                "rar", "zip", "gz", "bz2",
                // 视频格式
                "mp4", "avi", "rmvb",
                // pdf
                "pdf" };
    
        public static String getExtension(String prefix)
        {
            switch (prefix)
            {
                case IMAGE_PNG:
                    return "png";
                case IMAGE_JPG:
                    return "jpg";
                case IMAGE_JPEG:
                    return "jpeg";
                case IMAGE_BMP:
                    return "bmp";
                case IMAGE_GIF:
                    return "gif";
                default:
                    return "";
            }
        }
    }
    
    • 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

    ProjectConfig

    package com.qhzx.mad.backstage.configss;
    
    public class ProjectConfig {
    
        /**
         * 上传路径
         */
        private static final String profile = "/home/statics";
    
        /**
         * 资源映射路径 前缀
         */
        public static final String RESOURCE_PREFIX = "/profile";
    
        public static String getProfile() {
            return profile;
        }
    
        /**
         * 获取上传路径
         */
        public static String getUploadPath() {
            return profile + "/upload";
        }
    }
    
    • 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

    Result

    package com.qhzx.mad.backstage.configss;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.*;
    import lombok.experimental.Accessors;
    
    import java.io.Serializable;
    import static com.qhzx.mad.backstage.configss.CommonEnum.FAILED_RESPONSE;
    import static com.qhzx.mad.backstage.configss.CommonEnum.SUCCESS_RESPONSE;
    
    /**
     * @Description:响应结果集
     * @Author: x
     * @Date :
     */
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    @Accessors(chain = true)
    @ApiModel(description = "响应信息主体")
    @Data
    public class Result<T>  implements Serializable {
        private static final long serialVersionUID = 1L;
    
        @ApiModelProperty(value = "返回标记:成功标记=200,失败标记=400")
        private int code;
    
        @ApiModelProperty(value = "返回信息")
        private String msg;
    
        @ApiModelProperty(value = "数据")
        private T data;
    
        /**
         * 成功
         */
        public static <T> Result<T> succeed() {
            return restResult(null, SUCCESS_RESPONSE.getCode(), null);
        }
    
        public static <T> Result<T> succeed(T data) {
            return restResult(data, SUCCESS_RESPONSE.getCode(), null);
        }
    
        public static <T> Result<T> succeed(T data, String msg) {
            return restResult(data, SUCCESS_RESPONSE.getCode(), msg);
        }
    
        public static <T> Result<T> succeed(String msg) {
            return restResult(null, SUCCESS_RESPONSE.getCode(), msg);
        }
    
        /**
         * 响应返回结果
         *
         * @param rows 影响行数
         * @return 操作结果
         */
        public static Result toAjax(int rows) {
            return rows > 0 ? succeed() : fail(FAILED_RESPONSE.getCode(), "修改失败!请检查参数");
        }
    
        /**
         * 失败
         */
    
        public static <T> Result<T> fail(int code, String msg) {
            return restResult(null, code, msg);
        }
    
        /**
         * 失败
         */
        public static <T> Result<T> fail(String msg) {
            return restResult(null, FAILED_RESPONSE.getCode(), msg);
        }
    
    
        private static <T> Result<T> restResult(T data, int code, String msg) {
            Result<T> apiResult = new Result<>();
            apiResult.setCode(code);
            apiResult.setData(data);
            apiResult.setMsg(msg);
            return apiResult;
        }
    
        public Result(CommonEnum commonEnum, T data) {
            this.code = commonEnum.getCode();
            this.msg = commonEnum.getMsg();
            this.data = data;
        }
    
        public Result(CommonEnum commonEnum) {
            this.code = commonEnum.getCode();
            this.msg = commonEnum.getMsg();
        }
    
        public Result<T> setData(T data) {
            this.data = data;
            return this;
        }
    }
    
    
    • 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

    Seq

    package com.qhzx.mad.backstage.configss;
    
    
    import java.util.concurrent.atomic.AtomicInteger;
    
    /**
     * @author ruoyi 序列生成类
     */
    public class Seq {
        // 通用序列类型
        public static final String commSeqType = "COMMON";
    
        // 上传序列类型
        public static final String uploadSeqType = "UPLOAD";
    
        // 通用接口序列数
        private static AtomicInteger commSeq = new AtomicInteger(1);
    
        // 上传接口序列数
        private static AtomicInteger uploadSeq = new AtomicInteger(1);
    
        // 机器标识
        private static String machineCode = "A";
    
        /**
         * 获取通用序列号
         *
         * @return 序列值
         */
        public static String getId() {
            return getId(commSeqType);
        }
    
        /**
         * 默认16位序列号 yyMMddHHmmss + 一位机器标识 + 3长度循环递增字符串
         *
         * @return 序列值
         */
        public static String getId(String type) {
            AtomicInteger atomicInt = commSeq;
            if (uploadSeqType.equals(type)) {
                atomicInt = uploadSeq;
            }
            return getId(atomicInt, 3);
        }
    
        /**
         * 通用接口序列号 yyMMddHHmmss + 一位机器标识 + length长度循环递增字符串
         *
         * @param atomicInt 序列数
         * @param length    数值长度
         * @return 序列值
         */
        public static String getId(AtomicInteger atomicInt, int length) {
            String result = DateUtils.dateTimeNow();
            result += machineCode;
            result += getSeq(atomicInt, length);
            return result;
        }
    
        /**
         * 序列循环递增字符串[1, 10 的 (length)幂次方), 用0左补齐length位数
         *
         * @return 序列值
         */
        private synchronized static String getSeq(AtomicInteger atomicInt, int length) {
            // 先取值再+1
            int value = atomicInt.getAndIncrement();
    
            // 如果更新后值>=10 的 (length)幂次方则重置为1
            int maxSeq = (int) Math.pow(10, length);
            if (atomicInt.get() >= maxSeq) {
                atomicInt.set(1);
            }
            // 转字符串,用0左补齐
            return padl(value, length);
        }
    
        /**
         * 数字左边补齐0,使之达到指定长度。注意,如果数字转换为字符串后,长度大于size,则只保留 最后size个字符。
         *
         * @param num  数字对象
         * @param size 字符串指定长度
         * @return 返回数字的字符串格式,该字符串为指定长度。
         */
        public static final String padl(final Number num, final int size) {
            return padl(num.toString(), size, '0');
        }
    
        /**
         * 字符串左补齐。如果原始字符串s长度大于size,则只保留最后size个字符。
         *
         * @param s    原始字符串
         * @param size 字符串指定长度
         * @param c    用于补齐的字符
         * @return 返回指定长度的字符串,由原字符串左补齐或截取得到。
         */
        public static final String padl(final String s, final int size, final char c) {
            final StringBuilder sb = new StringBuilder(size);
            if (s != null) {
                final int len = s.length();
                if (s.length() <= size) {
                    for (int i = size - len; i > 0; i--) {
                        sb.append(c);
                    }
                    sb.append(s);
                } else {
                    return s.substring(len - size, len);
                }
            } else {
                for (int i = size; i > 0; i--) {
                    sb.append(c);
                }
            }
            return sb.toString();
        }
    }
    
    • 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

    CommonController

    package com.qhzx.mad.backstage.controller;
    
    import com.qhzx.mad.backstage.configss.FileUploadUtils;
    import com.qhzx.mad.backstage.configss.FileUtils;
    import com.qhzx.mad.backstage.configss.ProjectConfig;
    import com.qhzx.mad.backstage.configss.Result;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;
    import java.util.*;
    
    /**
     * 通用请求处理
     *
     * @author ruoyi
     */
    
    @Api(tags = "上传api")
    @RestController
    @CrossOrigin
    @RequestMapping("/common")
    public class CommonController {
        private static final Logger log = LoggerFactory.getLogger(CommonController.class);
    
    
        /**
         * 通用上传请求(单个)
         */
        @ApiOperation("通用上传请求")
        @PostMapping("/upload")
        public Result uploadFile(MultipartFile file) {
            try {
                // 上传文件路径
                String filePath = ProjectConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                Map<String, Object> ajax = new HashMap<>();
                ajax.put("fileName", fileName);
                ajax.put("newFileName", FileUtils.getName(fileName));
                ajax.put("originalFilename", file.getOriginalFilename());
                return Result.succeed(ajax);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.fail(e.getMessage());
            }
        }
    
    }
    
    
    • 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
  • 相关阅读:
    Programming Languages PartB Week2学习笔记——用Racket编写解释器
    牛客刷题,python入门基础(14)
    如何快速使用proteus【硬件课程设计】
    [解决方案]springboot怎么接受encode后的参数(参数通过&=拼接)
    【Linux】UOS桌面系统,设置普通用户sudo免密
    VPP以太网VLAN子接口
    c++中的cin和getline()函数
    微信小程序:(更新)云开发微群人脉
    第三方接口重试机制是怎么做的
    复盘模型总结
  • 原文地址:https://blog.csdn.net/qq_42981242/article/details/125431670