当前位置: 首页 > 知识库问答 >
问题:

通过邮递员测试上传endpoint

谢翰学
2023-03-14

我试图上传一个文件到我的服务器使用一个endpoint通过Spring公开。然而,当我试图通过邮递员测试api时,我得到当前请求不是一个多部分请求错误。我通过这个问题多部分异常:当前请求不是多部分请求,但仍然无法修复此问题。请帮助。提前感谢。

这是我的控制器:

@RestController
@RequestMapping
public class UploadController {

    @Autowired
    StorageService storageService;

    List<String> files = new ArrayList<String>();

    @PostMapping("/post")
    public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
        String message = "";
        try {
            storageService.store(file);
            files.add(file.getOriginalFilename());

            message = "You successfully uploaded " + file.getOriginalFilename() + "!";
            return ResponseEntity.status(HttpStatus.OK).body(message);
        } catch (Exception e) {
            message = "FAIL to upload " + file.getOriginalFilename() + "!";
            return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(message);
        }
    }

    @GetMapping("/getallfiles")
    public ResponseEntity<List<String>> getListFiles(Model model) {
        List<String> fileNames = files
                .stream().map(fileName -> MvcUriComponentsBuilder
                        .fromMethodName(UploadController.class, "getFile", fileName).build().toString())
                .collect(Collectors.toList());

        return ResponseEntity.ok().body(fileNames);
    }

    @GetMapping("/files/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> getFile(@PathVariable String filename) {
        Resource file = storageService.loadFile(filename);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
                .body(file);
    }
}

我的服务:

@Service
public class StorageService {

    Logger log = LoggerFactory.getLogger(this.getClass().getName());
    private final Path rootLocation = Paths.get("upload-dir");

    public void store(MultipartFile file) {
        try {
            Files.copy(file.getInputStream(), this.rootLocation.resolve(file.getOriginalFilename()));
        } catch (Exception e) {
            throw new RuntimeException("FAIL!");
        }
    }

    public Resource loadFile(String filename) {
        try {
            Path file = rootLocation.resolve(filename);
            Resource resource = new UrlResource(file.toUri());
            if (resource.exists() || resource.isReadable()) {
                return resource;
            } else {
                throw new RuntimeException("FAIL!");
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException("FAIL!");
        }
    }

    public void deleteAll() {
        FileSystemUtils.deleteRecursively(rootLocation.toFile());
    }

    public void init() {
        try {
            Files.createDirectory(rootLocation);
        } catch (IOException e) {
            throw new RuntimeException("Could not initialize storage!");
        }
    }
}

正如您在下面看到的,我将文件作为表单数据发送,并且没有设置任何标题

共有3个答案

夏俊杰
2023-03-14

尝试添加在您的请求标头Content-Type: Multipart/form-data(据我在邮递员中看到的,它丢失了)

丌官高远
2023-03-14

您的控制器需要一个请求参数“文件”:

@RequestParam("file") MultipartFile file

你必须在邮递员中设置关键“文件”,其中的值是你的文件(最后一张截图)。

弘浩博
2023-03-14

请参见下图,并将键值添加为file

 类似资料:
  • 我的主要目标是使用Microsoft Graph API将。pptx/.docx/.pdf文件上传到Microsoft Sharepoint。我可以使用下面的“PUT”请求上传到简单的文本文件,其中的内容类型是多部分/表单数据 ---------------------------------404518839734975569926100内容-处理:表单-数据;name=“文件”;filenam

  • 我有一个web API,我正在使用Postman测试。当我点击“发送”按钮时,Postman将数据发送到web API,这样我就可以测试web API,但我想查看Postman发送到web API的数据。我如何查看这个?

  • Spring启动应用程序。我有一个用于上传多部分文件的Restendpoint。 Spring boot版本为2.0.6。我尝试了以下方法。 1) spring:tomcat:最大http表单post大小:500MB最大swallow大小:500MB 2) spring:servlet:多部分:最大文件大小:500MB最大请求大小:500MB已启用:true 3) spring:servlet:多

  • 我对AWS很陌生,我在那个区域创建了s3 bucket,并将access keys区域传递到我的.env文件中,并编辑了conig/filesystems.php和我编写的用于发布数据的控制器,我的数据是混合型的,它包含二进制和普通数据,所以我有点困惑在postman中必须使用哪种内容类型(content-type=,在头内)。如果我使用普通content-type=application/jso

  • 我正在尝试为Web设置Firebase云消息传递。我成功地对其进行了正确初始化并获得了令牌: manifest.json与gcm_sender_id 我可以看到我在控制台中得到令牌,所以我试图验证它,并通过邮递员发送我的第一个通知-这里是留档。 发布网址:https://fcm.googleapis.com/v1/projects/PROJECTID/messages:发送授权:无授权 标题 Bo

  • 2FE552B7-53B1-4E4A-AFFF-3AEF8FE9D05B-----WebKitFormBoundaryLH8FJWGYEEVTCJMA内容-配置:表单-数据;name=“sample test file.pdf”;filename=“sample test file.pdf”content-type:application/pdf