I am the jar package required by the Pom.xml in the MAVEN project (where spring uploads the two main Jar:commons-fileupload.jar and Commons-io.jar of the file) in a SPRINGMVC environment that has already been built:
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.zhihua</groupId> <artifactid>springmvc_fileupload& lt;/artifactid> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <na Me>springmvc_fileupload Maven webapp</name> <url>http://maven.apache.org</url> <properties > <!--Spring version number--<spring.version>3.2.8.RELEASE</spring.version> <!--J
Unit version number-<junit.version>4.10</junit.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</artifact Id> <version>3.8.1</version> <scope>test</scope> </dependency> <!--springmvc Upload file dependencies -<dependency> <groupId>commons-fileupload</groupId> <artifactid
>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
<!--add Spring dependency--<dependency> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <version>${spring.version}</version>
</dependency> <dependency> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <version>${spring.version}</version>
</dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>sPring-context</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <ARTIFACTID>SP Ring-context-support</artifactid> <version>${spring.version}</version> </dependen cy> <dependency> <groupId>org.springframework</groupId> <artifacti D>spring-aop</artifactid> <version>${spring.version}</version> </dependency>
; <dependency> <groupId>org.springframework</groupId> <artifactid>spring-asp Ects</artifactid> <version>${spring.version}</version> </dependency> & Lt;dependency> <groupId>org.springframework</groupId> <ARTIFACTID>SPRING-TX&L t;/artifactid&Gt <version>${spring.version}</version> </dependency> <dependency> <gr Oupid>org.springframework</groupid> <artifactId>spring-jdbc</artifactId> < version>${spring.version}</version> </dependency> <dependency> <groupi D>org.springframework</groupid> <artifactId>spring-web</artifactId> <versi On>${spring.version}</version> </dependency> <!--spring Unit test-dependent-<depende Ncy> <groupId>org.springframework</groupId> <artifactid>spring-test</artif
Actid> <version>${spring.version}</version> <scope>test</scope> </dependency> <!--jstl Tag Support--<dependency> <groupid>jstl</groupi
D> <artifactId>jstl</artifactId> <version>1.2</version> </dependency
> <!--servlet Core Pack---<dependency> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!--JSP--<dependency>
; <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> < version>2.1</version> <scope>provided</scope> </dependency> </depend encies> <build> <finalName>SpringMVC_FileUpload</finalName> </build> </proje
Ct>
To add code in Spring-mvc.xml:
<!--Springmvc Upload a file, you need to configure the Multipartresolver processor--
<bean id= "multipartresolver"
class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver ">
<property name=" Defaultencoding " Value= "Utf-8" ></property>
<property name= "maxuploadsize" value= "10485760000" ></property >
<property name= "maxinmemorysize" value= "40960" ></property>
</bean>
Front page (fileupload.jsp) and upload successful return page (success.jsp):
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Controller's Code:
Package Com.zhihua.controller;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.util.Date;
Import Java.util.Iterator;
Import Javax.servlet.http.HttpServletRequest;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.multipart.MultipartFile;
Import Org.springframework.web.multipart.MultipartHttpServletRequest;
Import Org.springframework.web.multipart.commons.CommonsMultipartFile;
Import Org.springframework.web.multipart.commons.CommonsMultipartResolver; @Controller @RequestMapping ("/fileupload") public class Fileuploadcontroller {/** * upload files by stream * @RequestP Aram ("file") encapsulates the files obtained by the Name=file control into commonsmultipartfile objects * < Please replace the function description > <br> * < Please replace the detailed description > * @param file * @return * @author Caizh * @since [1.0.0] * @version [1.0.0,2017 February 5] */@RequestMapping ("/upload_01") public String fileupload_1 (@RequestParam ("file_01") Commonsmultipartfile file) throws IOException {//used to detect program run time Lo
ng startTime = System.currenttimemillis ();
System.out.println ("FileName:" +file.getoriginalfilename ()); try{//Get output stream outputstream OS = new FileOutputStream ("e:/" +new Date (). GetTime () +file.getoriginalfil
Ename ());
Gets the stream of the input stream commonsmultipartfile can directly get the file InputStream is = File.getinputstream ();
int temp;
Reads a byte and writes the while ((Temp=is.read ())! = ( -1)) {os.write (temp);
} os.flush ();
Os.close ();
Is.close ();
}catch (Exception e) {e.printstacktrace ();
} Long EndTime = System.currenttimemillis (); System.out.println ("Method one Run Time:" +string.valueof (Endtime-starttIME) + "MS");
return "View/success"; }/** * uses file. Transto to save the uploaded file * < replace the function description > <br> * < Please replace the detailed description > * @param file * @return * @thro WS IOException * @author Caizh * @since [1.0.0] * @version [1.0.0,2017 year February 5] */@RequestMapping ("/
Upload_02 ") Public String fileupload_2 (@RequestParam (" file_02 ") Commonsmultipartfile file) throws IOException {
Long startTime = System.currenttimemillis ();
System.out.println ("FileName:" +file.getoriginalfilename ());
String Path = "e:/" +new Date (). GetTime () +file.getoriginalfilename ();
File NewFile = new file (path);
Write the file File.transferto (newFile) directly by Commonsmultipartfile method;
Long endTime = System.currenttimemillis ();
System.out.println ("Method two run Time:" +string.valueof (endtime-starttime) + "MS");
return "View/success"; }/** * Upload files using Multipartfile * < Replace function description > <br> * ≪ Please replace the detailed description > * @param file * @return * @author Caizh * @since [1.0.0] * @version [1.0.0,2017 year February 5th] */@RequestMapping ("/upload_03") public String fileupload_3 (@RequestParam (value= "file_03", Required=false) Multipartfile file, HttpServletRequest request) {String path = Request.getsession (). Getservletcontext (
). Getrealpath ("Upload");
SYSTEM.OUT.PRINTLN (path);
String fileName = File.getoriginalfilename ();
File TargetFile = new file (path,filename);