Prechádzať zdrojové kódy

Signed-off-by: ljx <809268652@qq.com>上传和更新证件照

ljx 2 rokov pred
rodič
commit
337f14464b

+ 26 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java

@@ -96,6 +96,32 @@ public class CommonController
     }
 
     /**
+     * 通用上传请求(单个)
+     */
+    @PostMapping("/uploadTemp")
+    public AjaxResult uploadFileTemp(MultipartFile file) throws Exception
+    {
+        try
+        {
+            // 上传文件路径
+            String filePath = RuoYiConfig.getUploadPath2();
+            // 上传并返回新文件名称
+            String fileName = FileUploadUtils.upload2(filePath, file);
+            String url = serverConfig.getUrl() + fileName;
+            AjaxResult ajax = AjaxResult.success();
+            ajax.put("url", url);
+            ajax.put("fileName", fileName);
+            ajax.put("newFileName", FileUtils.getName(fileName));
+            ajax.put("originalFilename", file.getOriginalFilename());
+            return ajax;
+        }
+        catch (Exception e)
+        {
+            return AjaxResult.error(e.getMessage());
+        }
+    }
+
+    /**
      * 通用上传请求(多个)
      */
     @PostMapping("/uploads")

+ 1 - 1
ruoyi-admin/src/main/resources/application.yml

@@ -9,7 +9,7 @@ ruoyi:
   # 实例演示开关
   demoEnabled: true
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
-  profile: D:/ruoyi/uploadPath
+  profile: D:/certificate/images
   # 获取ip地址开关
   addressEnabled: false
   # 验证码类型 math 数组计算 char 字符验证

+ 8 - 0
ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java

@@ -132,4 +132,12 @@ public class RuoYiConfig
     {
         return getProfile() + "/upload";
     }
+
+    /**
+     * 获取上传路径
+     */
+    public static String getUploadPath2()
+    {
+        return getProfile();
+    }
 }

+ 88 - 1
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java

@@ -39,6 +39,7 @@ public class FileUploadUtils
 
     public static void setDefaultBaseDir(String defaultBaseDir)
     {
+
         FileUploadUtils.defaultBaseDir = defaultBaseDir;
     }
 
@@ -87,6 +88,26 @@ public class FileUploadUtils
     }
 
     /**
+     * 根据文件路径上传
+     *
+     * @param baseDir 相对应用的基目录
+     * @param file 上传的文件
+     * @return 文件名称
+     * @throws IOException
+     */
+    public static final String upload2(String baseDir, MultipartFile file) throws IOException
+    {
+        try
+        {
+            return upload2(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
+        }
+        catch (Exception e)
+        {
+            throw new IOException(e.getMessage(), e);
+        }
+    }
+
+    /**
      * 文件上传
      *
      * @param baseDir 相对应用的基目录
@@ -118,6 +139,37 @@ public class FileUploadUtils
     }
 
     /**
+     * 文件上传
+     *
+     * @param baseDir 相对应用的基目录
+     * @param file 上传的文件
+     * @param allowedExtension 上传文件类型
+     * @return 返回上传成功的文件名
+     * @throws FileSizeLimitExceededException 如果超出最大大小
+     * @throws FileNameLengthLimitExceededException 文件名太长
+     * @throws IOException 比如读写文件出错时
+     * @throws InvalidExtensionException 文件校验异常
+     */
+    public static final String upload2(String baseDir, MultipartFile file, String[] allowedExtension)
+            throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
+            InvalidExtensionException
+    {
+        int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
+        if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
+        {
+            throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
+        }
+
+        assertAllowed(file, allowedExtension);
+
+        String fileName = extractFilename2(file);
+
+        String absPath = getAbsoluteFile2(baseDir, fileName).getAbsolutePath();
+        file.transferTo(Paths.get(absPath));
+        return getPathFileName2(baseDir, fileName);
+    }
+
+    /**
      * 编码文件名
      */
     public static final String extractFilename(MultipartFile file)
@@ -125,11 +177,33 @@ public class FileUploadUtils
         return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
                 FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
     }
