package com.nq.service.impl; import cn.hutool.core.util.StrUtil; import com.google.common.collect.Lists; import com.nq.common.ServerResponse; import com.nq.service.IFileUploadService; import com.nq.utils.FTPUtil; import java.io.File; import java.io.IOException; import java.time.LocalDate; import java.util.HashMap; import java.util.Map; import java.util.UUID; import com.nq.utils.PropertiesUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @Service("iFileUploadService") public class FileUploadServiceImpl implements IFileUploadService { private static final Logger log = LoggerFactory.getLogger(FileUploadServiceImpl.class); @Override public ServerResponse uploadFile(MultipartFile file) { // 兼容c端组件上传原理 String fileType = file.getOriginalFilename(); if (StrUtil.isEmpty(fileType) || fileType.contains("blob")) { fileType = "blob.png"; } String fileName = file.getOriginalFilename(); String fileExtentionName = fileName.substring(fileName.lastIndexOf(".") + 1); // 生成唯一的文件名 String id = UUID.randomUUID().toString(); String path = LocalDate.now() + "/" + id + "." + fileExtentionName; // 确保目标文件夹存在 File targetDir = new File(PropertiesUtil.getProperty("loca.images.dir") + "/" + LocalDate.now()); if (!targetDir.exists()) { targetDir.mkdirs(); } // 构建本地文件路径 File localFile = new File(targetDir, id + "." + fileExtentionName); // 打印上传路径 log.info("LocalFileUploadService uploadFile localFilePath: {}", localFile.getAbsolutePath()); try { // 将文件保存到本地 file.transferTo(localFile); } catch (IOException e) { log.error("LocalFileUploadService uploadFile IOException", e.getMessage(), e); } // 返回相对路径 return ServerResponse.createBySuccess(path); } // public ServerResponse uploadFile(MultipartFile file) { // // 兼容c端组件上传原理 // // String fileName = file.getOriginalFilename(); // // // String fileExtentionName = fileName.substring(fileName.lastIndexOf(".") + 1); // // // String uploadFileName = UUID.randomUUID() + "." + fileExtentionName; // // String path = LocalDate.now() + "/" + uploadFileName; // // // 确保目标文件夹存在 // File targetDir = new File(PropertiesUtil.getProperty("ftp.server.http.prefix") + "/" + LocalDate.now()); // if (!targetDir.exists()) { // targetDir.mkdirs(); // } // // // 构建本地文件路径 // File localFile = new File(targetDir, uploadFileName); // // // 打印上传路径 // log.info("LocalFileUploadService uploadFile localFilePath: {}", localFile.getAbsolutePath()); // // try { // // 将文件保存到本地 // file.transferTo(localFile); // // // 如果需要自定义元数据,可以在此处理 // Map metadata = new HashMap<>(); // metadata.put("x-amz-meta-myVal", "test"); // 如果需要元数据,可以放在文件名或其他地方 // // // // } catch (IOException e) { // log.error("LocalFileUploadService uploadFile IOException", e.getMessage(), e); // log.error("上传文件异常 , 错误信息 = {}", e); // } // // 返回相对路径 // return ServerResponse.createBySuccess(path); // } public ServerResponse upload(MultipartFile file, String path) { String fileName = file.getOriginalFilename(); String fileExtentionName = fileName.substring(fileName.lastIndexOf(".") + 1); String uploadFileName = UUID.randomUUID() + "." + fileExtentionName; File fileDir = new File(path); if (!fileDir.exists()) { fileDir.setWritable(true); fileDir.mkdirs(); } File tartgetFile = new File(path, uploadFileName); boolean result = false; try { file.transferTo(tartgetFile); result = FTPUtil.uploadFile(Lists.newArrayList(new File[]{tartgetFile})); tartgetFile.delete(); } catch (Exception e) { log.error("上传文件异常 , 错误信息 = {}", e); return null; } if (result) { return ServerResponse.createBySuccess(tartgetFile.getName()); } return ServerResponse.createByErrorMsg("上传失败"); } }