티스토리 뷰

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) 실행

 

 

참조 : https://kim-jong-hyun.tistory.com/28

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함