xiangpei
2025-05-14 47cd9ecc0eff38ffe6b3b794b2bf197e958f4403
src/main/java/com/mindskip/xzs/controller/common/UploadController.java
@@ -3,14 +3,22 @@
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.configuration.RuoYiConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.UUID;
/**
 * @author:xp
@@ -30,14 +38,14 @@
    @PostMapping("/upload")
    public RestResponse uploadFile(MultipartFile file) throws Exception
    {
        // 检查文件是否为空
        if (file.isEmpty()) {
            return RestResponse.fail(500, "上传的文件为空");
        }
//        // 检查文件是否为空
//        if (file.isEmpty()) {
//            return RestResponse.fail(500, "上传的文件为空");
//        }
        try {
            // 获取文件名
            String fileName = StringUtils.cleanPath(file.getOriginalFilename());
            String originalFileName = StringUtils.cleanPath(file.getOriginalFilename());
            String randomName = UUID.randomUUID().toString().replace("-", "") + originalFileName.substring(originalFileName.lastIndexOf("."));
            // 指定文件存储路径
            String uploadDir = ruoYiConfig.getUrl(); // 修改为您希望存储的目录
            // 如果目录不存在,则创建目录
@@ -46,11 +54,14 @@
                dir.mkdirs();
            }
            // 构建目标文件的路径
            String filePath = uploadDir + "/" + fileName;
            String filePath = uploadDir + "/" + randomName;
            // 将文件保存到目标位置
            file.transferTo(new File(filePath));
            // 返回成功响应
            return RestResponse.ok("文件上传成功");
            HashMap hashMap = new HashMap(2);
            hashMap.put("name", originalFileName);
            hashMap.put("url", randomName);
            return RestResponse.ok(hashMap);
        } catch (IOException e) {
            e.printStackTrace();
            // 返回失败响应
@@ -58,4 +69,32 @@
        }
    }
    /**
     * 下载文件(单个)
     */
    @GetMapping("/download")
    public void download(@RequestParam String url, @RequestParam String fileName, HttpServletResponse response) throws Exception
    {
        // 提取文件路径
        String filePath = ruoYiConfig.getUrl() + File.separator + url;
        File file = new File(filePath);
        // 检查文件是否存在
        if (!file.exists()) {
            throw new RuntimeException("文件不存在");
        }
        // 读取文件内容
        byte[] fileContent = Files.readAllBytes(file.toPath());
        // 设置响应头
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        // 将文件内容写入响应输出流
        response.getOutputStream().write(fileContent);
        response.getOutputStream().flush();
    }
}