SpringMVC上传文件FileUpload使用方法详解
程序员文章站
2024-04-01 20:00:28
本文实例为大家分享了springmvc上传文件fileupload的具体代码,供大家参考,具体内容如下
我是在已经搭建好的springmvc环境下,maven工程中的...
本文实例为大家分享了springmvc上传文件fileupload的具体代码,供大家参考,具体内容如下
我是在已经搭建好的springmvc环境下,maven工程中的pom.xml所需要的jar包(其中spring上传文件的两个主要jar:commons-fileupload.jar和commons-io.jar):
<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</artifactid> <packaging>war</packaging> <version>0.0.1-snapshot</version> <name>springmvc_fileupload maven webapp</name> <url>http://maven.apache.org</url> <properties> <!-- spring版本号 --> <spring.version>3.2.8.release</spring.version> <!-- junit版本号 --> <junit.version>4.10</junit.version> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- springmvc上传文件依赖 --> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>1.3.1</version> </dependency> <!-- 添加spring依赖 --> <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>spring-context-support</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aop</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aspects</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <!--spring单元测试依赖 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- jstl标签支持 --> <dependency> <groupid>jstl</groupid> <artifactid>jstl</artifactid> <version>1.2</version> </dependency> <!-- servlet核心包 --> <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> </dependencies> <build> <finalname>springmvc_fileupload</finalname> </build> </project>
在spring-mvc.xml中添加代码:
<!-- springmvc上传文件时,需要配置multipartresolver处理器 --> <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>
前端页面(fileupload.jsp)和上传成功返回页面(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"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>springmvc-文件上传</title> </head> <body> <h1>采用流的方式上传文件</h1> <form name="form1" action="<c:url value='/fileupload/upload_01'/>" method="post" enctype="multipart/form-data"> <input type="file" name="file_01"/> <input type="submit" value="fileupload"/> </form> <br> <h1>采用multipart提供的file.transfer方法上传文件1</h1> <form name="form2" action="<c:url value='/fileupload/upload_02'/>" method="post" enctype="multipart/form-data"> <input type="file" name="file_02"/> <input type="submit" value="fileupload"/> </form> <br> <h1>采用multipart提供的file.transfer方法上传文件2</h1> <form name="form2" action="<c:url value='/fileupload/upload_03'/>" method="post" enctype="multipart/form-data"> <input type="file" name="file_03"/> <input type="submit" value="fileupload"/> </form> <br> <h1>使用spring mvc提供的方法上传文件</h1> <form name="form2" action="<c:url value='/fileupload/upload_spring'/>" method="post" enctype="multipart/form-data"> <input type="file" name="file_03"/> <input type="submit" value="fileupload"/> </form> </body> </html>
<%@ 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"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <h2>文件上传成功!</h2> </body> </html>
controller 的代码:
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 { /** * 通过流的方式上传文件 * @requestparam("file") 将name=file控件得到的文件封装成commonsmultipartfile 对象 * <请替换成功能描述> <br> * <请替换成详细描述> * @param file * @return * @author caizh * @since [1.0.0] * @version [1.0.0,2017年2月5日] */ @requestmapping("/upload_01") public string fileupload_1(@requestparam("file_01")commonsmultipartfile file)throws ioexception { //用来检测程序运行时间 long starttime = system.currenttimemillis(); system.out.println("filename:"+file.getoriginalfilename()); try{ //获取输出流 outputstream os = new fileoutputstream("e:/"+new date().gettime()+file.getoriginalfilename()); //获取输入流commonsmultipartfile中可以直接得到文件的流 inputstream is = file.getinputstream(); int temp; //一个一个字节的读取并写入 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("方法一的运行时间:"+string.valueof(endtime-starttime)+"ms"); return "view/success"; } /** * 采用file.transto 来保存上传的文件 * <请替换成功能描述> <br> * <请替换成详细描述> * @param file * @return * @throws ioexception * @author caizh * @since [1.0.0] * @version [1.0.0,2017年2月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); //通过commonsmultipartfile 的方法直接写文件 file.transferto(newfile); long endtime = system.currenttimemillis(); system.out.println("方法二的运行时间:"+string.valueof(endtime-starttime)+"ms"); return "view/success"; } /** * 使用multipartfile上传文件 * <请替换成功能描述> <br> * <请替换成详细描述> * @param file * @return * @author caizh * @since [1.0.0] * @version [1.0.0,2017年2月5日] */ @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); if(!targetfile.exists()){ targetfile.mkdirs(); } //保存 try { file.transferto(targetfile); }catch(exception e){ e.printstacktrace(); } return "view/success"; } /** * 采用spring提供的上传文件的方法 * <请替换成功能描述> <br> * <请替换成详细描述> * @param request * @return * @throws illegalstateexception * @throws ioexception * @author caizh * @since [1.0.0] * @version [1.0.0,2017年2月5日] */ @requestmapping("/upload_spring") public string springupload(httpservletrequest request)throws illegalstateexception, ioexception{ long starttime = system.currenttimemillis(); //将当前上下文初始化给 commonsmutipartresolver (多部分解析器) commonsmultipartresolver multipartresolver = new commonsmultipartresolver(request.getsession().getservletcontext()); //检查form中是否有enctype="multipart/form-data" if(multipartresolver.ismultipart(request)) { //将request变成多request multiparthttpservletrequest multirequest = (multiparthttpservletrequest)request; //获取multirequest中所有的文件名 iterator iter = multirequest.getfilenames(); while(iter.hasnext()){ //一次遍历所有的文件 multipartfile file = multirequest.getfile(iter.next().tostring()); if(file!=null){ string path = "e:/springupload"+file.getoriginalfilename(); //上传 file.transferto(new file(path)); } } } long endtime = system.currenttimemillis(); system.out.println("方法三的运行时间:"+string.valueof(endtime-starttime)+"ms"); return "view/success"; } }
好了,全部代码都给出了,博主是测试成功了,感兴趣的朋友可以去比较不同上传方式的效率!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: java线程间通信的通俗解释及代码示例
下一篇: 基于php实现的验证码小程序