당신은 FileSystemResource
사용하지 말아야합니다, 당신은 ServletContextResource
을 사용해야합니다
new ServletContextResource(servletContext, "/myfile.xml");
는 ServletContext를 당신에게 사용할 수 있음을 물론, 가정.
이 실제로 인 경우 FileSystemResource
을 사용하려는 경우 컨테이너가 어디에 있는지 물어보고 해당 경로를 상대 경로로 사용해야합니다.
String filePath = servletContext.getRealPath("/myfile.xml");
new FileSystemResource(filePath);
스프링이 당신을 위해 일하게하는 것이 더 쉽습니다. 이 Resource
이 필요한 콩이 있다고 가정 해보십시오. 리소스 경로를 String으로 삽입하고 Spring이 리소스 경로를 리소스로 변환하도록 할 수 있습니다.
public class MyBean {
private Resource myResource;
public void setMyResource(Resource myResource) {
this.myResource = myResource;
}
}
와 콩 파일에서
:
<bean id="myBean" class="MyBean">
<property name="myResource" value="/path/under/webapp/root/of/my/file.xml">
</bean>
봄은 ServletContextResource
에 자원 경로를 변환하고 빈에 그 전달합니다.
감사합니다. 내 서블릿에는 HttpServletRequest 및 HttpServletResponse가 있으므로 servletContext.getRealPath() 대신 "request.getRealPath()"를 수행합니다. – portoalet
내 생명을 구했어 !! – rpr