Spring MVC file upload

I. step summary

  1. Import jar package
  2. Configure web.xml
  3. Create configuration file mvc.xml in src directory
  4. Create the front page fileupload.jsp
  5. Create controller class HelloController
  6. Configure mvc.xml file
  7. Create the folder JSP in the web inf directory, and create the file success.jsp

 

II. Detailed steps

1. Import jar package

commons-logging-1.1.1.jar

jackson-annotations-2.5.4.jar

jackson-core-2.5.4.jar

jackson-databind-2.5.4.jar

spring-aop-4.1.6.RELEASE.jar

spring-beans-4.1.6.RELEASE.jar

spring-context-4.1.6.RELEASE.jar

spring-core-4.1.6.RELEASE.jar

spring-expression-4.1.6.RELEASE.jar

spring-tx-4.1.6.RELEASE.jar

spring-web-4.1.6.RELEASE.jar

spring-webmvc-4.1.6.RELEASE.jar

//On this basis, import two more jar packages

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar

 

 

2. Configure web.xml

  
<filter>
      <filter-name>encoding</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
          <param-name>encoding</param-name>
          <param-value>utf-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>encoding</filter-name>
      <url-pattern>*.do</url-pattern>
  </filter-mapping>
  
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:mvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>

  </servlet>
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
web.xml key code

 

 

3. Create the configuration file mvc.xml in the src directory

 

4. Create the front page fileupload.jsp

    <form action="upload.do" method="post" enctype="multipart/form-data">
        <input type="file" name="file" value="Select file"/>
        <input type="submit" value="submit"/>
    </form>

 

  

5. Create the controller class HelloController. The red part is required

package com.ahd.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.multipart.commons.CommonsMultipartFile;

@Controller
public class HelloController4{
    @RequestMapping("/upload")
    public String upload(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest req,ModelMap mm) throws IOException{
        //Get upload path
        String path=req.getRealPath("upload");
        //Get file name
        String filename=file.getOriginalFilename();
        
        InputStream is = file.getInputStream();
        OutputStream os = new FileOutputStream(new File(path,filename));
        
        byte[]b=new byte[1024];
        int len=0;
        
        while((len=is.read(b))!=-1){
            os.write(b, 0, len);
            
        }
        mm.addAttribute("msg", "File uploaded successfully");
        return "success";
    }
}

 

  

6. Configure the mvc.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        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 ">
    <!-- To configure handerAdapter  Adapter -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
   <!-- File upload configuration -->
    <bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- Set encoding format -->  
        <property name="defaultEncoding" value="utf-8"></property> 
        <!-- set file size -->  
        <property name="maxUploadSize" value="10485760000"></property>
        <!-- Set buffer size -->  
        <property name="maxInMemorySize" value="40960"></property>  
    </bean> 
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <!-- Prefix of the view after rendering the view name -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- Suffix of the rendered view -->
        <property name="suffix" value=".jsp"/>
        <!-- Example: the view name is:hello   After rendering:/WEB-INF/jsp/hello.jsp This page-->
    </bean>

    
    <!-- spring The container scans all classes under the specified package. If there is an annotation on the class, the corresponding annotation will be generated bean Object already mapped information -->
    <context:component-scan base-package="com.ahd.controller"/>
    
</beans>

 

 

7. Create the folder JSP and the file success.jsp in the web inf directory

    success.jsp
    ${msg}

Tags: Java Spring JSP xml

Posted on Wed, 04 Dec 2019 00:39:38 -0500 by Shellfishman