commit
24d1da8bc8
@ -14,19 +14,23 @@ import com.rymcu.forest.web.api.exception.BaseApiException;
|
|||||||
import com.rymcu.forest.web.api.exception.ErrorCode;
|
import com.rymcu.forest.web.api.exception.ErrorCode;
|
||||||
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.codec.binary.Base64;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.DigestUtils;
|
import org.springframework.util.DigestUtils;
|
||||||
import org.springframework.util.FileCopyUtils;
|
import org.springframework.util.FileCopyUtils;
|
||||||
|
import org.springframework.util.MimeTypeUtils;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
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.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.util.*;
|
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_SIMPLE_URL = "/api/upload/file";
|
||||||
private final static String UPLOAD_URL = "/api/upload/file/batch";
|
private final static String UPLOAD_URL = "/api/upload/file/batch";
|
||||||
private final static String LINK_TO_IMAGE_URL = "/api/upload/file/link";
|
private final static String LINK_TO_IMAGE_URL = "/api/upload/file/link";
|
||||||
|
|
||||||
private static final Environment env = SpringContextHolder.getBean(Environment.class);
|
private static final Environment env = SpringContextHolder.getBean(Environment.class);
|
||||||
|
private final org.slf4j.Logger logger = LoggerFactory.getLogger(UploadController.class);
|
||||||
@Resource
|
@Resource
|
||||||
private ForestFileService forestFileService;
|
private ForestFileService forestFileService;
|
||||||
|
|
||||||
@ -186,9 +190,6 @@ public class UploadController {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String fileType = FileUtils.getExtend(orgName);
|
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()) {
|
try (InputStream in = multipartFile.getInputStream()) {
|
||||||
String md5 = DigestUtils.md5DigestAsHex(in);
|
String md5 = DigestUtils.md5DigestAsHex(in);
|
||||||
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
|
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
|
||||||
@ -196,6 +197,9 @@ public class UploadController {
|
|||||||
successMap.put(orgName, fileUrl);
|
successMap.put(orgName, fileUrl);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
String fileName = System.currentTimeMillis() + fileType;
|
||||||
|
String savePath = file.getPath() + File.separator + fileName;
|
||||||
|
File saveFile = new File(savePath);
|
||||||
fileUrl = localPath + fileName;
|
fileUrl = localPath + fileName;
|
||||||
FileCopyUtils.copy(multipartFile.getBytes(), saveFile);
|
FileCopyUtils.copy(multipartFile.getBytes(), saveFile);
|
||||||
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), multipartFile.getSize(), fileType);
|
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), multipartFile.getSize(), fileType);
|
||||||
@ -249,6 +253,17 @@ public class UploadController {
|
|||||||
|
|
||||||
TokenUser tokenUser = getTokenUser(request);
|
TokenUser tokenUser = getTokenUser(request);
|
||||||
String url = linkToImageUrlDTO.getUrl();
|
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);
|
URL link = new URL(url);
|
||||||
HttpURLConnection conn = (HttpURLConnection) link.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) link.openConnection();
|
||||||
//设置超时间为3秒
|
//设置超时间为3秒
|
||||||
@ -256,7 +271,7 @@ public class UploadController {
|
|||||||
//防止屏蔽程序抓取而返回403错误
|
//防止屏蔽程序抓取而返回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("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", "");
|
conn.setRequestProperty("referer", "");
|
||||||
Map data = new HashMap(2);
|
|
||||||
//得到输入流
|
//得到输入流
|
||||||
try (InputStream inputStream = conn.getInputStream()) {
|
try (InputStream inputStream = conn.getInputStream()) {
|
||||||
//获取自己数组
|
//获取自己数组
|
||||||
@ -265,10 +280,9 @@ public class UploadController {
|
|||||||
data.put("message", "文件为空!");
|
data.put("message", "文件为空!");
|
||||||
return GlobalResultGenerator.genSuccessResult(data);
|
return GlobalResultGenerator.genSuccessResult(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取文件md5值
|
// 获取文件md5值
|
||||||
String md5 = DigestUtils.md5DigestAsHex(inputStream);
|
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);
|
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
|
||||||
|
|
||||||
data.put("originalURL", url);
|
data.put("originalURL", url);
|
||||||
@ -290,7 +304,6 @@ public class UploadController {
|
|||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
file.mkdirs();// 创建文件根目录
|
file.mkdirs();// 创建文件根目录
|
||||||
}
|
}
|
||||||
|
|
||||||
String fileName = System.currentTimeMillis() + fileType;
|
String fileName = System.currentTimeMillis() + fileType;
|
||||||
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
|
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
|
||||||
|
|
||||||
@ -303,11 +316,15 @@ public class UploadController {
|
|||||||
data.put("url", fileUrl);
|
data.put("url", fileUrl);
|
||||||
return GlobalResultGenerator.genSuccessResult(data);
|
return GlobalResultGenerator.genSuccessResult(data);
|
||||||
} catch (IOException e) {
|
} 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);
|
return GlobalResultGenerator.genSuccessResult(data);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -1,14 +1,21 @@
|
|||||||
package com.rymcu.forest.utils;
|
package com.rymcu.forest.utils;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
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 org.springframework.util.DigestUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
|
// 仅运行指定类
|
||||||
|
@SpringBootTest(classes = TestFileMd5.class)
|
||||||
public class TestFileMd5 {
|
public class TestFileMd5 {
|
||||||
|
|
||||||
|
|
||||||
|
@Value("classpath:1.txt")
|
||||||
|
private Resource testFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* c6c26c7e8a5eb493b14e84bd91df60e3
|
* c6c26c7e8a5eb493b14e84bd91df60e3
|
||||||
* d41d8cd98f00b204e9800998ecf8427e
|
* d41d8cd98f00b204e9800998ecf8427e
|
||||||
@ -17,10 +24,7 @@ public class TestFileMd5 {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void test() throws Exception {
|
public void test() throws Exception {
|
||||||
String pathName = "E:\\1.txt";
|
String md5 = DigestUtils.md5DigestAsHex(testFile.getInputStream());
|
||||||
InputStream inputStream = new FileInputStream(new File(pathName));
|
assertEquals("202cb962ac59075b964b07152d234b70", md5);
|
||||||
String md5 = DigestUtils.md5DigestAsHex((inputStream));
|
|
||||||
System.err.println(md5);
|
|
||||||
System.err.println(md5.length());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
src/test/resources/1.txt
Normal file
1
src/test/resources/1.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
123
|
Loading…
Reference in New Issue
Block a user