před 8 měsíci
rodič
revize
3c3097f898

+ 60 - 0
08.src/Xingxi/xingxi-admin/src/main/java/com/xingxi/web/controller/system/SysFileUploadController.java

@@ -0,0 +1,60 @@
+package com.xingxi.web.controller.system;
+
+import com.xingxi.common.core.controller.BaseController;
+import com.xingxi.common.core.domain.AjaxResult;
+import com.xingxi.common.enums.EUploadFileType;
+import com.xingxi.common.exception.file.InvalidExtensionException;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * 文件上传
+ *
+ * @author xingxi
+ */
+
+@RestController
+@RequestMapping("/common/upload/file")
+public class SysFileUploadController extends BaseController {
+    @PostMapping("/image")
+    public AjaxResult image(MultipartFile imageFile, @RequestHeader(required = false, name = "X-File-Type") String xFileType) {
+        EUploadFileType eUploadFileType = EUploadFileType.IMAGE;
+        EUploadFileType[] eUploadFileTypes = EUploadFileType.values();
+        for (EUploadFileType uploadFileType : eUploadFileTypes) {
+            if (uploadFileType.getVal().equals(xFileType)) {
+                eUploadFileType = uploadFileType;
+                break;
+            }
+        }
+
+        AjaxResult ajaxResult = AjaxResult.success();
+        try {
+            ajaxResult.put("imageUrl", this.upload(imageFile, eUploadFileType));
+        } catch (Exception ex) {
+            if (ex instanceof InvalidExtensionException) {
+                ajaxResult = AjaxResult.error("文件格式错误,请选择图片文件");
+            } else {
+                ajaxResult = AjaxResult.error();
+            }
+        }
+        return ajaxResult;
+    }
+
+    @PostMapping("/video")
+    public AjaxResult video(MultipartFile videoFile) {
+        AjaxResult ajaxResult = AjaxResult.success();
+        try {
+            ajaxResult.put("videoUrl", this.upload(videoFile, EUploadFileType.VIDEO));
+        } catch (Exception ex) {
+            if (ex instanceof InvalidExtensionException) {
+                ajaxResult = AjaxResult.error("文件格式错误,请选择视频文件");
+            } else {
+                ajaxResult = AjaxResult.error();
+            }
+        }
+        return ajaxResult;
+    }
+}

+ 31 - 29
08.src/Xingxi/xingxi-admin/src/main/resources/templates/master/designer/designer.html

@@ -77,36 +77,38 @@
                 removeUrl: prefix + "/remove",
                 exportUrl: prefix + "/export",
                 modalName: "设计师",
-                columns: [{
-                    checkbox: true
-                },
-                {
-                    field: 'id',
-                    title: '主键',
-                    visible: false
-                },
-                {
-                    field: 'designerName',
-                    title: '昵称'
-                },
-                {
-                    field: 'designerAvatar',
-                    title: '设计师头像'
-                },
-                {
-                    field: 'description',
-                    title: '设计师描述'
-                },
-                {
-                    title: '操作',
-                    align: 'center',
-                    formatter: function(value, row, index) {
-                        var actions = [];
-                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
-                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
-                        return actions.join('');
+                columns: [
+                    {
+                        field: 'id',
+                        title: '主键',
+                        visible: false
+                    },
+                    {
+                        field: 'designerName',
+                        title: '昵称'
+                    },
+                    {
+                        field: 'designerAvatar',
+                        title: '设计师头像',
+                        formatter: function(value, row, index) {
+                            return $.table.imageView(value);
+                        }
+                    },
+                    {
+                        field: 'description',
+                        title: '设计师描述'
+                    },
+                    {
+                        title: '操作',
+                        align: 'center',
+                        formatter: function(value, row, index) {
+                            var actions = [];
+                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
+                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
+                            return actions.join('');
+                        }
                     }
-                }]
+                ]
             };
             $.table.init(options);
         });

+ 175 - 3
08.src/Xingxi/xingxi-admin/src/main/resources/templates/master/designer/edit.html

