好吧,我正在使用 SpringMVC 开发一个应用程序,我必须在我的资源文件夹中存储一些图片,我需要有可以在我的计算机上运行的正确路径,并且在部署应用程序后,我尝试了很多东西,但没有t 似乎工作。
这是我的配置和我尝试过的。
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/images/**" location="/images/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.hpcdg.quality" />
</beans:beans>
Controller
if (!file.isEmpty()) {
try {
mod.addAttribute("objet", resourceLoader.getResource("/resources/images_Emp/"+this.GenerateName(8)+".png").getFile());
System.out.println(resourceLoader.getResource("/resources/images_Emp/"+this.GenerateName(8)+".png").getFile());
file.transferTo(resourceLoader.getResource("/resources/images_Emp/"+this.GenerateName(8)+".png").getFile());
} catch (Exception e) {
throw new RuntimeException("Product Image saving failed", e);
}
}
return "/QL/Ajouts";
}
当我显示路径时:我得到了工作区元数据的东西,我需要的是直接进入我的应用程序 webapp 中的资源文件夹的东西。
任何帮助将不胜感激。
最佳答案
使用 new ClassPathResource("images_Emp" + File.separator + this.GenerateName(8)+".png")
File.separator
使您的路径平台独立
关于spring - 如何在 spring mvc 中获取资源文件夹的正确路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28793808/