From 1b2bf0fc0386c4198e86694d31bbd0d0e2be69c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Fri, 14 Jan 2022 15:25:38 +0800 Subject: [PATCH 1/8] :bug: fixed empty file --- .../forest/web/api/common/UploadController.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) 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 98a21cd..210e1a1 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 @@ -23,10 +23,12 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; -import java.io.*; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; -import java.nio.file.Files; import java.util.*; /** @@ -186,9 +188,6 @@ public class UploadController { continue; } String fileType = FileUtils.getExtend(orgName); - String fileName = System.currentTimeMillis() + fileType; - String savePath = file.getPath() + File.separator + fileName; - File saveFile = new File(savePath); try (InputStream in = multipartFile.getInputStream()) { String md5 = DigestUtils.md5DigestAsHex(in); String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType); @@ -196,6 +195,9 @@ public class UploadController { successMap.put(orgName, fileUrl); continue; } + String fileName = System.currentTimeMillis() + fileType; + String savePath = file.getPath() + File.separator + fileName; + File saveFile = new File(savePath); fileUrl = localPath + fileName; FileCopyUtils.copy(multipartFile.getBytes(), saveFile); forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), multipartFile.getSize(), fileType); From 934b0efa4c12389eb0cf06db272b1083c1e61676 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, 20 Jan 2022 15:29:28 +0800 Subject: [PATCH 2/8] :bug: junit test --- pom.xml | 10 ++++++++++ .../com/rymcu/forest/utils/TestFileMd5.java | 20 +++++++++++-------- src/test/resources/1.txt | 1 + 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 src/test/resources/1.txt diff --git a/pom.xml b/pom.xml index 2d18bc8..6596ea2 100644 --- a/pom.xml +++ b/pom.xml @@ -296,6 +296,16 @@ + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + + true + + org.springframework.boot spring-boot-maven-plugin diff --git a/src/test/java/com/rymcu/forest/utils/TestFileMd5.java b/src/test/java/com/rymcu/forest/utils/TestFileMd5.java index 5a5f2be..17fe9f0 100644 --- a/src/test/java/com/rymcu/forest/utils/TestFileMd5.java +++ b/src/test/java/com/rymcu/forest/utils/TestFileMd5.java @@ -1,14 +1,21 @@ package com.rymcu.forest.utils; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.io.Resource; import org.springframework.util.DigestUtils; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; +import static org.junit.jupiter.api.Assertions.assertEquals; +// 仅运行指定类 +@SpringBootTest(classes = TestFileMd5.class) public class TestFileMd5 { + + @Value("classpath:1.txt") + private Resource testFile; + /** * c6c26c7e8a5eb493b14e84bd91df60e3 * d41d8cd98f00b204e9800998ecf8427e @@ -17,10 +24,7 @@ public class TestFileMd5 { */ @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()); + String md5 = DigestUtils.md5DigestAsHex(testFile.getInputStream()); + assertEquals("202cb962ac59075b964b07152d234b70", md5); } } diff --git a/src/test/resources/1.txt b/src/test/resources/1.txt new file mode 100644 index 0000000..d800886 --- /dev/null +++ b/src/test/resources/1.txt @@ -0,0 +1 @@ +123 \ No newline at end of file From e96759b2b217a3147e391d145d308491bc34c816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Fri, 21 Jan 2022 15:46:48 +0800 Subject: [PATCH 3/8] :bug: ignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 3650798..3145aa6 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ build/ ### lucene ### index userDic + +/src/main/resources/*.yml \ No newline at end of file From 644ba31d1cc4bacf8e1314eb0f6e36ef5439177a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Fri, 21 Jan 2022 15:47:09 +0800 Subject: [PATCH 4/8] :bug: add upload md5 error: --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3145aa6..6504885 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,4 @@ build/ index userDic -/src/main/resources/*.yml \ No newline at end of file +/src/main/resources/application.yml \ No newline at end of file From 12ccb9b7d0386932d70ba3761786238bf4b08ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Fri, 21 Jan 2022 15:49:53 +0800 Subject: [PATCH 5/8] :bug: fixed --- .gitignore | 3 ++- pom.xml | 10 ---------- .../forest/web/api/common/UploadController.java | 14 +++++++++++--- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 6504885..948d99e 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,5 @@ build/ index userDic -/src/main/resources/application.yml \ No newline at end of file + +*.yml diff --git a/pom.xml b/pom.xml index 6596ea2..2d18bc8 100644 --- a/pom.xml +++ b/pom.xml @@ -296,16 +296,6 @@ - - - org.apache.maven.plugins - maven-surefire-plugin - 3.0.0-M5 - - - true - - org.springframework.boot spring-boot-maven-plugin 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 210e1a1..f6b8160 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 @@ -251,6 +251,17 @@ public class UploadController { TokenUser tokenUser = getTokenUser(request); String url = linkToImageUrlDTO.getUrl(); + Map data = new HashMap(2); + + if (StringUtils.isBlank(url)) { + data.put("message", "文件为空!"); + return GlobalResultGenerator.genSuccessResult(data); + } + if (url.contains(Utils.getProperty("resource.file-path"))) { + data.put("originalURL", url); + data.put("url", url); + return GlobalResultGenerator.genSuccessResult(data); + } URL link = new URL(url); HttpURLConnection conn = (HttpURLConnection) link.openConnection(); //设置超时间为3秒 @@ -258,7 +269,6 @@ public class UploadController { //防止屏蔽程序抓取而返回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", ""); - Map data = new HashMap(2); //得到输入流 try (InputStream inputStream = conn.getInputStream()) { //获取自己数组 @@ -308,8 +318,6 @@ public class UploadController { data.put("message", "上传失败"); return GlobalResultGenerator.genSuccessResult(data); } - - } } From 7c6abc3db1901f61afbe1dd0f4702a2a6ccd1dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Fri, 21 Jan 2022 16:01:04 +0800 Subject: [PATCH 6/8] =?UTF-8?q?:bug:=20.png=20=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/rymcu/forest/util/FileUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/rymcu/forest/util/FileUtils.java b/src/main/java/com/rymcu/forest/util/FileUtils.java index 696c30b..fda6463 100644 --- a/src/main/java/com/rymcu/forest/util/FileUtils.java +++ b/src/main/java/com/rymcu/forest/util/FileUtils.java @@ -24,7 +24,7 @@ public class FileUtils { * @return */ public static String getExtend(String filename) { - return getExtend(filename, ""); + return getExtend(filename, ".png"); } /** From e37b52d42d331d0e441941b2d034ac683443b6ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Fri, 21 Jan 2022 17:31:03 +0800 Subject: [PATCH 7/8] :bug: fixed uupload link --- .../java/com/rymcu/forest/util/FileUtils.java | 2 +- .../forest/web/api/common/UploadController.java | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/rymcu/forest/util/FileUtils.java b/src/main/java/com/rymcu/forest/util/FileUtils.java index fda6463..696c30b 100644 --- a/src/main/java/com/rymcu/forest/util/FileUtils.java +++ b/src/main/java/com/rymcu/forest/util/FileUtils.java @@ -24,7 +24,7 @@ public class FileUtils { * @return */ public static String getExtend(String filename) { - return getExtend(filename, ".png"); + return getExtend(filename, ""); } /** 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 f6b8160..a3ff0a9 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 @@ -14,10 +14,12 @@ import com.rymcu.forest.web.api.exception.BaseApiException; import com.rymcu.forest.web.api.exception.ErrorCode; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; +import org.slf4j.LoggerFactory; 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.util.MimeTypeUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -43,8 +45,8 @@ public class UploadController { private final static String UPLOAD_SIMPLE_URL = "/api/upload/file"; private final static String UPLOAD_URL = "/api/upload/file/batch"; private final static String LINK_TO_IMAGE_URL = "/api/upload/file/link"; - private static final Environment env = SpringContextHolder.getBean(Environment.class); + private final org.slf4j.Logger logger = LoggerFactory.getLogger(UploadController.class); @Resource private ForestFileService forestFileService; @@ -269,6 +271,7 @@ public class UploadController { //防止屏蔽程序抓取而返回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", ""); + //得到输入流 try (InputStream inputStream = conn.getInputStream()) { //获取自己数组 @@ -277,10 +280,9 @@ public class UploadController { data.put("message", "文件为空!"); return GlobalResultGenerator.genSuccessResult(data); } - // 获取文件md5值 String md5 = DigestUtils.md5DigestAsHex(inputStream); - String fileType = FileUtils.getExtend(url); + String fileType = "." + MimeTypeUtils.parseMimeType(conn.getContentType()).getSubtype(); String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType); data.put("originalURL", url); @@ -302,7 +304,6 @@ public class UploadController { if (!file.exists()) { file.mkdirs();// 创建文件根目录 } - String fileName = System.currentTimeMillis() + fileType; fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName; @@ -315,9 +316,15 @@ public class UploadController { data.put("url", fileUrl); return GlobalResultGenerator.genSuccessResult(data); } catch (IOException e) { - data.put("message", "上传失败"); + /** + * 上传失败返回原链接 + */ + logger.error("link: {},\nmessage: {}", url, e.getMessage()); + data.put("originalURL", url); + data.put("url", url); return GlobalResultGenerator.genSuccessResult(data); } } + } From d6904d84d123421884e628e8231e63492b733e6b Mon Sep 17 00:00:00 2001 From: ronger Date: Sun, 23 Jan 2022 11:37:57 +0800 Subject: [PATCH 8/8] Update .gitignore --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index 948d99e..3650798 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,3 @@ build/ ### lucene ### index userDic - - -*.yml