diff --git a/src/main/java/com/rymcu/forest/entity/ForestFile.java b/src/main/java/com/rymcu/forest/entity/ForestFile.java index d2c3f4d..58a8dfa 100644 --- a/src/main/java/com/rymcu/forest/entity/ForestFile.java +++ b/src/main/java/com/rymcu/forest/entity/ForestFile.java @@ -23,6 +23,17 @@ public class ForestFile { @Column(name = "id") private Long id; + /** + * 文件大小 + */ + @Column(name = "file_size") + private long fileSize; + + /** + * 文件类型-文件后缀 + */ + @Column(name = "file_type") + private String fileType; /** * 访问路径 */ diff --git a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java index 327f217..96810cd 100644 --- a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java +++ b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java @@ -15,9 +15,10 @@ public interface ForestFileMapper extends Mapper { * * @param md5Value md5值 * @param createdBy 创建人 - * @return + * @param createdBy 创建人 + * @param fileType 文件类型 */ - String getFileUrlByMd5(@Param("md5Value") String md5Value, @Param("createdBy") long createdBy); + String getFileUrlByMd5(@Param("md5Value") String md5Value, @Param("createdBy") long createdBy, @Param("fileType") String fileType); /** * 插入文件对象 @@ -26,8 +27,10 @@ public interface ForestFileMapper extends Mapper { * @param filePath 上传路径 * @param md5Value md5值 * @param createdBy 创建人 + * @param fileSize 文件大小 + * @param fileType 文件类型 * @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); } diff --git a/src/main/java/com/rymcu/forest/service/ForestFileService.java b/src/main/java/com/rymcu/forest/service/ForestFileService.java index 5459d86..ba48900 100644 --- a/src/main/java/com/rymcu/forest/service/ForestFileService.java +++ b/src/main/java/com/rymcu/forest/service/ForestFileService.java @@ -17,9 +17,10 @@ public interface ForestFileService extends Service { * * @param md5Value md5值 * @param createdBy 用户id + * @param fileType 文件类型 * @return */ - String getFileUrlByMd5(String md5Value, long createdBy); + String getFileUrlByMd5(String md5Value, long createdBy, String fileType); /** * 插入文件对象 @@ -28,7 +29,9 @@ public interface ForestFileService extends Service { * @param filePath 上传路径 * @param md5Value md5值 * @param createdBy 创建人 + * @param fileSize 文件大小 + * @param fileType 文件类型 * @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); } diff --git a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java index 4f178f9..9354b19 100644 --- a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java @@ -24,11 +24,12 @@ public class ForestFileServiceImpl extends AbstractService implement * * @param md5Value md5值 * @param createdBy 用户id + * @param fileType 文件类型 * @return */ @Override - public String getFileUrlByMd5(String md5Value, long createdBy) { - return forestFileMapper.getFileUrlByMd5(md5Value, createdBy); + public String getFileUrlByMd5(String md5Value, long createdBy, String fileType) { + return forestFileMapper.getFileUrlByMd5(md5Value, createdBy, fileType); } /** @@ -38,10 +39,12 @@ public class ForestFileServiceImpl extends AbstractService implement * @param filePath 上传路径 * @param md5Value md5值 * @param createdBy 创建人 + * @param fileSize 文件大小 + * @param fileType 文件类型 * @return */ @Override - public int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy) { - return forestFileMapper.insertForestFile(fileUrl, filePath, md5Value, createdBy); + public int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy, long fileSize, String fileType) { + return forestFileMapper.insertForestFile(fileUrl, filePath, md5Value, createdBy, fileSize, fileType); } } diff --git a/src/main/java/com/rymcu/forest/util/FileUtils.java b/src/main/java/com/rymcu/forest/util/FileUtils.java index 61ee8ab..696c30b 100644 --- a/src/main/java/com/rymcu/forest/util/FileUtils.java +++ b/src/main/java/com/rymcu/forest/util/FileUtils.java @@ -18,7 +18,7 @@ public class FileUtils { private static final Logger logger = LoggerFactory.getLogger(FileUtils.class); /** - * 获取文件扩展名 + * 获取文件扩展名,带. * * @param filename * @return @@ -31,6 +31,7 @@ public class FileUtils { * 获取文件扩展名 * * @param filename + * @param defExt 默认文件后缀名, 带. * @return */ public static String getExtend(String filename, String defExt) { @@ -41,7 +42,7 @@ public class FileUtils { int i = filename.lastIndexOf('.'); if ((i > 0) && (i < (filename.length() - 1))) { - String result = filename.substring(i + 1).toLowerCase(); + String result = filename.substring(i).toLowerCase(); if (result.contains("?")) { return result.split("\\?")[0]; } diff --git a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java index 2495184..437ff25 100644 --- a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java +++ b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java @@ -42,8 +42,7 @@ public class UploadController { private final static String UPLOAD_URL = "/api/upload/file/batch"; 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 private ForestFileService forestFileService; @@ -120,8 +119,15 @@ public class UploadController { } TokenUser tokenUser = getTokenUser(request); Map data = new HashMap(2); + + if (multipartFile.getSize() == 0) { + data.put("message", "上传失败!"); + return GlobalResultGenerator.genSuccessResult(data); + } String md5 = DigestUtils.md5DigestAsHex(multipartFile.getInputStream()); - String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser()); + String orgName = multipartFile.getOriginalFilename(); + String fileType = FileUtils.getExtend(orgName); + String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType); if (StringUtils.isNotEmpty(fileUrl)) { data.put("url", fileUrl); return GlobalResultGenerator.genSuccessResult(data); @@ -137,15 +143,15 @@ public class UploadController { 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; fileUrl = localPath + fileName; File saveFile = new File(savePath); try { 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); } catch (IOException e) { data.put("message", "上传失败!"); @@ -174,21 +180,25 @@ public class UploadController { 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(); + + 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; File saveFile = new File(savePath); - try (InputStream in = multipartFiles[i].getInputStream(); - OutputStream out = Files.newOutputStream(saveFile.toPath())) { + try (InputStream in = multipartFiles[i].getInputStream(); OutputStream out = Files.newOutputStream(saveFile.toPath())) { String md5 = DigestUtils.md5DigestAsHex(in); - String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser()); + String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType); if (StringUtils.isNotEmpty(fileUrl)) { successMap.put(orgName, fileUrl); continue; } - fileUrl = localPath + fileName; 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); } catch (IOException e) { errFiles.add(orgName); @@ -246,53 +256,57 @@ 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", ""); - - //得到输入流 - InputStream inputStream = conn.getInputStream(); - - // 获取文件md5值 - String md5 = DigestUtils.md5DigestAsHex(inputStream); - String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser()); - 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); 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, tokenUser.getIdUser()); - data.put("originalURL", url); - data.put("url", fileUrl); - } catch (IOException e) { - data.put("message", "上传失败!"); - } - return GlobalResultGenerator.genSuccessResult(data); } diff --git a/src/main/java/mapper/ForestFileMapper.xml b/src/main/java/mapper/ForestFileMapper.xml index d9dcd7a..7047c34 100644 --- a/src/main/java/mapper/ForestFileMapper.xml +++ b/src/main/java/mapper/ForestFileMapper.xml @@ -3,6 +3,8 @@ + + @@ -12,8 +14,8 @@ - insert into forest_file (md5_value, file_path, file_url, created_time, created_by) - values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy}) + insert into forest_file (md5_value, file_path, file_url, created_time, created_by, file_size, file_type) + values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy}, #{fileSize}, #{fileType}) \ No newline at end of file