@@ -2,6 +2,133 @@
 <html lang="zh" xmlns:th="http://www.thymeleaf.org" >
 <head>
     <th:block th:include="include :: header('修改设计师')" />
+    <th:block th:include="include :: cropper-css" />
+    <style type='text/css'>
+        /* avator css start */
+        .container {
+            margin: 10px 5px 5px 5px;
+        }
+
+        .action {
+            padding: 5px 0px;
+        }
+
+        .cropped {
+            width: 200px;
+            border: 1px #ddd solid;
+            box-shadow: 0px 0px 12px #ddd;
+        }
+
+        .img-preview {
+            border-radius: 50%;
+            box-shadow: 0px 0px 12px #7e7e7e;
+            display: inline-block;
+        }
+
+        .preview-box {
+            text-align: center;
+            margin: 0px auto;
+            margin-top: 10px;
+            color: #bbb;
+        }
+
+        .preview-md {
+            width: 128px;
+            height: 128px;
+        }
+
+        .preview-sm {
+            width: 96px;
+            height: 96px;
+        }
+
+        .preview-xs {
+            width: 64px;
+            height: 64px;
+        }
+
+        .imageBox {
+            border: 1px solid #aaa;
+            overflow: hidden;
+            cursor: move;
+            box-shadow: 4px 4px 12px #B0B0B0;
+            margin: 0px auto;
+        }
+
+        .btn-custom {
+            float: right;
+            width: 46px;
+            display: inline-block;
+            margin-bottom: 10px;
+            height: 37px;
+            line-height: 37px;
+            font-size: 14px;
+            color: #FFFFFF;
+            margin: 0px 2px;
+            background-color: #f38e81;
+            border-radius: 3px;
+            text-decoration: none;
+            cursor: pointer;
+            box-shadow: 0px 0px 5px #B0B0B0;
+            border: 0px #fff solid;
+        }
+        /*选择文件上传*/
+        .new-contentarea {
+            width: 165px;
+            overflow: hidden;
+            margin: 0 auto;
+            position: relative;
+            float: left;
+        }
+
+        .new-contentarea label {
+            width: 100%;
+            height: 100%;
+            display: block;
+        }
+
+        .new-contentarea input[type=file] {
+            width: 188px;
+            height: 60px;
+            background: #333;
+            margin: 0 auto;
+            position: absolute;
+            right: 50%;
+            margin-right: -94px;
+            top: 0;
+            right/*\**/: 0px\9;
+            margin-right/*\**/: 0px\9;
+            width/*\**/: 10px\9;
+            opacity: 0;
+            filter: alpha(opacity=0);
+            z-index: 2;
+        }
+
+        a.upload-img {
+            width: 165px;
+            display: inline-block;
+            margin-bottom: 10px;
+            height: 37px;
+            line-height: 37px;
+            font-size: 14px;
+            color: #FFFFFF;
+            background-color: #f38e81;
+            border-radius: 3px;
+            text-decoration: none;
+            cursor: pointer;
+            border: 0px #fff solid;
+            box-shadow: 0px 0px 5px #B0B0B0;
+        }
+
+        a.upload-img:hover {
+            background-color: #ec7e70;
+        }
+
+        .tc {
+            text-align: center;
+        }
+        /* avator css end */
+    </style>
 </head>
 <body class="white-bg">
     <div class="wrapper wrapper-content animated fadeInRight ibox-content">
@@ -18,9 +145,8 @@
             <div class="col-xs-12">
                 <div class="form-group">
                     <label class="col-sm-3 control-label">设计师头像:</label>
-                    <div class="col-sm-8">
-                        <input name="designerAvatar" th:field="*{designerAvatar}" class="form-control" type="text">
-                    </div>
+                    <input type="file" class="form-control" name="imgFile" id="bannerImg" accept="image/*"/>
+                    <input type="hidden" th:field="*{designerAvatar}" class="form-control" name="imgUrl" id="designerAvatar" />
                 </div>
             </div>
             <div class="col-xs-12">
@@ -34,8 +160,54 @@
         </form>
     </div>
     <th:block th:include="include :: footer" />