+    /**
+     * 编码文件名
+     */
+    public static final String extractFilename2(MultipartFile file)
+    {
+        return StringUtils.format("{}/{}_{}.{}", StringUtils.substringBefore(file.getOriginalFilename(),"_"),
+                StringUtils.substringBefore(StringUtils.substringAfter(file.getOriginalFilename(),"_"),"."), Seq.getId(Seq.uploadSeqType).substring(14,17), 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 File getAbsoluteFile2(String uploadDir, String fileName) throws IOException
+    {
+        //File desc = new File(uploadDir + File.separator + fileName);
+        File desc = new File(uploadDir + "/" + fileName);
         if (!desc.exists())
         {
             if (!desc.getParentFile().exists())
@@ -144,9 +218,22 @@ public class FileUploadUtils
     {
         int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
         String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
-        return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
+        return Constants.RESOURCE_PREFIX +
+                "/" + currentDir +
+                "/" + fileName;
+    }
+
+    public static final String getPathFileName2(String uploadDir, String fileName) throws IOException
+    {
+        int dirLastIndex = RuoYiConfig.getUploadPath2().length() + 1;
+        String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
+        return Constants.RESOURCE_PREFIX +
+                //"/" + currentDir +
+                "/" + fileName;
     }
 
+
+
     /**
      * 文件大小校验
      *

+ 10 - 1
ruoyi-system/src/main/java/com/ruoyi/system/domain/GradTable.java

@@ -65,7 +65,7 @@ public class GradTable extends BaseEntity
     private String trainPeriod;
 
     /** 照片 */
-    @Excel(name = "照片")
+    //@Excel(name = "照片")
     private String picture;
 
     /** 手机号码 */
@@ -82,6 +82,15 @@ public class GradTable extends BaseEntity
     /** 自增id */
     private Integer id;
 
+    /**
+     * 模板id
+     */
+    @Excel(name = "模板id")
     private int templateId;
 
+    /**
+     * 年度
+     */
+    private String year;
+
 }

+ 10 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/GradTableServiceImpl.java

@@ -7,6 +7,7 @@ import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanValidators;
 import com.ruoyi.system.domain.ContractTable;
 import com.ruoyi.system.domain.dto.QueryEntity;
+import org.apache.commons.lang3.time.DateFormatUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.ruoyi.system.mapper.GradTableMapper;
@@ -63,6 +64,11 @@ public class GradTableServiceImpl implements IGradTableService
     @Override
     public int insertGradTable(GradTable gradTable)
     {
+        String picture = gradTable.getPicture();
+        //图片为空默认图片为服务器存放图片路径
+        if (picture == null && gradTable.getCertType() == 0){
+            gradTable.setPicture("/profile/" + DateFormatUtils.format(gradTable.getIssueDate(),"yyyy") + "/" + gradTable.getCertId() + ".jpg");
+        }
         return gradTableMapper.insertGradTable(gradTable);
     }
 
@@ -122,6 +128,10 @@ public class GradTableServiceImpl implements IGradTableService
                 {
                     BeanValidators.validateWithException(validator, con);
                     con.setCreateBy(operName);
+                    //图片为空默认图片为服务器存放图片路径
+                    if (con.getPicture() == null && con.getCertType() == 0){
+                        con.setPicture("/profile/" + DateFormatUtils.format(con.getIssueDate(),"yyyy") + "/" + con.getCertId() + ".jpg");
+                    }
                     this.insertGradTable(con);
                     successNum++;
                     successMsg.append("<br/>" + successNum + "、ID " + con.getId() + " 导入成功");

+ 5 - 1
ruoyi-system/src/main/resources/mapper/system/GradTableMapper.xml

@@ -21,10 +21,11 @@
         <result property="updateDate"    column="update_date"    />
         <result property="id"    column="id"    />
         <result property="templateId" column="template_id"/>
+        <result property="year" column="year"/>
     </resultMap>
 
     <sql id="selectGradTableVo">
-        select cert_type, uesr_name, user_id, cert_id, issue_date, cert_name, course_name, course_hours, level, train_period, picture, user_phone, user_email, update_date, id, template_id from grad_table
+        select cert_type, uesr_name, user_id, cert_id, issue_date, cert_name, course_name, course_hours, level, train_period, picture, user_phone, user_email, update_date, id, template_id, year from grad_table
     </sql>
 
     <select id="selectGradTableList" parameterType="GradTable" resultMap="GradTableResult">
@@ -74,6 +75,7 @@
             <if test="userEmail != null">user_email,</if>
             <if test="updateDate != null">update_date,</if>
             <if test="templateId != null">template_id,</if>
+            <if test="year != null">year,</if>
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="certType != null">#{certType},</if>
@@ -91,6 +93,7 @@
             <if test="userEmail != null">#{userEmail},</if>
             <if test="updateDate != null">#{updateDate},</if>
             <if test="templateId != null">#{templateId},</if>
+            <if test="year != null">#{year},</if>
         </trim>
     </insert>
 
@@ -112,6 +115,7 @@
             <if test="userEmail != null">user_email = #{userEmail},</if>
             <if test="updateDate != null">update_date = #{updateDate},</if>
             <if test="templateId != null">template_id = #{templateId},</if>
+            <if test="year != null">year = #{year},</if>
         </trim>
         where id = #{id}
     </update>