我有Spring MVC应用程序,我想将用户图像上载到resources/uploads文件夹中。显然,我想在我的网站上为他们提供服务。但是,当我尝试将sample.png放入resources文件夹中(仅出于测试目的)时,网络服务器回答了"not found"。我重新构建了项目,图片成为了可用的对象。我删除了图片,仍然可以访问。我重新构建了项目,服务器回答了应有的内容(“未找到”)。

这种奇怪的行为是什么?资源是否内置在最终的jar文件中?这是否意味着在重建项目之前将无法访问所有上传的用户图片?
如果是这样,我完全不应该将上传的文件放置到资源文件夹中,那么我应该将它们放置在哪里?
为什么会发生这种情况,我应该如何提供这些图片?

非常感谢你。

Context.xml:

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven>
        <message-converters>
          <beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
              <beans:property name="objectMapper" ref="customObjectMapper"/>
          </beans:bean>
      </message-converters>
    </annotation-driven>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />


    <!-- mustache.java -->
    <beans:bean id="viewResolver" class="org.springframework.web.servlet.view.mustache.MustacheViewResolver">
        <beans:property name="cache" value="false" />
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".mustache" />
        <beans:property name="templateLoader">
            <beans:bean class="org.springframework.web.servlet.view.mustache.MustacheTemplateLoader" />
        </beans:property>
    </beans:bean>


    <!-- Standard template engine -->
    <!-- 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.me.myproject" />


    <!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with
       username root and blank password. Change below if it's not the case -->
      <beans:bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <beans:property name="url" value="jdbc:mysql://localhost:3306/myproject"/>
    <beans:property name="username" value="someone"/>
    <beans:property name="password" value="something"/>
    <beans:property name="validationQuery" value="SELECT 1"/>
  </beans:bean>


  <!-- FlyWay -->
  <beans:bean id="flyway" class="com.googlecode.flyway.core.Flyway" init-method="migrate">
    <beans:property name="dataSource" ref="myDataSource"/>
  </beans:bean>

  <!-- Hibernate Session Factory -->
  <beans:bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" depends-on="flyway">
    <beans:property name="dataSource" ref="myDataSource"/>
    <beans:property name="packagesToScan">
      <beans:array>
        <beans:value>com.me.myproject</beans:value>
      </beans:array>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:value>
           hibernate.dialect=org.hibernate.dialect.MySQLDialect
          hibernate.hbm2ddl.auto=validate
        </beans:value>
      </beans:property>
    </beans:bean>

  <!-- Hibernate Transaction Manager -->
  <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="mySessionFactory"/>
  </beans:bean>

  <!-- Activates annotation based transaction management -->
  <tx:annotation-driven transaction-manager="transactionManager"/>


</beans:beans>

Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">





    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

最佳答案

您必须将文件保存到外部文件夹中。

例如,我创建了一个类似于/home/webappFolder/的目录,并在其中创建了其他子目录。例如,我在其中存储上载数据的存储库目录..在其中放置碧 Jade 报告文件的报告子目录..等等

/home/webappFolder/repo
/home/webappFolder/report
/home/webappFolder/logs

正如您在问题中张贴的xml中所看到的:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->

在资源中,您转换了静态资源。

10-04 21:52