+    <th:block th:include="include :: cropper-js" />
     <script th:inline="javascript">
         var prefix = ctx + "master/designer";
+        $('#designerAvatar').change(function () {
+            var fileSize = 0;
+            fileSize = this. files[0].size;
+            fileSize=Math. round( fileSize/1024*100)/100;
+
+            if(fileSize>2048){
+                layer.msg('上传文件不得大于2M,请重新上传。', {time: 3000, icon:6});
+                return false;
+            }
+
+            if (!/image\/\w+/.test(this.files[0].type)) {
+                layer.msg('上传的不是图片文件,请重新上传。', {time: 3000, icon:6});
+                return false;
+            }
+
+            if ($(this).val()) {
+                var formdata = new FormData();
+                formdata.append("imageFile", this.files[0]);
+                $.ajax({
+                    url: ctx + "common/upload/file/image",
+                    data: formdata,
+                    type: "post",
+                    processData: false,
+                    contentType: false,
+                    beforeSend: function () {
+                        $.modal.loading("正在处理中,请稍后...");
+                        $.modal.disable();
+                    },
+                    success: function (result) {
+                        if (result.code == web_status.SUCCESS) {
+                            $('#viewImage').attr('src', result.imageUrl);
+                            $('#bannerImgUrl').val(result.imageUrl);
+                            $.modal.alertSuccess(result.msg)
+                        } else if (result.code == web_status.WARNING) {
+                            $.modal.alertWarning(result.msg)
+                        } else {
+                            $.modal.alertError(result.msg);
+                        }
+                        $.modal.closeLoading();
+                        $.modal.enable();
+                    }
+                })
+            }
+        });
+
         $("#form-designer-edit").validate({
             focusCleanup: true
         });

+ 103 - 69
08.src/Xingxi/xingxi-common/src/main/java/com/xingxi/common/core/controller/BaseController.java

@@ -1,51 +1,58 @@
 package com.xingxi.common.core.controller;
 
-import java.beans.PropertyEditorSupport;
-import java.util.Date;
-import java.util.List;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.web.bind.WebDataBinder;
-import org.springframework.web.bind.annotation.InitBinder;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
+import com.xingxi.common.aliyun.oss.AliyunOSSClient;
 import com.xingxi.common.core.domain.AjaxResult;
 import com.xingxi.common.core.domain.AjaxResult.Type;
 import com.xingxi.common.core.domain.entity.SysUser;
 import com.xingxi.common.core.page.PageDomain;
 import com.xingxi.common.core.page.TableDataInfo;
 import com.xingxi.common.core.page.TableSupport;
-import com.xingxi.common.utils.DateUtils;
-import com.xingxi.common.utils.PageUtils;
-import com.xingxi.common.utils.ServletUtils;
-import com.xingxi.common.utils.ShiroUtils;
-import com.xingxi.common.utils.StringUtils;
+import com.xingxi.common.enums.EUploadFileType;
+import com.xingxi.common.enums.EUploadType;
+import com.xingxi.common.utils.*;
+import com.xingxi.common.utils.file.FileUploadUtils;
+import com.xingxi.common.utils.file.MimeTypeUtils;
 import com.xingxi.common.utils.sql.SqlUtil;
+import lombok.SneakyThrows;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.InitBinder;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import java.beans.PropertyEditorSupport;
+import java.util.Date;
+import java.util.List;
 
 /**
  * web层通用数据处理
- * 
+ *
  * @author ruoyi
  */
