如何在Spring MVC应用程序中显示上传的图像

时间:2014-01-03 15:32:19

标签: spring spring-mvc

我有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>

4 个答案:

答案 0 :(得分:10)

这是一个完全用于此目的的即用型图像上传/下载控制器:

  1. 首先,我们需要通过简单的表单(admin.jsp)上传图片:

     <form method="POST" action="uploadFile" enctype="multipart/form-data">
        File to upload: <input type="file" name="file" >
        <br />
        Name: <input type="text" name="name" >
        <br />
        <br />
        <input type="submit" value="Upload">
    </form>
    <c:if test="${not empty message}">
        ${message} <!-- here would be a message with a result of processing -->
    </c:if>
    
  2. 现在我们需要一个控制器,可以将图像上传到服务器并稍后在jsp页面显示:

    package com.pizza.controllers;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.servlet.ModelAndView;
    
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.file.Files;
    
    @Controller
    public class FileUploadController {
    
        private static final String PIZZA_IMAGES = "pizzaImages";
        private static final String TOMCAT_HOME_PROPERTY = "catalina.home";
        private static final String TOMCAT_HOME_PATH = System.getProperty(TOMCAT_HOME_PROPERTY);
        private static final String PIZZA_IMAGES_PATH = TOMCAT_HOME_PATH + File.separator + PIZZA_IMAGES;
    
        private static final File PIZZA_IMAGES_DIR = new File(PIZZA_IMAGES_PATH);
        private static final String PIZZA_IMAGES_DIR_ABSOLUTE_PATH = PIZZA_IMAGES_DIR.getAbsolutePath() + File.separator;
    
        private static final String FAILED_UPLOAD_MESSAGE = "You failed to upload [%s] because the file because %s";
        private static final String SUCCESS_UPLOAD_MESSAGE = "You successfully uploaded file = [%s]";
    
        @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
        public ModelAndView uploadFileHandler(@RequestParam("name") String name,
                                          @RequestParam("file") MultipartFile file) {
            ModelAndView modelAndView = new ModelAndView("admin");
    
            if (file.isEmpty()) {
                modelAndView.addObject("message", String.format(FAILED_UPLOAD_MESSAGE, name, "file is empty"));
            } else {
                createPizzaImagesDirIfNeeded();
                modelAndView.addObject("message", createImage(name, file));
            }
    
            return modelAndView;
        }
    
        private void createPizzaImagesDirIfNeeded() {
            if (!PIZZA_IMAGES_DIR.exists()) {
                PIZZA_IMAGES_DIR.mkdirs();
            }
        }
    
        private String createImage(String name, MultipartFile file) {
            try {
                File image = new File(PIZZA_IMAGES_DIR_ABSOLUTE_PATH + name);
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(image));
                stream.write(file.getBytes());
                stream.close();
    
                return String.format(SUCCESS_UPLOAD_MESSAGE, name);
            } catch (Exception e) {
                return String.format(FAILED_UPLOAD_MESSAGE, name, e.getMessage());
            }
        }
    
        @RequestMapping(value = "/image/{imageName}")
        @ResponseBody
        public byte[] getImage(@PathVariable(value = "imageName") String imageName) throws IOException {
            createPizzaImagesDirIfNeeded();
    
            File serverFile = new File(PIZZA_IMAGES_DIR_ABSOLUTE_PATH + imageName + ".jpg");
    
            return Files.readAllBytes(serverFile.toPath());
        }
    
    }
    
  3. 现在让我们测试一下我们的上传功能。选择图像并为其指定名称(包括扩展名)。稍后(点击Upload按钮后)此图片将显示在{Tomcat.dir}/pizzaImages文件夹中:

  4. Upload image to server folder

    1. 让我们检查图片显示的功能。为此,我们只需要在我们需要显示图像的平台中包含<img>标记(这就是Spring MVC的工作方式):

      <img src="/image/11" />

    2. P.S。所以你看,它很简单。

答案 1 :(得分:2)

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

例如,我创建了一个像/home/webappFolder/这样的目录,在其中我创建了一些其他子目录。例如,我存储上传数据的存储库目录..我将jasper报告文件放在其中的报告子目录..等等

/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 -->

在您提供静态资源的资源中。

答案 2 :(得分:2)

我经常遇到这样的问题,具体取决于我的项目基础。例如,如果这是Netbeans中的Maven项目,并且您的上传内容将转到目标目录,则下次执行清洁时会将其删除。建立。

如果您将它们粘贴在src / main / webapps / resrouces中,则在重新部署之前它们可能无法使用/删除,具体取决于IDE中的设置,因为应用程序用完了目标目录。

这一切都取决于你的设置,IDE,目录结构等。我认为它与Spring MVC没什么关系。

答案 3 :(得分:1)

结帐本指南:https://github.com/jdmr/fileUpload 它解释了如何将文件上传到&#34;外部&#34;文件夹,然后提供文件。看一下这里的控制器类: https://github.com/jdmr/fileUpload/blob/master/src/main/java/org/davidmendoza/fileUpload/web/ImageController.java