diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/file/IFileDao.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/file/IFileDao.java index 87b0c1b..47a690e 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/file/IFileDao.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/file/IFileDao.java @@ -32,6 +32,7 @@ public interface IFileDao { /** * 删除文件 + * * @param params * @throws RemoveException */ @@ -39,6 +40,7 @@ public interface IFileDao { /** * 删除文件(物理删除) + * * @param params * @throws RemoveException */ @@ -73,9 +75,18 @@ public interface IFileDao { /** * 获取文件列表(带路径) + * * @return * @throws SearchException */ List listFileInfoWithPath(Map params) throws SearchException; + /** + * 文件列表 + * + * @param fileMd5 文件MD5值 + * @return + * @throws SearchException + */ + List listFileByMd5(String fileMd5) throws SearchException; } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/dtos/file/FileInfoDTO.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/dtos/file/FileInfoDTO.java index b932bec..310af8e 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/dtos/file/FileInfoDTO.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/dtos/file/FileInfoDTO.java @@ -17,6 +17,8 @@ import io.swagger.annotations.ApiModelProperty; @ApiModel public class FileInfoDTO extends FileDTO { + @ApiModelProperty(name = "filePath", value = "文件全路径") + private String filePath; @ApiModelProperty(name = "fileUrl", value = "文件链接") private String fileUrl; @ApiModelProperty(name = "fileSummary", value = "文件说明") @@ -24,6 +26,14 @@ public class FileInfoDTO extends FileDTO { @ApiModelProperty(name = "isBack", value = "是否备份") private Integer isBack; + public String getFilePath() { + return filePath == null ? "" : filePath.trim(); + } + + public void setFilePath(String filePath) { + this.filePath = filePath; + } + @Override public String getFileUrl() { return fileUrl == null ? "" : fileUrl.trim(); @@ -53,7 +63,9 @@ public class FileInfoDTO extends FileDTO { @Override public String toString() { final StringBuilder sb = new StringBuilder("{"); - sb.append("\"fileUrl\":\"") + sb.append("\"filePath\":\"") + .append(filePath).append('\"'); + sb.append(",\"fileUrl\":\"") .append(fileUrl).append('\"'); sb.append(",\"fileSummary\":\"") .append(fileSummary).append('\"'); diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java index c5d13ce..7f8b7c0 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java @@ -42,6 +42,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; +import java.security.MessageDigest; import java.util.*; /** @@ -55,6 +56,7 @@ import java.util.*; public class FileServiceImpl extends AbstractService implements IFileService { private static final Logger LOG = LoggerFactory.getLogger(FileServiceImpl.class); + private static final char[] HEX_CODE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; @Autowired private FileProperties fileProperties; @@ -153,9 +155,26 @@ public class FileServiceImpl extends AbstractService implements IFileService { String uploadPath = getUploadPath(baseUploadPath, uploadTypeEnum, fileType); // 文件保存名称 String uploadFileName = getUploadFileName(fileType); - if (!uploadFile(uploadFile, uploadPath, uploadFileName)) { + String fileMd5 = uploadFile(uploadFile, uploadPath, uploadFileName); + if (fileMd5 == null) { throw new SaveException("文件上传失败"); } + // 获取MD5相同的文件 + List fileInfoDTOs = fileDao.listFileByMd5("MD5:" + fileMd5); + if (fileInfoDTOs.size() > 0) { + // 删除新增的文件 + File uploadedFile = new File(uploadPath + File.separator + uploadFileName); + if (uploadedFile.exists()) { + uploadedFile.delete(); + } + // 保存记录,但文件信息都是原有的文件 + params.clear(); + FileInfoDTO fileInfoDTO = fileInfoDTOs.get(0); + params.put("fileSummary", "REF:" + fileInfoDTO.getFileId()); + saveFile(token, params, fileInfoDTO.getFileName(), fileInfoDTO.getFilePath(), fileInfoDTO.getFileUrl(), fileInfoDTO.getFileType(), fileInfoDTO.getFileSize()); + return; + } + params.put("fileSummary", "MD5:" + fileMd5); saveUploadFileInfo(token, fileName, uploadPath, uploadFileName, fileType, fileSize, params); } @@ -205,10 +224,25 @@ public class FileServiceImpl extends AbstractService implements IFileService { File photo = new File(fileFullPath); fileSize = photo.length(); } + saveFile(token, params, fileName, fileFullPath, String.format("files/%s/%s", fixPath, uploadFileName), fileType, fileSize); + } + + /** + * 保存文件 + * + * @param token token + * @param params 参数 + * @param fileName 文件名 + * @param fileFullPath 文件全路径 + * @param fileUrl 文件相对地址 + * @param fileType 文件类型 + * @param fileSize 文件大小 + */ + private void saveFile(String token, Map params, String fileName, String fileFullPath, String fileUrl, String fileType, long fileSize) { params.put("fileId", UUIDUtil.getUUID()); params.put("fileName", fileName); params.put("filePath", fileFullPath); - params.put("fileUrl", String.format("files/%s/%s", fixPath, uploadFileName)); + params.put("fileUrl", fileUrl); params.put("fileType", fileType); params.put("fileSize", fileSize); params.put("isBack", 0); @@ -596,7 +630,8 @@ public class FileServiceImpl extends AbstractService implements IFileService { * @param uploadFileName * @return */ - private boolean uploadFile(MultipartFile uploadFile, String filePath, String uploadFileName) throws FileException { + private String uploadFile(MultipartFile uploadFile, String filePath, String uploadFileName) throws FileException { + String fileMd5; File uploadFolder = new File(filePath); if (!uploadFolder.exists()) { uploadFolder.mkdirs(); @@ -606,10 +641,20 @@ public class FileServiceImpl extends AbstractService implements IFileService { try { uploadFileInputStream = uploadFile.getInputStream(); uploadFileOutputStream = new FileOutputStream(new File(uploadFolder + "/" + uploadFileName)); + MessageDigest messageDigest = MessageDigest.getInstance("MD5"); for (byte[] buf = new byte[INPUT_STREAM_SIZE]; uploadFileInputStream.read(buf) > -1; ) { uploadFileOutputStream.write(buf, 0, buf.length); + messageDigest.update(buf, 0, buf.length); } uploadFileOutputStream.flush(); + // 计算文件的MD5 + byte[] data = messageDigest.digest(); + StringBuilder fileMd5SB = new StringBuilder(data.length * 2); + for (byte b : data) { + fileMd5SB.append(HEX_CODE[(b >> 4) & 0xF]); + fileMd5SB.append(HEX_CODE[(b & 0xF)]); + } + fileMd5 = fileMd5SB.toString(); } catch (Exception e) { LOG.error(e.getMessage(), e); throw new FileException("文件上传失败"); @@ -626,7 +671,7 @@ public class FileServiceImpl extends AbstractService implements IFileService { throw new FileException("文件上传失败"); } } - return true; + return fileMd5; } /** diff --git a/cloud-common-plugin/src/main/resources/mybatis/mapper/file/file-mapper.xml b/cloud-common-plugin/src/main/resources/mybatis/mapper/file/file-mapper.xml index ec86e98..b2ef460 100644 --- a/cloud-common-plugin/src/main/resources/mybatis/mapper/file/file-mapper.xml +++ b/cloud-common-plugin/src/main/resources/mybatis/mapper/file/file-mapper.xml @@ -32,7 +32,7 @@ - + @@ -111,7 +111,19 @@ SELECT - * + file_id, + file_name, + file_path, + file_url, + file_type, + file_size, + file_summary, + is_back, + creator, + gmt_create, + modifier, + gmt_modified, + is_delete FROM sys_file WHERE @@ -132,6 +156,10 @@ AND file_name LIKE CONCAT('%', #{keywords}, '%') + + AND + file_summary = #{fileSummary} + AND LEFT(gmt_create, 10) = ]]> #{startTime} @@ -156,7 +184,19 @@ SELECT - * + file_id, + file_name, + file_path, + file_url, + file_type, + file_size, + file_summary, + is_back, + creator, + gmt_create, + modifier, + gmt_modified, + is_delete FROM sys_file WHERE @@ -199,4 +251,28 @@ + + + \ No newline at end of file