首页 > 解决方案 > 如何访问@RequestParam 以在 Spring Rest 控制器中获取文件名

问题描述

我想以另一种方法使用此端点上传文件名

 @PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile 
    file) {
    String fileName = fileStorageService.storeFile(file);

    String fileDownloadUri = 
       ServletUriComponentsBuilder.fromCurrentContextPath()
            .path("/downloadFile/")
            .path(fileName)
            .toUriString();

    return new UploadFileResponse(UUID.randomUUID(), fileName, fileDownloadUri, file.getContentType(), file.getSize());
}

另一种方法是将文件名与一些数据一起发送到 mongodb。

    @PostMapping("/uploadPath")
 public String setPath(@RequestBody Image image ) {
     image.setImageId(UUID.randomUUID());
     String fileName = m.
     image.setFileName(fileName);
     String filePath = Paths.get(fileStorageProperties.getUploadDir()).toAbsolutePath().normalize()+File.separator+fileName; 
     image.setImagePath(filePath);
      imgserv.insertPath(image);

 }

是否可以从端点(/uploadFile)访问文件名并使用端点(/uploadPath)存储在数据库中

标签: spring

解决方案


推荐阅读