简体   繁体   English

获取上载文件的MultipartFile数量

[英]Get MultipartFile amount of uploaded files

I have a method that processes the POST request for files using MultipartFile. 我有一个使用MultipartFile处理文件的POST请求的方法。

@PostMapping(value = "/payment-files", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 
public PaymentFileStatus postPaymentFile(
         @RequestParam("paymentFile") MultipartFile file,
) {
    byte[] fileBytes;
    try {
        fileBytes = file.getBytes();
    } catch (IOException e) {
        throw new PublicException(MESSAGE_NOT_READABLE, e);
    }

    return foo(fileBytes);
}

My task is to reject the request if more than one file is uploaded. 我的任务是,如果上传了多个文件,则拒绝该请求。 I was wondering if I somehow can get the number of uploaded files from the MultipartFile object? 我想知道是否能以某种方式从MultipartFile对象中获取上传文件的数量? Or do you have any other ideas how to check how many files were uploaded? 还是您还有其他想法如何检查上传了多少文件? Or maybe I should use @PreAuthorize ? 也许我应该使用@PreAuthorize Or maybe the validation should be implemented in other place? 还是应该在其他地方进行验证? Thanks! 谢谢!

@RequestParam("paymentFile") MultipartFile file,

更改为@RequestParam("paymentFile") List<MultipartFile> file,此后,您可以获得上载的数字文件。

Try this. 尝试这个。

Pass multiple MultipartFile files in array. 在数组中传递多个MultipartFile文件。

@PostMapping(value = "/payment-files", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 
public PaymentFileStatus postPaymentFile(
         @RequestParam("paymentFile") MultipartFile[] file
) {
    byte[] fileBytes;
    try {
        fileBytes = file.getBytes();
    } catch (IOException e) {
        throw new PublicException(MESSAGE_NOT_READABLE, e);
    }

    return foo(fileBytes);
}

Refer :- Solution 参考:- 解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM