Merge branch 'wx-dev' of https://github.com/rymcu/forest into wx-dev
This commit is contained in:
commit
5cdf2d5c04
63
src/main/java/com/rymcu/forest/entity/ForestFile.java
Normal file
63
src/main/java/com/rymcu/forest/entity/ForestFile.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
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 = "file_url")
|
||||||
|
private String fileUrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传路径
|
||||||
|
*/
|
||||||
|
@Column(name = "file_path")
|
||||||
|
private String filePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* md5
|
||||||
|
*/
|
||||||
|
@Column(name = "md5_value")
|
||||||
|
private String md5Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建时间
|
||||||
|
*/
|
||||||
|
@Column(name = "created_time")
|
||||||
|
private Date createdTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新时间
|
||||||
|
*/
|
||||||
|
@Column(name = "updated_time")
|
||||||
|
private Date updatedTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建人
|
||||||
|
*/
|
||||||
|
@Column(name = "created_by")
|
||||||
|
private long createdBy;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
32
src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java
Normal file
32
src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
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<ForestFile> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过md5获取文件访问链接
|
||||||
|
*
|
||||||
|
* @param md5Value md5值
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String getFileUrlByMd5(@Param("md5Value") String md5Value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入文件对象
|
||||||
|
*
|
||||||
|
* @param fileUrl 访问路径
|
||||||
|
* @param filePath 上传路径
|
||||||
|
* @param md5Value md5值
|
||||||
|
* @param createdBy 创建人
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int insertForestFile(@Param("fileUrl") String fileUrl, @Param("filePath") String filePath, @Param("md5Value") String md5Value, @Param("createdBy") long createdBy);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.rymcu.forest.service;
|
||||||
|
|
||||||
|
import com.rymcu.forest.core.service.Service;
|
||||||
|
import com.rymcu.forest.entity.ForestFile;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件服务记录
|
||||||
|
*
|
||||||
|
* @author caterpillar
|
||||||
|
* @date 2022-1-12 22:32:49
|
||||||
|
*/
|
||||||
|
public interface ForestFileService extends Service<ForestFile> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过md5获取文件访问链接
|
||||||
|
*
|
||||||
|
* @param md5Value md5值
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String getFileUrlByMd5(String md5Value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入文件对象
|
||||||
|
*
|
||||||
|
* @param fileUrl 访问路径
|
||||||
|
* @param filePath 上传路径
|
||||||
|
* @param md5Value md5值
|
||||||
|
* @param createdBy 创建人
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy);
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author caterpillar
|
||||||
|
* @date 2022-1-12 22:34:55
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class ForestFileServiceImpl extends AbstractService<ForestFile> implements ForestFileService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ForestFileMapper forestFileMapper;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过md5获取文件访问链接
|
||||||
|
*
|
||||||
|
* @param md5Value md5值
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String getFileUrlByMd5(String md5Value) {
|
||||||
|
return forestFileMapper.getFileUrlByMd5(md5Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入文件对象
|
||||||
|
*
|
||||||
|
* @param fileUrl 访问路径
|
||||||
|
* @param filePath 上传路径
|
||||||
|
* @param md5Value md5值
|
||||||
|
* @param createdBy 创建人
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy) {
|
||||||
|
return forestFileMapper.insertForestFile(fileUrl, filePath, md5Value, createdBy);
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ import com.rymcu.forest.core.result.GlobalResultGenerator;
|
|||||||
import com.rymcu.forest.dto.LinkToImageUrlDTO;
|
import com.rymcu.forest.dto.LinkToImageUrlDTO;
|
||||||
import com.rymcu.forest.dto.TokenUser;
|
import com.rymcu.forest.dto.TokenUser;
|
||||||
import com.rymcu.forest.jwt.def.JwtConstants;
|
import com.rymcu.forest.jwt.def.JwtConstants;
|
||||||
|
import com.rymcu.forest.service.ForestFileService;
|
||||||
import com.rymcu.forest.util.FileUtils;
|
import com.rymcu.forest.util.FileUtils;
|
||||||
import com.rymcu.forest.util.SpringContextHolder;
|
import com.rymcu.forest.util.SpringContextHolder;
|
||||||
import com.rymcu.forest.util.UserUtils;
|
import com.rymcu.forest.util.UserUtils;
|
||||||
@ -14,17 +15,18 @@ 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.springframework.core.env.Environment;
|
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.FileCopyUtils;
|
||||||
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.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.*;
|
||||||
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.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,74 +44,8 @@ public class UploadController {
|
|||||||
|
|
||||||
private static Environment env = SpringContextHolder.getBean(Environment.class);
|
private static Environment env = SpringContextHolder.getBean(Environment.class);
|
||||||
|
|
||||||
@PostMapping("/file")
|
@Resource
|
||||||
public GlobalResult uploadPicture(@RequestParam(value = "file", required = false) MultipartFile multipartFile, @RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) {
|
private ForestFileService forestFileService;
|
||||||
if (multipartFile == null) {
|
|
||||||
return GlobalResultGenerator.genErrorResult("请选择要上传的文件");
|
|
||||||
}
|
|
||||||
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 orgName = multipartFile.getOriginalFilename();
|
|
||||||
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(orgName).toLowerCase();
|
|
||||||
|
|
||||||
String savePath = file.getPath() + File.separator + fileName;
|
|
||||||
|
|
||||||
Map data = new HashMap(2);
|
|
||||||
File saveFile = new File(savePath);
|
|
||||||
try {
|
|
||||||
FileCopyUtils.copy(multipartFile.getBytes(), saveFile);
|
|
||||||
data.put("url", localPath + fileName);
|
|
||||||
} catch (IOException e) {
|
|
||||||
data.put("message", "上传失败!");
|
|
||||||
}
|
|
||||||
return GlobalResultGenerator.genSuccessResult(data);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/file/batch")
|
|
||||||
public GlobalResult batchFileUpload(@RequestParam(value = "file[]", required = false) MultipartFile[] multipartFiles, @RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) {
|
|
||||||
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 + "/";
|
|
||||||
Map succMap = new HashMap(10);
|
|
||||||
Set errFiles = new HashSet();
|
|
||||||
|
|
||||||
for (int i = 0, len = multipartFiles.length; i < len; i++) {
|
|
||||||
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);
|
|
||||||
succMap.put(orgName, localPath + fileName);
|
|
||||||
} catch (IOException e) {
|
|
||||||
errFiles.add(orgName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Map data = new HashMap(2);
|
|
||||||
data.put("errFiles", errFiles);
|
|
||||||
data.put("succMap", succMap);
|
|
||||||
return GlobalResultGenerator.genSuccessResult(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getTypePath(Integer type) {
|
private static String getTypePath(Integer type) {
|
||||||
String typePath;
|
String typePath;
|
||||||
@ -129,6 +65,148 @@ public class UploadController {
|
|||||||
return typePath;
|
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")
|
||||||
|
@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");
|
||||||
|
String dir = ctxHeadPicPath + "/" + typePath;
|
||||||
|
File file = new File(dir);
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.mkdirs();// 创建文件根目录
|
||||||
|
}
|
||||||
|
|
||||||
|
String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/";
|
||||||
|
|
||||||
|
String orgName = multipartFile.getOriginalFilename();
|
||||||
|
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(orgName).toLowerCase();
|
||||||
|
|
||||||
|
String savePath = file.getPath() + File.separator + fileName;
|
||||||
|
fileUrl = localPath + fileName;
|
||||||
|
File saveFile = new File(savePath);
|
||||||
|
try {
|
||||||
|
FileCopyUtils.copy(multipartFile.getBytes(), saveFile);
|
||||||
|
forestFileService.insertForestFile(fileUrl, savePath, md5, 1);
|
||||||
|
data.put("url", fileUrl);
|
||||||
|
} catch (IOException e) {
|
||||||
|
data.put("message", "上传失败!");
|
||||||
|
}
|
||||||
|
return GlobalResultGenerator.genSuccessResult(data);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/file/batch")
|
||||||
|
@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");
|
||||||
|
String dir = ctxHeadPicPath + "/" + typePath;
|
||||||
|
File file = new File(dir);
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.mkdirs();// 创建文件根目录
|
||||||
|
}
|
||||||
|
|
||||||
|
String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/";
|
||||||
|
Map succMap = new HashMap(10);
|
||||||
|
Set errFiles = new HashSet();
|
||||||
|
|
||||||
|
for (int i = 0, len = multipartFiles.length; i < len; i++) {
|
||||||
|
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 (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);
|
||||||
|
data.put("succMap", succMap);
|
||||||
|
return GlobalResultGenerator.genSuccessResult(data);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/simple/token")
|
@GetMapping("/simple/token")
|
||||||
public GlobalResult uploadSimpleToken(HttpServletRequest request) throws BaseApiException {
|
public GlobalResult uploadSimpleToken(HttpServletRequest request) throws BaseApiException {
|
||||||
String authHeader = request.getHeader(JwtConstants.AUTHORIZATION);
|
String authHeader = request.getHeader(JwtConstants.AUTHORIZATION);
|
||||||
@ -158,20 +236,37 @@ public class UploadController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/file/link")
|
@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();
|
String url = linkToImageUrlDTO.getUrl();
|
||||||
URL link = new URL(url);
|
URL link = new URL(url);
|
||||||
HttpURLConnection conn = (HttpURLConnection)link.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) link.openConnection();
|
||||||
//设置超时间为3秒
|
//设置超时间为3秒
|
||||||
conn.setConnectTimeout(3*1000);
|
conn.setConnectTimeout(3 * 1000);
|
||||||
//防止屏蔽程序抓取而返回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", "");
|
||||||
|
|
||||||
//得到输入流
|
//得到输入流
|
||||||
InputStream inputStream = conn.getInputStream();
|
InputStream inputStream = conn.getInputStream();
|
||||||
//获取自己数组
|
|
||||||
byte[] getData = readInputStream(inputStream);
|
// 获取文件md5值
|
||||||
|
String md5 = DigestUtils.md5DigestAsHex(inputStream);
|
||||||
|
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();
|
Integer type = linkToImageUrlDTO.getType();
|
||||||
if (Objects.isNull(type)) {
|
if (Objects.isNull(type)) {
|
||||||
type = 1;
|
type = 1;
|
||||||
@ -185,18 +280,20 @@ public class UploadController {
|
|||||||
file.mkdirs();// 创建文件根目录
|
file.mkdirs();// 创建文件根目录
|
||||||
}
|
}
|
||||||
|
|
||||||
String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/";
|
|
||||||
|
|
||||||
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(url);
|
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(url);
|
||||||
|
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
|
||||||
|
|
||||||
String savePath = file.getPath() + File.separator + fileName;
|
String savePath = file.getPath() + File.separator + fileName;
|
||||||
|
|
||||||
Map data = new HashMap(2);
|
|
||||||
File saveFile = new File(savePath);
|
File saveFile = new File(savePath);
|
||||||
try {
|
try {
|
||||||
|
//获取自己数组
|
||||||
|
byte[] getData = readInputStream(inputStream);
|
||||||
FileCopyUtils.copy(getData, saveFile);
|
FileCopyUtils.copy(getData, saveFile);
|
||||||
|
forestFileService.insertForestFile(fileUrl, savePath, md5, 1);
|
||||||
data.put("originalURL", url);
|
data.put("originalURL", url);
|
||||||
data.put("url", localPath + fileName);
|
data.put("url", fileUrl);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
data.put("message", "上传失败!");
|
data.put("message", "上传失败!");
|
||||||
}
|
}
|
||||||
@ -204,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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
25
src/main/java/mapper/ForestFileMapper.xml
Normal file
25
src/main/java/mapper/ForestFileMapper.xml
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.rymcu.forest.mapper.ForestFileMapper">
|
||||||
|
<resultMap id="BaseResultMap" type="com.rymcu.forest.entity.ForestFile">
|
||||||
|
<id column="id" property="id"/>
|
||||||
|
<result column="md5_value" property="md5Value"/>
|
||||||
|
<result column="file_path" property="filePath"/>
|
||||||
|
<result column="file_url" property="fileUrl"/>
|
||||||
|
<result column="created_time" property="createdTime"/>
|
||||||
|
<result column="updated_time" property="updatedTime"/>
|
||||||
|
<result column="created_by" property="createdBy"/>
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<insert id="insertForestFile">
|
||||||
|
insert into forest_file (md5_value, file_path, file_url, created_time, created_by)
|
||||||
|
values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy})
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
<select id="getFileUrlByMd5" resultType="java.lang.String">
|
||||||
|
select file_url
|
||||||
|
from forest_file
|
||||||
|
where md5_value = #{md5Value}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
</mapper>
|
@ -346,17 +346,38 @@ create table forest_lucene_user_dic
|
|||||||
dic char(32) null comment '字典',
|
dic char(32) null comment '字典',
|
||||||
constraint forest_lucene_user_dic_id_uindex
|
constraint forest_lucene_user_dic_id_uindex
|
||||||
unique (id)
|
unique (id)
|
||||||
)
|
) comment '用户扩展字典';
|
||||||
comment '用户扩展字典';
|
|
||||||
|
|
||||||
alter table forest_lucene_user_dic
|
alter table forest_lucene_user_dic
|
||||||
add primary key (id);
|
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)
|
||||||
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);
|
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 (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)
|
||||||
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);
|
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 '文件上传记录表';
|
26
src/test/java/com/rymcu/forest/utils/TestFileMd5.java
Normal file
26
src/test/java/com/rymcu/forest/utils/TestFileMd5.java
Normal file
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user