RestTemplate.getForEntity将删除文件扩展名?

时间:2017-10-02 02:09:55

标签: java spring-boot resttemplate

您好我正在编写一个应用程序,它允许用户上传文件然后下载它。我发现文件扩展名将从restTemplate.getForEntity中删除,因此RequestMapping将不会获得完整的文件名。谁能证实这是正确的以及如何克服这个问题?

控制器:

@RequestMapping(value = "/uploaded/{filename}", method = RequestMethod.GET)
public ResponseEntity<Resource> getUploaded(@PathVariable String filename) {
    logger.debug("getUploaded filename=" + filename);

    Resource file = storageService.loadAsResource(filename + ".png");

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

单元测试:

    System.err.println("imageUpload = " + imageUpload);
    ResponseEntity<Resource> responseResource = this.restTemplate.getForEntity("/uploaded/" + imageUpload, Resource.class);
    assertNotNull(responseResource);
    System.err.println("responseResource = " + responseResource.toString());
    assertEquals(HttpStatus.FOUND, responseResource.getStatusCode());
    assertEquals(resource, responseResource.getBody());

记录显示:

imageUpload = favicon1.png
getUploaded filename=favicon1
responseResource = <200 OK,Byte array resource [resource loaded from byte array],{Content-Type=[image/png], Content-Length=[8971], Date=[Mon, 02 Oct 2017 01:58:39 GMT]}>

正如您所看到的,imageUpload是一个扩展名为“.png”的文件名,并且正确传递给restTemplate.getForEntity()但是getUploaded()只能接收没有扩展名的文件名,因此我必须手动添加“.png”很难看所以我不知道我的实现是否有任何问题,或者其他人如何解决这个文件扩展问题应该是一般性的。

1 个答案:

答案 0 :(得分:0)

最终解决方案:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void configurePathMatch(PathMatchConfigurer matcher) {
        matcher.setUseRegisteredSuffixPatternMatch(true);
    }
}