From 6fc4af57d22780cf9809c5bcd4ad8a7ee815d2ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Wed, 12 Jan 2022 22:09:29 +0800 Subject: [PATCH 1/6] :bug: add test md5 --- .../com/rymcu/forest/utils/TestFileMd5.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/test/java/com/rymcu/forest/utils/TestFileMd5.java diff --git a/src/test/java/com/rymcu/forest/utils/TestFileMd5.java b/src/test/java/com/rymcu/forest/utils/TestFileMd5.java new file mode 100644 index 0000000..5a5f2be --- /dev/null +++ b/src/test/java/com/rymcu/forest/utils/TestFileMd5.java @@ -0,0 +1,26 @@ +package com.rymcu.forest.utils; + +import org.junit.jupiter.api.Test; +import org.springframework.util.DigestUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +public class TestFileMd5 { + + /** + * c6c26c7e8a5eb493b14e84bd91df60e3 + * d41d8cd98f00b204e9800998ecf8427e + * + * @throws Exception + */ + @Test + public void test() throws Exception { + String pathName = "E:\\1.txt"; + InputStream inputStream = new FileInputStream(new File(pathName)); + String md5 = DigestUtils.md5DigestAsHex((inputStream)); + System.err.println(md5); + System.err.println(md5.length()); + } +} From 97d08c4c7ef822dfb394f59b20447f256d8df604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Wed, 12 Jan 2022 22:36:55 +0800 Subject: [PATCH 2/6] :bug: add service --- .../com/rymcu/forest/entity/ForestFile.java | 57 +++++++++++++++++++ .../rymcu/forest/mapper/ForestFileMapper.java | 30 ++++++++++ .../forest/service/ForestFileService.java | 33 +++++++++++ .../service/impl/ForestFileServiceImpl.java | 47 +++++++++++++++ src/main/java/mapper/ForestFileMapper.xml | 24 ++++++++ 5 files changed, 191 insertions(+) create mode 100644 src/main/java/com/rymcu/forest/entity/ForestFile.java create mode 100644 src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java create mode 100644 src/main/java/com/rymcu/forest/service/ForestFileService.java create mode 100644 src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java create mode 100644 src/main/java/mapper/ForestFileMapper.xml diff --git a/src/main/java/com/rymcu/forest/entity/ForestFile.java b/src/main/java/com/rymcu/forest/entity/ForestFile.java new file mode 100644 index 0000000..d277c71 --- /dev/null +++ b/src/main/java/com/rymcu/forest/entity/ForestFile.java @@ -0,0 +1,57 @@ +package com.rymcu.forest.entity; + +import lombok.Data; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.Date; + +/** + * @author caterpillar + */ +@Data +@Table(name = "forest_File") +public class ForestFile { + + /** + * 主键 + */ + @Id + @GeneratedValue(generator = "JDBC") + @Column(name = "id") + private Long id; + + /** + * 访问路径 + */ + @Column(name = "web_path") + private String webPath; + + /** + * 上传路径 + */ + @Column(name = "upload_path") + private String uploadPath; + + /** + * md5 + */ + @Column(name = "md5_value") + private String md5Value; + + /** + * 创建时间 + */ + @Column(name = "create_time") + private Date createTime; + + /** + * 更新时间 + */ + @Column(name = "update_time") + private Date updateTime; + + +} diff --git a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java new file mode 100644 index 0000000..76a02a9 --- /dev/null +++ b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java @@ -0,0 +1,30 @@ +package com.rymcu.forest.mapper; + +import com.rymcu.forest.core.mapper.Mapper; +import com.rymcu.forest.entity.ForestFile; +import org.apache.ibatis.annotations.Param; + +/** + * @author caterpillar + * @date 2022-1-12 22:33:16 + */ +public interface ForestFileMapper extends Mapper { + + /** + * 通过md5获取文件对象 + * + * @param md5Value md5值 + * @return + */ + ForestFile getByMd5(@Param("md5Value") String md5Value); + + /** + * 插入文件对象 + * + * @param webPath 访问路径 + * @param uploadPath 上传路径 + * @param md5Value md5值 + * @return + */ + int insert(@Param("webPath") String webPath, @Param("uploadPath") String uploadPath, @Param("md5Value") String md5Value); +} diff --git a/src/main/java/com/rymcu/forest/service/ForestFileService.java b/src/main/java/com/rymcu/forest/service/ForestFileService.java new file mode 100644 index 0000000..e2e2bac --- /dev/null +++ b/src/main/java/com/rymcu/forest/service/ForestFileService.java @@ -0,0 +1,33 @@ +package com.rymcu.forest.service; + +import com.rymcu.forest.core.service.Service; +import com.rymcu.forest.entity.ForestFile; +import org.apache.ibatis.annotations.Param; + + +/** + * 文件服务记录 + * + * @author caterpillar + * @date 2022-1-12 22:32:49 + */ +public interface ForestFileService extends Service { + + /** + * 通过md5获取文件对象 + * + * @param md5Value md5值 + * @return + */ + ForestFile getByMd5(@Param("md5Value") String md5Value); + + /** + * 插入文件对象 + * + * @param webPath 访问路径 + * @param uploadPath 上传路径 + * @param md5Value md5值 + * @return + */ + int insert(@Param("webPath") String webPath, @Param("uploadPath") String uploadPath, @Param("md5Value") String md5Value); +} diff --git a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java new file mode 100644 index 0000000..21f8d52 --- /dev/null +++ b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java @@ -0,0 +1,47 @@ +package com.rymcu.forest.service.impl; + +import com.rymcu.forest.core.service.AbstractService; +import com.rymcu.forest.entity.ForestFile; +import com.rymcu.forest.mapper.ForestFileMapper; +import com.rymcu.forest.service.ForestFileService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * @author caterpillar + * @date 2022-1-12 22:34:55 + */ +@Service +@Slf4j +public class ForestFileServiceImpl extends AbstractService implements ForestFileService { + + @Resource + private ForestFileMapper forestFileMapper; + + + /** + * 通过md5获取文件对象 + * + * @param md5Value md5值 + * @return + */ + @Override + public ForestFile getByMd5(String md5Value) { + return forestFileMapper.getByMd5(md5Value); + } + + /** + * 插入文件对象 + * + * @param webPath 访问路径 + * @param uploadPath 上传路径 + * @param md5Value md5值 + * @return + */ + @Override + public int insert(String webPath, String uploadPath, String md5Value) { + return forestFileMapper.insert(webPath, uploadPath, md5Value); + } +} diff --git a/src/main/java/mapper/ForestFileMapper.xml b/src/main/java/mapper/ForestFileMapper.xml new file mode 100644 index 0000000..0443d01 --- /dev/null +++ b/src/main/java/mapper/ForestFileMapper.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + insert into forest_user_role (md5, upload_path, web_path, created_time) + values (#{md5Value}, #{uploadPath}, #{webPath}, sysdate()) + + + + + \ No newline at end of file From 245b3594812acddcbbba2069d337579801a729fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Thu, 13 Jan 2022 10:32:43 +0800 Subject: [PATCH 3/6] :bug: fixed file --- .../com/rymcu/forest/entity/ForestFile.java | 24 ++++++++++++------- .../rymcu/forest/mapper/ForestFileMapper.java | 5 ++-- .../forest/service/ForestFileService.java | 12 +++++----- .../service/impl/ForestFileServiceImpl.java | 15 ++++++------ src/main/java/mapper/ForestFileMapper.xml | 15 ++++++------ 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/rymcu/forest/entity/ForestFile.java b/src/main/java/com/rymcu/forest/entity/ForestFile.java index d277c71..d2c3f4d 100644 --- a/src/main/java/com/rymcu/forest/entity/ForestFile.java +++ b/src/main/java/com/rymcu/forest/entity/ForestFile.java @@ -12,7 +12,7 @@ import java.util.Date; * @author caterpillar */ @Data -@Table(name = "forest_File") +@Table(name = "forest_file") public class ForestFile { /** @@ -26,14 +26,14 @@ public class ForestFile { /** * 访问路径 */ - @Column(name = "web_path") - private String webPath; + @Column(name = "file_url") + private String fileUrl; /** * 上传路径 */ - @Column(name = "upload_path") - private String uploadPath; + @Column(name = "file_path") + private String filePath; /** * md5 @@ -44,14 +44,20 @@ public class ForestFile { /** * 创建时间 */ - @Column(name = "create_time") - private Date createTime; + @Column(name = "created_time") + private Date createdTime; /** * 更新时间 */ - @Column(name = "update_time") - private Date updateTime; + @Column(name = "updated_time") + private Date updatedTime; + + /** + * 创建人 + */ + @Column(name = "created_by") + private long createdBy; } diff --git a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java index 76a02a9..359ff50 100644 --- a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java +++ b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java @@ -16,7 +16,7 @@ public interface ForestFileMapper extends Mapper { * @param md5Value md5值 * @return */ - ForestFile getByMd5(@Param("md5Value") String md5Value); + ForestFile getForestFileByMd5(@Param("md5Value") String md5Value); /** * 插入文件对象 @@ -24,7 +24,8 @@ public interface ForestFileMapper extends Mapper { * @param webPath 访问路径 * @param uploadPath 上传路径 * @param md5Value md5值 + * @param createdBy 创建人 * @return */ - int insert(@Param("webPath") String webPath, @Param("uploadPath") String uploadPath, @Param("md5Value") String md5Value); + int insert(@Param("webPath") String webPath, @Param("uploadPath") String uploadPath, @Param("md5Value") String md5Value, @Param("createdBy") long createdBy); } diff --git a/src/main/java/com/rymcu/forest/service/ForestFileService.java b/src/main/java/com/rymcu/forest/service/ForestFileService.java index e2e2bac..d4d8362 100644 --- a/src/main/java/com/rymcu/forest/service/ForestFileService.java +++ b/src/main/java/com/rymcu/forest/service/ForestFileService.java @@ -2,7 +2,6 @@ package com.rymcu.forest.service; import com.rymcu.forest.core.service.Service; import com.rymcu.forest.entity.ForestFile; -import org.apache.ibatis.annotations.Param; /** @@ -19,15 +18,16 @@ public interface ForestFileService extends Service { * @param md5Value md5值 * @return */ - ForestFile getByMd5(@Param("md5Value") String md5Value); + ForestFile getForestFileByMd5(String md5Value); /** * 插入文件对象 * - * @param webPath 访问路径 - * @param uploadPath 上传路径 - * @param md5Value md5值 + * @param fileUrl 访问路径 + * @param filePath 上传路径 + * @param md5Value md5值 + * @param createdBy 创建人 * @return */ - int insert(@Param("webPath") String webPath, @Param("uploadPath") String uploadPath, @Param("md5Value") String md5Value); + int insert(String fileUrl, String filePath, String md5Value, long createdBy); } diff --git a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java index 21f8d52..ed87d5d 100644 --- a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java @@ -28,20 +28,21 @@ public class ForestFileServiceImpl extends AbstractService implement * @return */ @Override - public ForestFile getByMd5(String md5Value) { - return forestFileMapper.getByMd5(md5Value); + public ForestFile getForestFileByMd5(String md5Value) { + return forestFileMapper.getForestFileByMd5(md5Value); } /** * 插入文件对象 * - * @param webPath 访问路径 - * @param uploadPath 上传路径 - * @param md5Value md5值 + * @param fileUrl 访问路径 + * @param filePath 上传路径 + * @param md5Value md5值 + * @param createdBy 创建人 * @return */ @Override - public int insert(String webPath, String uploadPath, String md5Value) { - return forestFileMapper.insert(webPath, uploadPath, md5Value); + public int insert(String fileUrl, String filePath, String md5Value, long createdBy) { + return forestFileMapper.insert(fileUrl, filePath, md5Value, createdBy); } } diff --git a/src/main/java/mapper/ForestFileMapper.xml b/src/main/java/mapper/ForestFileMapper.xml index 0443d01..8e9080b 100644 --- a/src/main/java/mapper/ForestFileMapper.xml +++ b/src/main/java/mapper/ForestFileMapper.xml @@ -4,18 +4,19 @@ - - - - + + + + + - insert into forest_user_role (md5, upload_path, web_path, created_time) - values (#{md5Value}, #{uploadPath}, #{webPath}, sysdate()) + insert into forest_user_role (md5, file_path, file_url, created_time, created_by) + values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy}) - select * from forest_file where md5_value = #{md5Value} From dc34fded6f9e8756c0d0be0e207723d6ca412f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Thu, 13 Jan 2022 12:33:13 +0800 Subject: [PATCH 4/6] :bug: add service --- .../rymcu/forest/mapper/ForestFileMapper.java | 4 +-- .../forest/service/ForestFileService.java | 4 +-- .../service/impl/ForestFileServiceImpl.java | 10 +++---- .../web/api/common/UploadController.java | 29 +++++++++++++++---- src/main/java/mapper/ForestFileMapper.xml | 4 +-- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java index 359ff50..0dadde7 100644 --- a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java +++ b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java @@ -11,12 +11,12 @@ import org.apache.ibatis.annotations.Param; public interface ForestFileMapper extends Mapper { /** - * 通过md5获取文件对象 + * 通过md5获取文件访问链接 * * @param md5Value md5值 * @return */ - ForestFile getForestFileByMd5(@Param("md5Value") String md5Value); + String getFileUrlByMd5(@Param("md5Value") String md5Value); /** * 插入文件对象 diff --git a/src/main/java/com/rymcu/forest/service/ForestFileService.java b/src/main/java/com/rymcu/forest/service/ForestFileService.java index d4d8362..2c2c6ab 100644 --- a/src/main/java/com/rymcu/forest/service/ForestFileService.java +++ b/src/main/java/com/rymcu/forest/service/ForestFileService.java @@ -13,12 +13,12 @@ import com.rymcu.forest.entity.ForestFile; public interface ForestFileService extends Service { /** - * 通过md5获取文件对象 + * 通过md5获取文件访问链接 * * @param md5Value md5值 * @return */ - ForestFile getForestFileByMd5(String md5Value); + String getFileUrlByMd5(String md5Value); /** * 插入文件对象 diff --git a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java index ed87d5d..d3422fd 100644 --- a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java @@ -4,8 +4,8 @@ import com.rymcu.forest.core.service.AbstractService; import com.rymcu.forest.entity.ForestFile; import com.rymcu.forest.mapper.ForestFileMapper; import com.rymcu.forest.service.ForestFileService; -import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @@ -14,7 +14,6 @@ import javax.annotation.Resource; * @date 2022-1-12 22:34:55 */ @Service -@Slf4j public class ForestFileServiceImpl extends AbstractService implements ForestFileService { @Resource @@ -22,14 +21,14 @@ public class ForestFileServiceImpl extends AbstractService implement /** - * 通过md5获取文件对象 + * 通过md5获取文件访问链接 * * @param md5Value md5值 * @return */ @Override - public ForestFile getForestFileByMd5(String md5Value) { - return forestFileMapper.getForestFileByMd5(md5Value); + public String getFileUrlByMd5(String md5Value) { + return forestFileMapper.getFileUrlByMd5(md5Value); } /** @@ -42,6 +41,7 @@ public class ForestFileServiceImpl extends AbstractService implement * @return */ @Override + @Transactional(rollbackFor = Exception.class) public int insert(String fileUrl, String filePath, String md5Value, long createdBy) { return forestFileMapper.insert(fileUrl, filePath, md5Value, createdBy); } diff --git a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java index c0e9455..9c9cf20 100644 --- a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java +++ b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java @@ -5,6 +5,7 @@ import com.rymcu.forest.core.result.GlobalResultGenerator; import com.rymcu.forest.dto.LinkToImageUrlDTO; import com.rymcu.forest.dto.TokenUser; import com.rymcu.forest.jwt.def.JwtConstants; +import com.rymcu.forest.service.ForestFileService; import com.rymcu.forest.util.FileUtils; import com.rymcu.forest.util.SpringContextHolder; import com.rymcu.forest.util.UserUtils; @@ -14,10 +15,12 @@ import com.rymcu.forest.web.api.exception.ErrorCode; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.springframework.core.env.Environment; +import org.springframework.util.DigestUtils; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.ByteArrayOutputStream; import java.io.File; @@ -42,6 +45,9 @@ public class UploadController { private static Environment env = SpringContextHolder.getBean(Environment.class); + @Resource + private ForestFileService forestFileService; + @PostMapping("/file") public GlobalResult uploadPicture(@RequestParam(value = "file", required = false) MultipartFile multipartFile, @RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) { if (multipartFile == null) { @@ -161,9 +167,9 @@ public class UploadController { public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO) throws IOException { String url = linkToImageUrlDTO.getUrl(); URL link = new URL(url); - HttpURLConnection conn = (HttpURLConnection)link.openConnection(); + HttpURLConnection conn = (HttpURLConnection) link.openConnection(); //设置超时间为3秒 - conn.setConnectTimeout(3*1000); + conn.setConnectTimeout(3 * 1000); //防止屏蔽程序抓取而返回403错误 conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"); conn.setRequestProperty("referer", ""); @@ -172,6 +178,19 @@ public class UploadController { InputStream inputStream = conn.getInputStream(); //获取自己数组 byte[] getData = readInputStream(inputStream); + + // 获取文件md5值 + String md5 = DigestUtils.md5DigestAsHex(getData); + String fileUrl = forestFileService.getFileUrlByMd5(md5); + + Map data = new HashMap(2); + data.put("originalURL", url); + + if (StringUtils.isNotEmpty(fileUrl)) { + data.put("url", fileUrl); + return GlobalResultGenerator.genSuccessResult(data); + } + Integer type = linkToImageUrlDTO.getType(); if (Objects.isNull(type)) { type = 1; @@ -185,18 +204,18 @@ public class UploadController { file.mkdirs();// 创建文件根目录 } - String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/"; String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(url); + fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName; String savePath = file.getPath() + File.separator + fileName; - Map data = new HashMap(2); File saveFile = new File(savePath); try { FileCopyUtils.copy(getData, saveFile); + forestFileService.insert(fileUrl, savePath, md5, 1); data.put("originalURL", url); - data.put("url", localPath + fileName); + data.put("url", fileUrl); } catch (IOException e) { data.put("message", "上传失败!"); } diff --git a/src/main/java/mapper/ForestFileMapper.xml b/src/main/java/mapper/ForestFileMapper.xml index 8e9080b..2a103cd 100644 --- a/src/main/java/mapper/ForestFileMapper.xml +++ b/src/main/java/mapper/ForestFileMapper.xml @@ -16,8 +16,8 @@ values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy}) - + select file_url from forest_file where md5_value = #{md5Value} From 1aadd6f05a97e14ea51aac35e29d3c76af9a703c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Thu, 13 Jan 2022 15:18:23 +0800 Subject: [PATCH 5/6] =?UTF-8?q?:bug:=20add=20upload=20md5=20error:=20token?= =?UTF-8?q?=E5=A4=B1=E6=95=88=EF=BC=8C=E6=97=A0=E6=B3=95=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E7=99=BB=E5=BD=95=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rymcu/forest/mapper/ForestFileMapper.java | 11 +- .../forest/service/ForestFileService.java | 2 +- .../service/impl/ForestFileServiceImpl.java | 6 +- .../web/api/common/UploadController.java | 192 +++++++++++------- src/main/java/mapper/ForestFileMapper.xml | 6 +- 5 files changed, 126 insertions(+), 91 deletions(-) diff --git a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java index 0dadde7..339fdaf 100644 --- a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java +++ b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java @@ -21,11 +21,12 @@ public interface ForestFileMapper extends Mapper { /** * 插入文件对象 * - * @param webPath 访问路径 - * @param uploadPath 上传路径 - * @param md5Value md5值 - * @param createdBy 创建人 + * @param fileUrl 访问路径 + * @param filePath 上传路径 + * @param md5Value md5值 + * @param createdBy 创建人 * @return */ - int insert(@Param("webPath") String webPath, @Param("uploadPath") String uploadPath, @Param("md5Value") String md5Value, @Param("createdBy") long createdBy); + int insertForestFile(@Param("fileUrl") String fileUrl, @Param("filePath") String filePath, @Param("md5Value") String md5Value, @Param("createdBy") long createdBy); + } diff --git a/src/main/java/com/rymcu/forest/service/ForestFileService.java b/src/main/java/com/rymcu/forest/service/ForestFileService.java index 2c2c6ab..78f7bac 100644 --- a/src/main/java/com/rymcu/forest/service/ForestFileService.java +++ b/src/main/java/com/rymcu/forest/service/ForestFileService.java @@ -29,5 +29,5 @@ public interface ForestFileService extends Service { * @param createdBy 创建人 * @return */ - int insert(String fileUrl, String filePath, String md5Value, long createdBy); + int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy); } diff --git a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java index d3422fd..65b1a2a 100644 --- a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java @@ -5,7 +5,6 @@ import com.rymcu.forest.entity.ForestFile; import com.rymcu.forest.mapper.ForestFileMapper; import com.rymcu.forest.service.ForestFileService; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @@ -41,8 +40,7 @@ public class ForestFileServiceImpl extends AbstractService implement * @return */ @Override - @Transactional(rollbackFor = Exception.class) - public int insert(String fileUrl, String filePath, String md5Value, long createdBy) { - return forestFileMapper.insert(fileUrl, filePath, md5Value, createdBy); + public int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy) { + return forestFileMapper.insertForestFile(fileUrl, filePath, md5Value, createdBy); } } diff --git a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java index 9c9cf20..bd723fa 100644 --- a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java +++ b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java @@ -15,6 +15,7 @@ import com.rymcu.forest.web.api.exception.ErrorCode; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.springframework.core.env.Environment; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.DigestUtils; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.*; @@ -22,12 +23,10 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.file.Files; import java.util.*; /** @@ -48,11 +47,86 @@ public class UploadController { @Resource private ForestFileService forestFileService; + private static String getTypePath(Integer type) { + String typePath; + switch (type) { + case 0: + typePath = "avatar"; + break; + case 1: + typePath = "article"; + break; + case 2: + typePath = "tags"; + break; + default: + typePath = "images"; + } + return typePath; + } + + public static String uploadBase64File(String fileStr, Integer type) { + if (StringUtils.isBlank(fileStr)) { + return ""; + } + String typePath = getTypePath(type); + //图片存储路径 + String ctxHeadPicPath = env.getProperty("resource.pic-path"); + String dir = ctxHeadPicPath + "/" + typePath; + File file = new File(dir); + if (!file.exists()) { + file.mkdirs();// 创建文件根目录 + } + + String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/"; + String fileName = System.currentTimeMillis() + ".png"; + String savePath = file.getPath() + File.separator + fileName; + File saveFile = new File(savePath); + try { + FileCopyUtils.copy(Base64.decodeBase64(fileStr.substring(fileStr.indexOf(",") + 1)), saveFile); + fileStr = localPath + fileName; + } catch (IOException e) { + fileStr = "上传失败!"; + } + return fileStr; + } + + /** + * 从输入流中获取字节数组 + * + * @param inputStream + * @return + * @throws IOException + */ + public static byte[] readInputStream(InputStream inputStream) throws IOException { + byte[] buffer = new byte[1024]; + int len = 0; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + while ((len = inputStream.read(buffer)) != -1) { + bos.write(buffer, 0, len); + } + bos.close(); + return bos.toByteArray(); + } + @PostMapping("/file") - public GlobalResult uploadPicture(@RequestParam(value = "file", required = false) MultipartFile multipartFile, @RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) { + @Transactional(rollbackFor = Exception.class) + public GlobalResult uploadPicture(@RequestParam(value = "file", required = false) MultipartFile multipartFile, @RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) throws IOException, BaseApiException { if (multipartFile == null) { return GlobalResultGenerator.genErrorResult("请选择要上传的文件"); } + //todo 无法获取当前登录用户 +// User user = UserUtils.getCurrentUserByToken(); +// if (Objects.isNull(user)) { +// throw new BaseApiException(ErrorCode.INVALID_TOKEN); +// } + Map data = new HashMap(2); + String md5 = DigestUtils.md5DigestAsHex(multipartFile.getInputStream()); + String fileUrl = forestFileService.getFileUrlByMd5(md5); + if (StringUtils.isNotEmpty(fileUrl)) { + data.put("url", fileUrl); + return GlobalResultGenerator.genSuccessResult(data); + } String typePath = getTypePath(type); //图片存储路径 String ctxHeadPicPath = env.getProperty("resource.pic-path"); @@ -68,12 +142,12 @@ public class UploadController { String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(orgName).toLowerCase(); String savePath = file.getPath() + File.separator + fileName; - - Map data = new HashMap(2); + fileUrl = localPath + fileName; File saveFile = new File(savePath); try { FileCopyUtils.copy(multipartFile.getBytes(), saveFile); - data.put("url", localPath + fileName); + forestFileService.insertForestFile(fileUrl, savePath, md5, 1); + data.put("url", fileUrl); } catch (IOException e) { data.put("message", "上传失败!"); } @@ -82,7 +156,13 @@ public class UploadController { } @PostMapping("/file/batch") - public GlobalResult batchFileUpload(@RequestParam(value = "file[]", required = false) MultipartFile[] multipartFiles, @RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) { + @Transactional(rollbackFor = Exception.class) + public GlobalResult batchFileUpload(@RequestParam(value = "file[]", required = false) MultipartFile[] multipartFiles, @RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) throws BaseApiException { + //todo 无法获取当前登录用户 +// User user = UserUtils.getCurrentUserByToken(); +// if (Objects.isNull(user)) { +// throw new BaseApiException(ErrorCode.INVALID_TOKEN); +// } String typePath = getTypePath(type); //图片存储路径 String ctxHeadPicPath = env.getProperty("resource.pic-path"); @@ -100,16 +180,26 @@ public class UploadController { MultipartFile multipartFile = multipartFiles[i]; String orgName = multipartFile.getOriginalFilename(); String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(orgName).toLowerCase(); - String savePath = file.getPath() + File.separator + fileName; - File saveFile = new File(savePath); - try { - FileCopyUtils.copy(multipartFile.getBytes(), saveFile); + try (InputStream in = multipartFiles[i].getInputStream(); + OutputStream out = Files.newOutputStream(saveFile.toPath())) { + String md5 = DigestUtils.md5DigestAsHex(in); + String fileUrl = forestFileService.getFileUrlByMd5(md5); + if (StringUtils.isNotEmpty(fileUrl)) { + succMap.put(orgName, fileUrl); + continue; + } + + fileUrl = localPath + fileName; + FileCopyUtils.copy(in, out); + forestFileService.insertForestFile(fileUrl, savePath, md5, 1); succMap.put(orgName, localPath + fileName); } catch (IOException e) { errFiles.add(orgName); } + + } Map data = new HashMap(2); data.put("errFiles", errFiles); @@ -117,24 +207,6 @@ public class UploadController { return GlobalResultGenerator.genSuccessResult(data); } - private static String getTypePath(Integer type) { - String typePath; - switch (type) { - case 0: - typePath = "avatar"; - break; - case 1: - typePath = "article"; - break; - case 2: - typePath = "tags"; - break; - default: - typePath = "images"; - } - return typePath; - } - @GetMapping("/simple/token") public GlobalResult uploadSimpleToken(HttpServletRequest request) throws BaseApiException { String authHeader = request.getHeader(JwtConstants.AUTHORIZATION); @@ -164,7 +236,13 @@ public class UploadController { } @PostMapping("/file/link") - public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO) throws IOException { + @Transactional(rollbackFor = Exception.class) + public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO) throws IOException, BaseApiException { + //todo 无法获取当前登录用户 +// User user = UserUtils.getCurrentUserByToken(); +// if (Objects.isNull(user)) { +// throw new BaseApiException(ErrorCode.INVALID_TOKEN); +// } String url = linkToImageUrlDTO.getUrl(); URL link = new URL(url); HttpURLConnection conn = (HttpURLConnection) link.openConnection(); @@ -176,11 +254,9 @@ public class UploadController { //得到输入流 InputStream inputStream = conn.getInputStream(); - //获取自己数组 - byte[] getData = readInputStream(inputStream); // 获取文件md5值 - String md5 = DigestUtils.md5DigestAsHex(getData); + String md5 = DigestUtils.md5DigestAsHex(inputStream); String fileUrl = forestFileService.getFileUrlByMd5(md5); Map data = new HashMap(2); @@ -212,8 +288,10 @@ public class UploadController { File saveFile = new File(savePath); try { + //获取自己数组 + byte[] getData = readInputStream(inputStream); FileCopyUtils.copy(getData, saveFile); - forestFileService.insert(fileUrl, savePath, md5, 1); + forestFileService.insertForestFile(fileUrl, savePath, md5, 1); data.put("originalURL", url); data.put("url", fileUrl); } catch (IOException e) { @@ -223,46 +301,4 @@ public class UploadController { } - public static String uploadBase64File(String fileStr, Integer type) { - if (StringUtils.isBlank(fileStr)) { - return ""; - } - String typePath = getTypePath(type); - //图片存储路径 - String ctxHeadPicPath = env.getProperty("resource.pic-path"); - String dir = ctxHeadPicPath + "/" + typePath; - File file = new File(dir); - if (!file.exists()) { - file.mkdirs();// 创建文件根目录 - } - - String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/"; - String fileName = System.currentTimeMillis() + ".png"; - String savePath = file.getPath() + File.separator + fileName; - File saveFile = new File(savePath); - try { - FileCopyUtils.copy(Base64.decodeBase64(fileStr.substring(fileStr.indexOf(",") + 1)), saveFile); - fileStr = localPath + fileName; - } catch (IOException e) { - fileStr = "上传失败!"; - } - return fileStr; - } - - /** - * 从输入流中获取字节数组 - * @param inputStream - * @return - * @throws IOException - */ - public static byte[] readInputStream(InputStream inputStream) throws IOException { - byte[] buffer = new byte[1024]; - int len = 0; - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - while((len = inputStream.read(buffer)) != -1) { - bos.write(buffer, 0, len); - } - bos.close(); - return bos.toByteArray(); - } } diff --git a/src/main/java/mapper/ForestFileMapper.xml b/src/main/java/mapper/ForestFileMapper.xml index 2a103cd..8b37b3c 100644 --- a/src/main/java/mapper/ForestFileMapper.xml +++ b/src/main/java/mapper/ForestFileMapper.xml @@ -11,12 +11,12 @@ - - insert into forest_user_role (md5, file_path, file_url, created_time, created_by) + + insert into forest_file (md5_value, file_path, file_url, created_time, created_by) values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy}) - select file_url from forest_file where md5_value = #{md5Value} From 28365e7cb098c556495c32399bd4a6665392edd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Thu, 13 Jan 2022 15:42:04 +0800 Subject: [PATCH 6/6] :bug: add forest_file.table.sql --- src/main/resources/static/forest.sql | 37 ++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/resources/static/forest.sql b/src/main/resources/static/forest.sql index ca90822..b227878 100644 --- a/src/main/resources/static/forest.sql +++ b/src/main/resources/static/forest.sql @@ -346,17 +346,38 @@ create table forest_lucene_user_dic dic char(32) null comment '字典', constraint forest_lucene_user_dic_id_uindex unique (id) -) - comment '用户扩展字典'; +) comment '用户扩展字典'; alter table forest_lucene_user_dic add primary key (id); -insert into forest.forest_role (id, name, input_code, status, created_time, updated_time, weights) values (1, '管理员', 'admin', '0', '2019-11-16 04:22:45', '2019-11-16 04:22:45', 1); -insert into forest.forest_role (id, name, input_code, status, created_time, updated_time, weights) values (2, '社区管理员', 'blog_admin', '0', '2019-12-05 03:10:05', '2019-12-05 17:11:35', 2); -insert into forest.forest_role (id, name, input_code, status, created_time, updated_time, weights) values (3, '作者', 'zz', '0', '2020-03-12 15:07:27', '2020-03-12 15:07:27', 3); -insert into forest.forest_role (id, name, input_code, status, created_time, updated_time, weights) values (4, '普通用户', 'user', '0', '2019-12-05 03:10:59', '2020-03-12 15:13:49', 4); +insert into forest.forest_role (id, name, input_code, status, created_time, updated_time, weights) +values (1, '管理员', 'admin', '0', '2019-11-16 04:22:45', '2019-11-16 04:22:45', 1); +insert into forest.forest_role (id, name, input_code, status, created_time, updated_time, weights) +values (2, '社区管理员', 'blog_admin', '0', '2019-12-05 03:10:05', '2019-12-05 17:11:35', 2); +insert into forest.forest_role (id, name, input_code, status, created_time, updated_time, weights) +values (3, '作者', 'zz', '0', '2020-03-12 15:07:27', '2020-03-12 15:07:27', 3); +insert into forest.forest_role (id, name, input_code, status, created_time, updated_time, weights) +values (4, '普通用户', 'user', '0', '2019-12-05 03:10:59', '2020-03-12 15:13:49', 4); -insert into forest.forest_user (id, account, password, nickname, real_name, sex, avatar_type, avatar_url, email, phone, status, created_time, updated_time, last_login_time, signature) values (1, 'admin', '8ce2dd866238958ac4f07870766813cdaa39a9b83a8c75e26aa50f23', 'admin', 'admin', '0', '0', null, null, null, '0', '2021-01-25 18:21:51', '2021-01-25 18:21:54', null, null); +insert into forest.forest_user (id, account, password, nickname, real_name, sex, avatar_type, avatar_url, email, phone, + status, created_time, updated_time, last_login_time, signature) +values (1, 'admin', '8ce2dd866238958ac4f07870766813cdaa39a9b83a8c75e26aa50f23', 'admin', 'admin', '0', '0', null, null, + null, '0', '2021-01-25 18:21:51', '2021-01-25 18:21:54', null, null); -insert into forest.forest_user_role (id_user, id_role, created_time) values (1, 1, '2021-01-25 18:22:12'); +insert into forest.forest_user_role (id_user, id_role, created_time) +values (1, 1, '2021-01-25 18:22:12'); + + +CREATE TABLE `forest_file` +( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id', + `md5_value` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '文件md5值', + `file_path` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '文件上传路径', + `file_url` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '网络访问路径', + `created_time` datetime DEFAULT NULL COMMENT '创建时间', + `updated_time` datetime DEFAULT NULL COMMENT '更新时间', + `created_by` int(11) DEFAULT NULL COMMENT '创建人', + PRIMARY KEY (`id`), + UNIQUE KEY `index_md5_value` (`md5_value`) +) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci comment '文件上传记录表'; \ No newline at end of file