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

标签 spring spring-mvc

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

这奇怪的行为是什么?资源是否被构建到最终的 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>

最佳答案

这是一个专门用于此目的的现成图像上传/下载 Controller :

  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. 现在我们需要一个 Controller ,它可以将图像上传到服务器并稍后在 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 中文件夹:

Upload image to server folder

  • 让我们检查一下图像显示的功能。为此,我们只需要包含 <img>标记到我们需要显示图像的位置(这就是 Spring MVC 的工作原理):

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

  • 附注所以你看,这很简单。

    关于spring - 如何在 Spring MVC 应用程序中显示上传的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20907133/

    相关文章:

    java - Spring批处理元数据表的问题

    java - 为什么不使用click参数(Spring boot和vaadin)

    java - Spring mvc Controller bean 配置

    java - Spring MVC UTF-8 编码

    java - Spring 测试和 NullPointerException

    java - 涉及属性和元素的自定义 JSP TLD

    java - 使用spring cloud更改eureka服务器的默认端口

    java - Spring Boot - 合并 map 属性的 map

    java - 如何将 "include"Spring Security配置到应用程序中

    spring - 无法 Autowiring 包中的属性