URLDecoder:使用spring上传时,不完整的尾随转义(%)模式

时间:2019-06-30 11:51:56

标签: java spring

我的研究对象中只有一个入门者:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

2.1.6-发布

我尝试上传文件并使用MultipartFile,但不起作用。
该框架的工作方式类似于urlencoded form

    @PostMapping(value = "/image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Mono<ResponseEntity<List<String>>> searchByImage(@RequestParam("file") MultipartFile file) throws Exception {
        return Mono.just(ResponseEntity.ok(service.guess(file.getBytes())));
    }

并抛出

java.lang.IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern
    at java.base/java.net.URLDecoder.decode(URLDecoder.java:225) ~[na:na]
    at java.base/java.net.URLDecoder.decode(URLDecoder.java:142) ~[na:na]
    at org.springframework.http.codec.FormHttpMessageReader.parseFormData(FormHttpMessageReader.java:146) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.http.codec.FormHttpMessageReader.lambda$readMono$0(FormHttpMessageReader.java:113) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107) ~[reactor-core-3.2.10.RELEASE.jar:3.2.10.RELEASE]
    at reactor.core.publisher.FluxContextStart$ContextStartSubscriber.onNext(FluxContextStart.java:103) ~[reactor-core-3.2.10.RELEASE.jar:3.2.10.RELEASE]

发生错误:

    private MultiValueMap<String, String> parseFormData(Charset charset, String body) {
        String[] pairs = StringUtils.tokenizeToStringArray(body, "&");
        MultiValueMap<String, String> result = new LinkedMultiValueMap<>(pairs.length);
        try {
            for (String pair : pairs) {
                int idx = pair.indexOf('=');
                if (idx == -1) {
                    result.add(URLDecoder.decode(pair, charset.name()), null);
                }
                else {
                    String name = URLDecoder.decode(pair.substring(0, idx),  charset.name());
                    String value = URLDecoder.decode(pair.substring(idx + 1), charset.name());
                    result.add(name, value);
                }
            }
        }

我转向使用FilePart,但得到相同的结果...
我错过了一些配置吗?

1 个答案:

答案 0 :(得分:1)

似乎我不小心在邮递员中设置了标题application/x-www-form-urlencoded
我删除了它,结果变成了:

{
    "timestamp": "2019-06-30T12:00:29.703+0000",
    "path": "/api/image",
    "status": 400,
    "error": "Bad Request",
    "message": "Required MultipartFile parameter 'file' is not present"
}

似乎无法解析。
我放完

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>  

进入依赖关系。
工作正常。