2017-10-02 3 views
0

안녕하세요. 사용자가 파일을 업로드 한 다음 나중에 다운로드 할 수있게하는 응용 프로그램을 작성 중입니다. 내가 찾은 것은 파일 확장자가 restTemplate.getForEntity에서 제거되어 RequestMapping이 전체 파일 이름을 얻지 못한다는 것입니다. 아무도 이것이 옳고 이것을 극복하는 방법을 확인할 수 있습니까?RestTemplate.getForEntity는 파일 확장자를 제거합니까?

컨트롤러 :

@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()는 확장명이없는 파일 이름 만받을 수 있으므로 o 수동으로 '.png'를 추가하십시오. 따라서 구현에 문제가 있거나 다른 사람이이 파일 확장 문제를 해결하는 방법이 일반화되어야하는지 여부는 알 수 없습니다.

+0

보십시오. 기본적으로 URL을 확장자로 전달하는 경우 콘텐츠 확인자를 찾으려고 시도합니다. http://websystique.com/springmvc/spring-4-mvc-contentnegotiatingviewresolver-example/ – chetank

답변

0

최종 솔루션 : contentnegotiating 해결을 사용

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