Merge pull request #76 from acaterpillar/master

🐛 fixed empty file
This commit is contained in:
ronger 2022-01-24 15:19:17 +08:00 committed by GitHub
commit 24d1da8bc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 21 deletions

View File

@ -14,19 +14,23 @@ 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;
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.*;
/**
@ -41,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;
@ -186,9 +190,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 +197,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);
@ -249,6 +253,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秒
@ -256,7 +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", "");
Map data = new HashMap(2);
//得到输入流
try (InputStream inputStream = conn.getInputStream()) {
//获取自己数组
@ -265,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);
@ -290,7 +304,6 @@ public class UploadController {
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
String fileName = System.currentTimeMillis() + fileType;
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
@ -303,11 +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);
}
}
}
}

View File

@ -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);
}
}

1
src/test/resources/1.txt Normal file
View File

@ -0,0 +1 @@
123