简体   繁体   English

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

[英]RestTemplate.getForEntity will remove file extension?

Hi I am writing an application which enables user to upload a file then later download it. 嗨,我正在编写一个应用程序,使用户可以上传文件,然后再下载它。 What I found is that the file extension will be removed from restTemplate.getForEntity thus RequestMapping will not get full file name. 我发现文件扩展名将从restTemplate.getForEntity中删除,因此RequestMapping将不会获得完整的文件名。 Can anybody confirm this is correct and how to overcome this? 谁能确认这是正确的,以及如何克服呢?

Controller: 控制器:

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

Unit Testing: 单元测试:

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

Logging show: 记录显示:

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]}>

As you can see, imageUpload is a file name with '.png' extension and is correctly passed to restTemplate.getForEntity() but getUploaded() can only receive filename without extension thus I have to manually add '.png' which is ugly. 如您所见,imageUpload是扩展名为'.png'的文件名,可以正确地传递给restTemplate.getForEntity(),但getUploaded()只能接收不带扩展名的文件名,因此我必须手动添加'.png',这很难看。 So I don't know if there is anything wrong with my implementation or how anybody else solve this file extension problem which should be general. 因此,我不知道我的实现是否有问题,或者其他人如何解决此文件扩展名问题,这应该是普遍的。

Final solution: 最终解决方案:

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

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

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