IT, 프로그래밍/Spring
Spring 이미지 외부 폴더로 업로드 후에 이미지 접근하기(불러오기)
오리@
2018. 8. 7. 21:36
이미지 업로드 후에 로컬에서 불러오려고 할 때, 안되는 경우가 있다.
특히 외부에 폴더를 지정했을 경우, 일반적인 방법으로는 업로드 한 이미지 폴더에 접근할 수 있는 방법이 없기때문에 톰캣과 서블릿 설정 파일에 따로 설정을 해 주어야 한다.
1. Server.xml 파일에 업로드 폴더 경로를 매핑해준다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/> <!-- 이게 핵심임 --> <Context path="/img" reloadable="true" docBase="지정한 폴더 절대 경로"/> </Host> | cs |
2. 서블릿 설정 파일 (servlet-context)에 리소스 매핑을 추가한다.
1 2 3 4 5 | <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <resources mapping="/img/**" location="지정한 업로드 폴더 절대경로" /> | cs |
설정이 정상적으로 되었다면, 아래와 같이 접근하면 이미지가 나온다.
http://localhost:8080/img/파일명.jpg
지금 img로 매핑된 부분은 원하는대로 바꿔주면 된다.
물론 하위 폴더도 접근이 가능하다.