-public class BaseController
-{
+public class BaseController {
     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
     /**
+     * 阿里云OSS上传客户端
+     */
+    @Resource
+    private AliyunOSSClient client;
+
+    /**
      * 将前台传递过来的日期格式的字符串,自动转化为Date类型
      */
     @InitBinder
-    public void initBinder(WebDataBinder binder)
-    {
+    public void initBinder(WebDataBinder binder) {
         // Date 类型转换
-        binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
-        {
+        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
             @Override
-            public void setAsText(String text)
-            {
+            public void setAsText(String text) {
                 setValue(DateUtils.parseDate(text));
             }
         });
@@ -54,19 +61,16 @@ public class BaseController
     /**
      * 设置请求分页数据
      */
-    protected void startPage()
-    {
+    protected void startPage() {
         PageUtils.startPage();
     }
 
     /**
      * 设置请求排序数据
      */
-    protected void startOrderBy()
-    {
+    protected void startOrderBy() {
         PageDomain pageDomain = TableSupport.buildPageRequest();
-        if (StringUtils.isNotEmpty(pageDomain.getOrderBy()))
-        {
+        if (StringUtils.isNotEmpty(pageDomain.getOrderBy())) {
             String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
             PageHelper.orderBy(orderBy);
         }
@@ -75,41 +79,36 @@ public class BaseController
     /**
      * 清理分页的线程变量
      */
-    protected void clearPage()
-    {
+    protected void clearPage() {
         PageUtils.clearPage();
     }
 
     /**
      * 获取request
      */
-    public HttpServletRequest getRequest()
-    {
+    public HttpServletRequest getRequest() {
         return ServletUtils.getRequest();
     }
 
     /**
      * 获取response
      */
-    public HttpServletResponse getResponse()
-    {
+    public HttpServletResponse getResponse() {
         return ServletUtils.getResponse();
     }
 
     /**
      * 获取session
      */
-    public HttpSession getSession()
-    {
+    public HttpSession getSession() {
         return getRequest().getSession();
     }
 
     /**
      * 响应请求分页数据
      */
-    @SuppressWarnings({ "rawtypes", "unchecked" })
-    protected TableDataInfo getDataTable(List<?> list)
-    {
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    protected TableDataInfo getDataTable(List<?> list) {
         TableDataInfo rspData = new TableDataInfo();
         rspData.setCode(0);
         rspData.setRows(list);
@@ -117,113 +116,148 @@ public class BaseController
         return rspData;
     }
 
+    @SneakyThrows
+    protected String upload(MultipartFile file, EUploadFileType eUploadFileType) {
+        String filePath = eUploadFileType.getPath();
+        if (file != null && !file.isEmpty()) {
+            if (eUploadFileType.equals(EUploadFileType.IMAGE) ||
+                    eUploadFileType.equals(EUploadFileType.MERC) ||
+                    eUploadFileType.equals(EUploadFileType.VOUCHER)) {
+                return FileUploadUtils.upload(filePath, file, MimeTypeUtils.IMAGE_EXTENSION);
+            }
+            if (eUploadFileType.equals(EUploadFileType.VIDEO)) {
+                return FileUploadUtils.upload(filePath, file, MimeTypeUtils.MEDIA_EXTENSION);
+            }
+            if (eUploadFileType.equals(EUploadFileType.EXCEL)) {
+                return FileUploadUtils.upload(filePath, file, MimeTypeUtils.EXCEL_EXTENSION);
+            }
+        }
+        return null;
+    }
+
+    /**
+     * 阿里云OSS上传文件
+     *
+     * @param file        文件
+     * @param eUploadType 上传类型
+     * @return
+     * @throws Exception
+     */
+    protected String putObject(MultipartFile file, EUploadType eUploadType) throws Exception {
+        String objectName = null;
+        if (file != null && !file.isEmpty()) {
+            switch (eUploadType) {
+                case IMAGE:
+                    objectName = FileUploadUtils.generateFileName(file, MimeTypeUtils.IMAGE_EXTENSION);
+                    break;
+                case VIDEO:
+                    objectName = FileUploadUtils.generateFileName(file, MimeTypeUtils.MEDIA_EXTENSION);
+                    break;
+                case EXCEL:
+                    objectName = FileUploadUtils.generateFileName(file, MimeTypeUtils.EXCEL_EXTENSION);
+                    break;
+            }
+        }
+        if (objectName != null) {
+            return client.putObject(objectName, file.getInputStream());
+        }
+        return null;
+    }
+
     /**
      * 响应返回结果
-     * 
+     *
      * @param rows 影响行数
      * @return 操作结果
      */
-    protected AjaxResult toAjax(int rows)
-    {
+    protected AjaxResult toAjax(int rows) {
         return rows > 0 ? success() : error();
     }
 
     /**
      * 响应返回结果
-     * 
+     *
      * @param result 结果
      * @return 操作结果
      */
-    protected AjaxResult toAjax(boolean result)
-    {
+    protected AjaxResult toAjax(boolean result) {
         return result ? success() : error();
     }
 
     /**
      * 返回成功
      */
-    public AjaxResult success()
-    {
+    public AjaxResult success() {
         return AjaxResult.success();
     }
 
     /**
      * 返回失败消息
      */
-    public AjaxResult error()
-    {
+    public AjaxResult error() {
         return AjaxResult.error();
     }
 
     /**
      * 返回成功消息
      */
-    public AjaxResult success(String message)
-    {
+    public AjaxResult success(String message) {
         return AjaxResult.success(message);
     }
 
     /**
      * 返回成功数据
      */
-    public static AjaxResult success(Object data)
-    {
+    public static AjaxResult success(Object data) {
         return AjaxResult.success("操作成功", data);
     }
 
     /**
      * 返回失败消息
      */
-    public AjaxResult error(String message)
-    {
+    public AjaxResult error(String message) {
         return AjaxResult.error(message);
     }
 
     /**
      * 返回错误码消息
      */
-    public AjaxResult error(Type type, String message)
-    {
+    public AjaxResult error(Type type, String message) {
         return new AjaxResult(type, message);
     }
 
     /**
      * 页面跳转
      */
-    public String redirect(String url)
-    {
+    public String redirect(String url) {
         return StringUtils.format("redirect:{}", url);
     }
 
     /**
      * 获取用户缓存信息
      */
-    public SysUser getSysUser()
-    {
+    public SysUser getSysUser() {
         return ShiroUtils.getSysUser();
     }
 
     /**
      * 设置用户缓存信息
      */
-    public void setSysUser(SysUser user)
-    {
+    public void setSysUser(SysUser user) {
         ShiroUtils.setSysUser(user);
     }
 
     /**
      * 获取登录用户id
      */
-    public Long getUserId()
-    {
+    public Long getUserId() {
         return getSysUser().getUserId();
     }
 
     /**
      * 获取登录用户名
      */
-    public String getLoginName()
-    {
+    public String getLoginName() {
         return getSysUser().getLoginName();
     }
 }

+ 11 - 0
08.src/Xingxi/xingxi-common/src/main/java/com/xingxi/common/enums/BaseEnum.java

@@ -0,0 +1,11 @@
+package com.xingxi.common.enums;
+
+/**
+ * 枚举类型基本方法接口
+ *
+ * @author xingxi
+ */
+public interface BaseEnum {
+    String getVal();
+    String getDesc();
+}

+ 2 - 3
08.src/Xingxi/xingxi-common/src/main/java/com/xingxi/common/enums/BusinessStatus.java

@@ -2,11 +2,10 @@ package com.xingxi.common.enums;
 
 /**
  * 操作状态
- * 
+ *
  * @author ruoyi
  */
-public enum BusinessStatus
-{
+public enum BusinessStatus {
     /**
      * 成功
      */

+ 3 - 4
08.src/Xingxi/xingxi-common/src/main/java/com/xingxi/common/enums/BusinessType.java

@@ -2,11 +2,10 @@ package com.xingxi.common.enums;
 
 /**
  * 业务操作类型
- * 
+ *
  * @author ruoyi
  */
-public enum BusinessType
-{
+public enum BusinessType {
     /**
      * 其它
      */
@@ -51,7 +50,7 @@ public enum BusinessType
      * 生成代码
      */
     GENCODE,
-    
+
     /**
      * 清空
      */

+ 24 - 0
08.src/Xingxi/xingxi-common/src/main/java/com/xingxi/common/enums/EUploadFileType.java

@@ -0,0 +1,24 @@
+package com.xingxi.common.enums;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 上传文件类型
+ *
+ * @author xingxi
+ */
+@Getter
+@AllArgsConstructor
+public enum EUploadFileType implements BaseEnum {
+    MERC("merc", "public/merc/privacy", "身份证和营业执照之类"),
+    VOUCHER("voucher", "public/merc/voucher", "业务凭证"),
+    INVOICE("invoice", "public/merc/invoice", "发票"),
+    IMAGE("image", "public/image", "各种公共图片"),
+    VIDEO("video", "public/video", "视频"),
+    EXCEL("excel", "public/file", "视频");
+
+    private final String val;
+    private final String path;
+    private final String desc;
+}

+ 13 - 0
08.src/Xingxi/xingxi-common/src/main/java/com/xingxi/common/enums/EUploadType.java

@@ -0,0 +1,13 @@
+package com.xingxi.common.enums;
+
+/**
+ * @program: hocy
+ * @description: upload type
+ * @author: dong
+ * @create: 2020-01-12 19:40
+ */
+public enum EUploadType {
+    VIDEO,
+    IMAGE,
+    EXCEL;
+}

+ 56 - 78
08.src/Xingxi/xingxi-common/src/main/java/com/xingxi/common/utils/file/FileUploadUtils.java

@@ -1,11 +1,5 @@
 package com.xingxi.common.utils.file;
 
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Paths;
-import java.util.Objects;
-import org.apache.commons.io.FilenameUtils;
-import org.springframework.web.multipart.MultipartFile;
 import com.xingxi.common.config.RuoYiConfig;
 import com.xingxi.common.constant.Constants;
 import com.xingxi.common.exception.file.FileNameLengthLimitExceededException;
@@ -14,14 +8,20 @@ import com.xingxi.common.exception.file.InvalidExtensionException;
 import com.xingxi.common.utils.DateUtils;
 import com.xingxi.common.utils.StringUtils;
 import com.xingxi.common.utils.uuid.Seq;
+import org.apache.commons.io.FilenameUtils;
+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
-{
+public class FileUploadUtils {
     /**
      * 默认大小 50M
      */
@@ -37,13 +37,11 @@ public class FileUploadUtils
      */
     private static String defaultBaseDir = RuoYiConfig.getProfile();
 
-    public static void setDefaultBaseDir(String defaultBaseDir)
-    {
+    public static void setDefaultBaseDir(String defaultBaseDir) {
         FileUploadUtils.defaultBaseDir = defaultBaseDir;
     }
 
-    public static String getDefaultBaseDir()
-    {
+    public static String getDefaultBaseDir() {
         return defaultBaseDir;
     }
 
@@ -54,14 +52,10 @@ public class FileUploadUtils
      * @return 文件名称
      * @throws Exception
      */
-    public static final String upload(MultipartFile file) throws IOException
-    {
-        try
-        {
+    public static final String upload(MultipartFile file) throws IOException {
+        try {
             return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
-        }
-        catch (Exception e)
-        {
+        } catch (Exception e) {
             throw new IOException(e.getMessage(), e);
         }
     }
@@ -70,18 +64,14 @@ public class FileUploadUtils
      * 根据文件路径上传
      *
      * @param baseDir 相对应用的基目录
-     * @param file 上传的文件
+     * @param file    上传的文件
      * @return 文件名称
      * @throws IOException
      */
-    public static final String upload(String baseDir, MultipartFile file) throws IOException
-    {
-        try
-        {
+    public static final String upload(String baseDir, MultipartFile file) throws IOException {
+        try {
             return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
-        }
-        catch (Exception e)
-        {
+        } catch (Exception e) {
             throw new IOException(e.getMessage(), e);
         }
     }
@@ -89,22 +79,20 @@ public class FileUploadUtils
     /**
      * 文件上传
      *
-     * @param baseDir 相对应用的基目录
-     * @param file 上传的文件
+     * @param baseDir          相对应用的基目录
+     * @param file             上传的文件
      * @param allowedExtension 上传文件类型
      * @return 返回上传成功的文件名
-     * @throws FileSizeLimitExceededException 如果超出最大大小
+     * @throws FileSizeLimitExceededException       如果超出最大大小
      * @throws FileNameLengthLimitExceededException 文件名太长
-     * @throws IOException 比如读写文件出错时
-     * @throws InvalidExtensionException 文件校验异常
+     * @throws IOException                          比如读写文件出错时
+     * @throws InvalidExtensionException            文件校验异常
      */
     public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
             throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
-            InvalidExtensionException
-    {
+            InvalidExtensionException {
         int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length();
-        if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
-        {
+        if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
             throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
         }
 
@@ -117,31 +105,38 @@ public class FileUploadUtils
         return getPathFileName(baseDir, fileName);
     }
 
+    public static String generateFileName(MultipartFile file, String[] allowedExtension) throws Exception {
+        int fileNamelength = file.getOriginalFilename().length();
+        // 校验文件名称长度
+        if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
+            throw new RuntimeException(String.format("上传的文件名最长%s个字符", DEFAULT_FILE_NAME_LENGTH));
+        }
+        // 校验文件后缀类型
+        assertAllowed(file, allowedExtension);
+        // 生成文件名称
+        return extractFilename(file);
+    }
+
     /**
      * 编码文件名
      */
-    public static final String extractFilename(MultipartFile file)
-    {
+    public static final String extractFilename(MultipartFile file) {
         return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
                 FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
     }
 
-    public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
-    {
+    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())
-            {
+        if (!desc.exists()) {
+            if (!desc.getParentFile().exists()) {
                 desc.getParentFile().mkdirs();
             }
         }
         return desc;
     }
 
-    public static final String getPathFileName(String uploadDir, String fileName) throws IOException
-    {
+    public static final String getPathFileName(String uploadDir, String fileName) throws IOException {
         int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
         String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
         return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
@@ -156,40 +151,28 @@ public class FileUploadUtils
      * @throws InvalidExtensionException
      */
     public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
-            throws FileSizeLimitExceededException, InvalidExtensionException
-    {
+            throws FileSizeLimitExceededException, InvalidExtensionException {
         long size = file.getSize();
-        if (size > DEFAULT_MAX_SIZE)
-        {
+        if (size > DEFAULT_MAX_SIZE) {
             throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
         }
 
         String fileName = file.getOriginalFilename();
         String extension = getExtension(file);
-        if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
-        {
-            if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
-            {
+        if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
+            if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) {
                 throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
                         fileName);
-            }
-            else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
-            {
+            } else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) {
                 throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
                         fileName);
-            }
-            else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
-            {
+            } else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) {
                 throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
                         fileName);
-            }
-            else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION)
-            {
+            } else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) {
                 throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
                         fileName);
-            }
-            else
-            {
+            } else {
                 throw new InvalidExtensionException(allowedExtension, extension, fileName);
             }
         }
@@ -202,12 +185,9 @@ public class FileUploadUtils
      * @param allowedExtension
      * @return
      */
-    public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
-    {
-        for (String str : allowedExtension)
-        {
-            if (str.equalsIgnoreCase(extension))
-            {
+    public static final boolean isAllowedExtension(String extension, String[] allowedExtension) {
+        for (String str : allowedExtension) {
+            if (str.equalsIgnoreCase(extension)) {
                 return true;
             }
         }
@@ -216,15 +196,13 @@ public class FileUploadUtils
 
     /**
      * 获取文件名的后缀
-     * 
+     *
      * @param file 表单文件
      * @return 后缀名
      */
-    public static final String getExtension(MultipartFile file)
-    {
+    public static final String getExtension(MultipartFile file) {
         String extension = FilenameUtils.getExtension(file.getOriginalFilename());
-        if (StringUtils.isEmpty(extension))
-        {
+        if (StringUtils.isEmpty(extension)) {
             extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
         }
         return extension;

+ 2 - 0
08.src/Xingxi/xingxi-common/src/main/java/com/xingxi/common/utils/file/MimeTypeUtils.java

@@ -24,6 +24,8 @@ public class MimeTypeUtils
     public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg",
             "asf", "rm", "rmvb" };
 
+    public static final String[] EXCEL_EXTENSION = { "xls", "xlsx" };
+
     public static final String[] VIDEO_EXTENSION = { "mp4", "avi", "rmvb" };
 
     public static final String[] DEFAULT_ALLOWED_EXTENSION = {