Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Boot GET请求返回400个映像_Spring_Rest_Spring Mvc_Spring Boot - Fatal编程技术网

Spring Boot GET请求返回400个映像

Spring Boot GET请求返回400个映像,spring,rest,spring-mvc,spring-boot,Spring,Rest,Spring Mvc,Spring Boot,我上传图片的帖子请求如下: @RequestMapping(method = RequestMethod.POST, value = BASE_PATH) public ResponseEntity<?> createFile(@RequestParam("file") MultipartFile file, HttpServletRequest servletRequest){ URI localUri = null; try{ imageServ

我上传图片的帖子请求如下:

@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());
}

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

所以我意识到这个问题相当卑微。正在调用的服务方法:

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

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

是否正确映射了
/fileupload dir/
?从提供的代码中我看不出来-您的方法似乎映射到了
。/raw
@ochi这很有趣,在findOneImage服务方法中,我实际上在resourceLoader.getResource(“文件:“+UPLOAD\u ROOT+”/“+filename”)中漏掉了一个冒号。我不太明白“:”的用法,但这就是问题所在。你的问题标题有误导性。。。标题说你得到了一个404(未找到资源),但实际上你得到了一个400(错误的请求)-现在我们知道为什么了…谢谢你指出这一点。。我正在改变它@ochi
*   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
Couldn't Findimage2.jpg=>ServletContext resource [/fileupload-dir/image2.jpg] cannot be resolved to URL because it does not exist
public Resource findOneImage(String filename){
    return resourceLoader.getResource("file:" + UPLOAD_ROOT + "/" + filename);
}