【问题标题】:Spring Boot GET request returning 400 for imagesSpring Boot GET 请求为图像返回 400
【发布时间】:2017-06-14 18:22:24
【问题描述】:

我的上传图片请求如下:

@RequestMapping(method = RequestMethod.POST, value = BASE_PATH)
public ResponseEntity<?> createFile(@RequestParam("file") MultipartFile file, HttpServletRequest servletRequest){

    URI localUri = null;
    try{
        imageService.createImage(file);
        final URI localhostUri = new URI(servletRequest.getRequestURL().toString() + "/")
                                        .resolve(file.getOriginalFilename() + "/raw");

        localUri = localhostUri;

    }catch (IOException e){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                        .body("Could not upload " + file.getOriginalFilename() + "=>" + e.getMessage());
    }catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return ResponseEntity.created(localUri)
            .body("Successfully Uploaded " + file.getOriginalFilename());
}

以下是一次获取图像的GET方法:

@RequestMapping(method = RequestMethod.GET, value = BASE_PATH + "/" + FILENAME + "/raw")
@ResponseBody
public ResponseEntity<?> oneRawImage(@PathVariable String filename){

    try{
        Resource file = imageService.findOneImage(filename);
        return ResponseEntity.ok()
                .contentLength(file.contentLength())
                .contentType(MediaType.IMAGE_JPEG)
                .body(new InputStreamResource(file.getInputStream()));
    }catch (IOException e){
        return ResponseEntity.badRequest()
                            .body("Couldn't Find" + filename + "=>" + e.getMessage());
    }

}

当我进行 POST 时,我得到一个正确的响应,说明文件已上传(我不确定是否是,因为返回请求超出了 try 块。以下是我得到的响应:

*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> POST /images HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.49.0
> Accept: */*
> Content-Length: 69927
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------
b5f1ef58e19bbb8d
> 
< HTTP/1.1 100 
< HTTP/1.1 201 
< Location: http://localhost:8080/images/image2.jpg/raw
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 32
< Date: Wed, 14 Jun 2017 18:24:06 GMT
< 
* Connection #0 to host localhost left intact
Successfully Uploaded image2.jpg

但是当我尝试执行 GET 请求以获取我发布的图像时,我收到以下错误:

Couldn't Findimage2.jpg=>ServletContext resource [/fileupload-dir/image2.jpg] cannot be resolved to URL because it does not exist

我尝试了多种方式调试,但都没有成功。 springers 的任何帮助将不胜感激!谢谢

【问题讨论】:

  • /fileupload-dir/ 映射正确吗?我无法从提供的代码中看出 - 您的方法似乎映射到 ../raw
  • @ochi 这很有趣,在 findOneImage 服务方法中,我实际上在 resourceLoader.getResource("file:" + UPLOAD_ROOT + "/" + filename) 处漏掉了一个冒号。我不完全理解“:”的用法,但这就是问题所在。
  • 您的问题标题具有误导性...标题说您收到 404(找不到资源)但实际上您收到的是 400(错误请求)-现在我们知道为什么...跨度>
  • 感谢您指出这一点。我正在更改它@ochi

标签: spring rest spring-mvc spring-boot


【解决方案1】:

所以我意识到这个问题很琐碎。被调用的服务方法:

public Resource findOneImage(String filename){
    return resourceLoader.getResource("file:" + UPLOAD_ROOT + "/" + filename);
}

“文件”中缺少冒号。因此,resourceLoader 没有被解析为正确的 url。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 2020-10-26
    • 2021-07-14
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多