liujie
2026-02-05 80cff51173ecb83a7c7f74c7d977d35687b69c99
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/ProjectController.java
@@ -16,16 +16,28 @@
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.*;
import io.swagger.annotations.*;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.parsing.Problem;
import org.springframework.core.io.InputStreamResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
@@ -440,4 +452,51 @@
        return R.ok(list);
    }
    @Resource
    UserFileService userFileService;
    @SneakyThrows
    @ApiOperation(value = "下载文件(名称)")
    @GetMapping("/download/{name}")
    public ResponseEntity<org.springframework.core.io.Resource> downloadFileByName(@PathVariable String name) {
        var userFile = userFileService.getOne(new LambdaQueryWrapper<UserFile>().eq(UserFile::getName, name));
        return downloadFile(userFile);
    }
    private ResponseEntity<org.springframework.core.io.Resource> downloadFile(UserFile userFile) throws IOException {
        InputStreamResource resource =userFileService.resolveResource(userFile);
        var fileSize = userFile.getSize();
//        if(resource == null) {
//            resource = new InputStreamResource(getClass().getClassLoader().getResourceAsStream("images/wukong.png"));
//            fileSize = 6215360L;// resource.contentLength();
//        }
        String encodedFileName = URLEncoder.encode(userFile.getOriginFileName(), "UTF-8");
        String headerValue = String.format("attachment; filename=\"%s\"", encodedFileName);
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(userFile.getContentType()))
                .contentLength(fileSize)
                .header(HttpHeaders.CONTENT_DISPOSITION, headerValue)
                .body(resource);
    }
    @Value("${file.upload.dir}/files")
    private String UPLOAD_DIR;
//    @PostConstruct
//    void init(){
//        fileLocation  = Path.of(UPLOAD_DIR).toAbsolutePath().normalize();
//        try {
//            Files.createDirectories(this.fileLocation);
//        } catch (Exception ex) {
//            throw new RuntimeException("Could not create the directory where the uploaded files will be stored.", ex);
//        }
//    }
}