🎨 文件上传记录功能优化
🎨 文件上传记录功能优化
This commit is contained in:
commit
591b7bf1e6
@ -23,6 +23,17 @@ public class ForestFile {
|
|||||||
@Column(name = "id")
|
@Column(name = "id")
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件大小
|
||||||
|
*/
|
||||||
|
@Column(name = "file_size")
|
||||||
|
private long fileSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件类型-文件后缀
|
||||||
|
*/
|
||||||
|
@Column(name = "file_type")
|
||||||
|
private String fileType;
|
||||||
/**
|
/**
|
||||||
* 访问路径
|
* 访问路径
|
||||||
*/
|
*/
|
||||||
|
@ -13,10 +13,12 @@ public interface ForestFileMapper extends Mapper<ForestFile> {
|
|||||||
/**
|
/**
|
||||||
* 通过md5获取文件访问链接
|
* 通过md5获取文件访问链接
|
||||||
*
|
*
|
||||||
* @param md5Value md5值
|
* @param md5Value md5值
|
||||||
* @return
|
* @param createdBy 创建人
|
||||||
|
* @param createdBy 创建人
|
||||||
|
* @param fileType 文件类型
|
||||||
*/
|
*/
|
||||||
String getFileUrlByMd5(@Param("md5Value") String md5Value);
|
String getFileUrlByMd5(@Param("md5Value") String md5Value, @Param("createdBy") long createdBy, @Param("fileType") String fileType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插入文件对象
|
* 插入文件对象
|
||||||
@ -25,8 +27,10 @@ public interface ForestFileMapper extends Mapper<ForestFile> {
|
|||||||
* @param filePath 上传路径
|
* @param filePath 上传路径
|
||||||
* @param md5Value md5值
|
* @param md5Value md5值
|
||||||
* @param createdBy 创建人
|
* @param createdBy 创建人
|
||||||
|
* @param fileSize 文件大小
|
||||||
|
* @param fileType 文件类型
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
int insertForestFile(@Param("fileUrl") String fileUrl, @Param("filePath") String filePath, @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, @Param("fileSize") long fileSize, @Param("fileType") String fileType);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,12 @@ public interface ForestFileService extends Service<ForestFile> {
|
|||||||
/**
|
/**
|
||||||
* 通过md5获取文件访问链接
|
* 通过md5获取文件访问链接
|
||||||
*
|
*
|
||||||
* @param md5Value md5值
|
* @param md5Value md5值
|
||||||
|
* @param createdBy 用户id
|
||||||
|
* @param fileType 文件类型
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
String getFileUrlByMd5(String md5Value);
|
String getFileUrlByMd5(String md5Value, long createdBy, String fileType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插入文件对象
|
* 插入文件对象
|
||||||
@ -27,7 +29,9 @@ public interface ForestFileService extends Service<ForestFile> {
|
|||||||
* @param filePath 上传路径
|
* @param filePath 上传路径
|
||||||
* @param md5Value md5值
|
* @param md5Value md5值
|
||||||
* @param createdBy 创建人
|
* @param createdBy 创建人
|
||||||
|
* @param fileSize 文件大小
|
||||||
|
* @param fileType 文件类型
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy);
|
int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy, long fileSize, String fileType);
|
||||||
}
|
}
|
||||||
|
@ -22,12 +22,14 @@ public class ForestFileServiceImpl extends AbstractService<ForestFile> implement
|
|||||||
/**
|
/**
|
||||||
* 通过md5获取文件访问链接
|
* 通过md5获取文件访问链接
|
||||||
*
|
*
|
||||||
* @param md5Value md5值
|
* @param md5Value md5值
|
||||||
|
* @param createdBy 用户id
|
||||||
|
* @param fileType 文件类型
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String getFileUrlByMd5(String md5Value) {
|
public String getFileUrlByMd5(String md5Value, long createdBy, String fileType) {
|
||||||
return forestFileMapper.getFileUrlByMd5(md5Value);
|
return forestFileMapper.getFileUrlByMd5(md5Value, createdBy, fileType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,10 +39,12 @@ public class ForestFileServiceImpl extends AbstractService<ForestFile> implement
|
|||||||
* @param filePath 上传路径
|
* @param filePath 上传路径
|
||||||
* @param md5Value md5值
|
* @param md5Value md5值
|
||||||
* @param createdBy 创建人
|
* @param createdBy 创建人
|
||||||
|
* @param fileSize 文件大小
|
||||||
|
* @param fileType 文件类型
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy) {
|
public int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy, long fileSize, String fileType) {
|
||||||
return forestFileMapper.insertForestFile(fileUrl, filePath, md5Value, createdBy);
|
return forestFileMapper.insertForestFile(fileUrl, filePath, md5Value, createdBy, fileSize, fileType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ public class FileUtils {
|
|||||||
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
|
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文件扩展名
|
* 获取文件扩展名,带.
|
||||||
*
|
*
|
||||||
* @param filename
|
* @param filename
|
||||||
* @return
|
* @return
|
||||||
@ -31,6 +31,7 @@ public class FileUtils {
|
|||||||
* 获取文件扩展名
|
* 获取文件扩展名
|
||||||
*
|
*
|
||||||
* @param filename
|
* @param filename
|
||||||
|
* @param defExt 默认文件后缀名, 带.
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String getExtend(String filename, String defExt) {
|
public static String getExtend(String filename, String defExt) {
|
||||||
@ -41,7 +42,7 @@ public class FileUtils {
|
|||||||
int i = filename.lastIndexOf('.');
|
int i = filename.lastIndexOf('.');
|
||||||
|
|
||||||
if ((i > 0) && (i < (filename.length() - 1))) {
|
if ((i > 0) && (i < (filename.length() - 1))) {
|
||||||
String result = filename.substring(i + 1).toLowerCase();
|
String result = filename.substring(i).toLowerCase();
|
||||||
if (result.contains("?")) {
|
if (result.contains("?")) {
|
||||||
return result.split("\\?")[0];
|
return result.split("\\?")[0];
|
||||||
}
|
}
|
||||||
|
@ -42,8 +42,7 @@ public class UploadController {
|
|||||||
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 Environment env = SpringContextHolder.getBean(Environment.class);
|
private static final Environment env = SpringContextHolder.getBean(Environment.class);
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private ForestFileService forestFileService;
|
private ForestFileService forestFileService;
|
||||||
|
|
||||||
@ -120,8 +119,15 @@ public class UploadController {
|
|||||||
}
|
}
|
||||||
TokenUser tokenUser = getTokenUser(request);
|
TokenUser tokenUser = getTokenUser(request);
|
||||||
Map data = new HashMap(2);
|
Map data = new HashMap(2);
|
||||||
|
|
||||||
|
if (multipartFile.getSize() == 0) {
|
||||||
|
data.put("message", "上传失败!");
|
||||||
|
return GlobalResultGenerator.genSuccessResult(data);
|
||||||
|
}
|
||||||
String md5 = DigestUtils.md5DigestAsHex(multipartFile.getInputStream());
|
String md5 = DigestUtils.md5DigestAsHex(multipartFile.getInputStream());
|
||||||
String fileUrl = forestFileService.getFileUrlByMd5(md5);
|
String orgName = multipartFile.getOriginalFilename();
|
||||||
|
String fileType = FileUtils.getExtend(orgName);
|
||||||
|
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
|
||||||
if (StringUtils.isNotEmpty(fileUrl)) {
|
if (StringUtils.isNotEmpty(fileUrl)) {
|
||||||
data.put("url", fileUrl);
|
data.put("url", fileUrl);
|
||||||
return GlobalResultGenerator.genSuccessResult(data);
|
return GlobalResultGenerator.genSuccessResult(data);
|
||||||
@ -137,15 +143,15 @@ public class UploadController {
|
|||||||
|
|
||||||
String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/";
|
String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/";
|
||||||
|
|
||||||
String orgName = multipartFile.getOriginalFilename();
|
|
||||||
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(orgName).toLowerCase();
|
String fileName = System.currentTimeMillis() + fileType;
|
||||||
|
|
||||||
String savePath = file.getPath() + File.separator + fileName;
|
String savePath = file.getPath() + File.separator + fileName;
|
||||||
fileUrl = localPath + fileName;
|
fileUrl = localPath + fileName;
|
||||||
File saveFile = new File(savePath);
|
File saveFile = new File(savePath);
|
||||||
try {
|
try {
|
||||||
FileCopyUtils.copy(multipartFile.getBytes(), saveFile);
|
FileCopyUtils.copy(multipartFile.getBytes(), saveFile);
|
||||||
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser());
|
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), multipartFile.getSize(), fileType);
|
||||||
data.put("url", fileUrl);
|
data.put("url", fileUrl);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
data.put("message", "上传失败!");
|
data.put("message", "上传失败!");
|
||||||
@ -174,21 +180,25 @@ public class UploadController {
|
|||||||
for (int i = 0, len = multipartFiles.length; i < len; i++) {
|
for (int i = 0, len = multipartFiles.length; i < len; i++) {
|
||||||
MultipartFile multipartFile = multipartFiles[i];
|
MultipartFile multipartFile = multipartFiles[i];
|
||||||
String orgName = multipartFile.getOriginalFilename();
|
String orgName = multipartFile.getOriginalFilename();
|
||||||
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(orgName).toLowerCase();
|
|
||||||
|
if (multipartFile.getSize() == 0) {
|
||||||
|
errFiles.add(orgName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String fileType = FileUtils.getExtend(orgName);
|
||||||
|
String fileName = System.currentTimeMillis() + fileType;
|
||||||
String savePath = file.getPath() + File.separator + fileName;
|
String savePath = file.getPath() + File.separator + fileName;
|
||||||
File saveFile = new File(savePath);
|
File saveFile = new File(savePath);
|
||||||
try (InputStream in = multipartFiles[i].getInputStream();
|
try (InputStream in = multipartFiles[i].getInputStream(); OutputStream out = Files.newOutputStream(saveFile.toPath())) {
|
||||||
OutputStream out = Files.newOutputStream(saveFile.toPath())) {
|
|
||||||
String md5 = DigestUtils.md5DigestAsHex(in);
|
String md5 = DigestUtils.md5DigestAsHex(in);
|
||||||
String fileUrl = forestFileService.getFileUrlByMd5(md5);
|
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
|
||||||
if (StringUtils.isNotEmpty(fileUrl)) {
|
if (StringUtils.isNotEmpty(fileUrl)) {
|
||||||
successMap.put(orgName, fileUrl);
|
successMap.put(orgName, fileUrl);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
fileUrl = localPath + fileName;
|
fileUrl = localPath + fileName;
|
||||||
FileCopyUtils.copy(in, out);
|
FileCopyUtils.copy(in, out);
|
||||||
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser());
|
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), multipartFile.getSize(), fileType);
|
||||||
successMap.put(orgName, localPath + fileName);
|
successMap.put(orgName, localPath + fileName);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
errFiles.add(orgName);
|
errFiles.add(orgName);
|
||||||
@ -235,12 +245,9 @@ public class UploadController {
|
|||||||
|
|
||||||
@PostMapping("/file/link")
|
@PostMapping("/file/link")
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO) throws IOException, BaseApiException {
|
public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO, HttpServletRequest request) throws IOException, BaseApiException {
|
||||||
//todo 无法获取当前登录用户
|
|
||||||
// User user = UserUtils.getCurrentUserByToken();
|
TokenUser tokenUser = getTokenUser(request);
|
||||||
// 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();
|
||||||
@ -249,53 +256,57 @@ 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", "");
|
||||||
|
|
||||||
//得到输入流
|
|
||||||
InputStream inputStream = conn.getInputStream();
|
|
||||||
|
|
||||||
// 获取文件md5值
|
|
||||||
String md5 = DigestUtils.md5DigestAsHex(inputStream);
|
|
||||||
String fileUrl = forestFileService.getFileUrlByMd5(md5);
|
|
||||||
|
|
||||||
Map data = new HashMap(2);
|
Map data = new HashMap(2);
|
||||||
data.put("originalURL", url);
|
//得到输入流
|
||||||
|
try (InputStream inputStream = conn.getInputStream()) {
|
||||||
|
//获取自己数组
|
||||||
|
byte[] getData = readInputStream(inputStream);
|
||||||
|
if (getData.length == 0) {
|
||||||
|
data.put("message", "文件为空!");
|
||||||
|
return GlobalResultGenerator.genSuccessResult(data);
|
||||||
|
}
|
||||||
|
|
||||||
if (StringUtils.isNotEmpty(fileUrl)) {
|
// 获取文件md5值
|
||||||
|
String md5 = DigestUtils.md5DigestAsHex(inputStream);
|
||||||
|
String fileType = FileUtils.getExtend(url);
|
||||||
|
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
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 fileName = System.currentTimeMillis() + fileType;
|
||||||
|
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
|
||||||
|
|
||||||
|
String savePath = file.getPath() + File.separator + fileName;
|
||||||
|
File saveFile = new File(savePath);
|
||||||
|
|
||||||
|
FileCopyUtils.copy(getData, saveFile);
|
||||||
|
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), getData.length, fileType);
|
||||||
|
data.put("originalURL", url);
|
||||||
data.put("url", fileUrl);
|
data.put("url", fileUrl);
|
||||||
return GlobalResultGenerator.genSuccessResult(data);
|
return GlobalResultGenerator.genSuccessResult(data);
|
||||||
|
} catch (IOException e) {
|
||||||
|
data.put("message", "上传失败");
|
||||||
|
return GlobalResultGenerator.genSuccessResult(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
Integer type = linkToImageUrlDTO.getType();
|
|
||||||
if (Objects.isNull(type)) {
|
|
||||||
type = 1;
|
|
||||||
}
|
|
||||||
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 fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(url);
|
|
||||||
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
|
|
||||||
|
|
||||||
String savePath = file.getPath() + File.separator + fileName;
|
|
||||||
|
|
||||||
File saveFile = new File(savePath);
|
|
||||||
try {
|
|
||||||
//获取自己数组
|
|
||||||
byte[] getData = readInputStream(inputStream);
|
|
||||||
FileCopyUtils.copy(getData, saveFile);
|
|
||||||
forestFileService.insertForestFile(fileUrl, savePath, md5, 1);
|
|
||||||
data.put("originalURL", url);
|
|
||||||
data.put("url", fileUrl);
|
|
||||||
} catch (IOException e) {
|
|
||||||
data.put("message", "上传失败!");
|
|
||||||
}
|
|
||||||
return GlobalResultGenerator.genSuccessResult(data);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
<mapper namespace="com.rymcu.forest.mapper.ForestFileMapper">
|
<mapper namespace="com.rymcu.forest.mapper.ForestFileMapper">
|
||||||
<resultMap id="BaseResultMap" type="com.rymcu.forest.entity.ForestFile">
|
<resultMap id="BaseResultMap" type="com.rymcu.forest.entity.ForestFile">
|
||||||
<id column="id" property="id"/>
|
<id column="id" property="id"/>
|
||||||
|
<id column="file_size" property="fileSize"/>
|
||||||
|
<id column="file_type" property="fileType"/>
|
||||||
<result column="md5_value" property="md5Value"/>
|
<result column="md5_value" property="md5Value"/>
|
||||||
<result column="file_path" property="filePath"/>
|
<result column="file_path" property="filePath"/>
|
||||||
<result column="file_url" property="fileUrl"/>
|
<result column="file_url" property="fileUrl"/>
|
||||||
@ -12,14 +14,16 @@
|
|||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<insert id="insertForestFile">
|
<insert id="insertForestFile">
|
||||||
insert into forest_file (md5_value, file_path, file_url, created_time, created_by)
|
insert into forest_file (md5_value, file_path, file_url, created_time, created_by, file_size, file_type)
|
||||||
values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy})
|
values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy}, #{fileSize}, #{fileType})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
<select id="getFileUrlByMd5" resultType="java.lang.String">
|
<select id="getFileUrlByMd5" resultType="java.lang.String">
|
||||||
select file_url
|
select file_url
|
||||||
from forest_file
|
from forest_file
|
||||||
where md5_value = #{md5Value}
|
where md5_value = #{md5Value}
|
||||||
|
and created_by = #{createdBy}
|
||||||
|
and file_type = #{fileType}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
@ -384,4 +384,3 @@ create table forest_login_record
|
|||||||
constraint forest_login_record_id_uindex
|
constraint forest_login_record_id_uindex
|
||||||
unique (id)
|
unique (id)
|
||||||
) comment '登录记录表';
|
) comment '登录记录表';
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user