spring - 如何在 spring mvc 中获取资源文件夹的正确路径?

标签 spring spring-mvc

好吧,我正在使用 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/

相关文章:

java - 使用 Spring boot 解析具有无效键值的 JSON 对象

java - 我们如何使用Spring Boot将jsp文件中的表单数据保存到数据库中?

java - PrePersist、自定义生成器或服务层中的 Spring JPA/Hibernate 复合 key 生成?

java - Spring MVC @ModelAttribute 方法返回 "Bad request"400

spring-mvc - Spring MVC打开PDF作为 View

java - 将 Elasticsearch 与 Spring MVC 集成

spring - 如何修复 "Invalid remember-me token (Series/token) mismatch"错误?

java - 如何在 Spring 中手动启动共享 EntityManager 上的事务?

java - 没有数据源的 Spring Boot + Spring Batch

spring - @Autowired HttpServletRequest 与作为参数传递 - 最佳实践