티스토리 뷰
01) 개요
- 마이페이지 개발중 이미지파일이 프로젝트 내부가 아닌 외부 경로 파일을 보여주려함. (업로드 했던 파일)
- ex) Spring에서 파일업로드를 구현할때 File 객체를 생성해서 최상위 경로를 C드라이브로 잡게되는데 로컬에 있는 파일은 브라우저 보안상 접근이 불가함.
- 다행히 Spring에선 외부 경로에 있는 리소스를 접근할 수 있는 방법을 제공함.
02) 외부경로 생성 및 이미지 추가
03) Java 설정 코드 추가
- WebConfiguration을 WebMvcConfigurer를 상속받아 작성
package com.kjc.workplus.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Value("${resource.path}")
private String resourcePath;
@Value("${upload.path}")
private String uploadPath;
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(uploadPath)
.addResourceLocations(resourcePath);
}
}
- addResourceHandlers() 메서드를 오버라이딩 하고 위와 같이 작성.
- application.properties에도 추가
- 해당 소스는 클라이언트의 요청 url이 /upload/로 시작될 경우 C:/resource/로 요청을 전달함.
04) 실행
'Spring boot > Spring' 카테고리의 다른 글
07. Spring(4) - RequestRejectedExcecption 핸들링 (0) | 2022.01.17 |
---|---|
06. HttpServletRequest 메서드 (0) | 2021.12.12 |
04. Spring - @RequestParam과 @PahtVariable (0) | 2021.06.01 |
03. @Controller와 @RestController의 차이 (0) | 2021.05.31 |
02. Dispatcher-Servlet 이란 (0) | 2021.05.30 |
댓글