------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

我将用自认为最简单的语言,描述Springmvc的文件上传,来将老夫毕生功力灌顶传授给你

首先文件上传,又简至深

前提有吗?jar包,form表单里的属性(method="post" enctype="multipart/form-data")

jar包的节点我给出来:

        <!--文件上传的jar包-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.</version>
</dependency> <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>

下面我开始我第一个案例,最简单的文件上传:

  1.jsp页面:fileupload.jsp

<%--
Created by IntelliJ IDEA.
User: Dawn
Date: //
Time: :
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<h1>文件上传</h1>
<form action="${pageContext.request.contextPath}/fileuploadfirst" method="post" enctype="multipart/form-data">
文件1 <input type="file" name="upload"/>
<input type="submit"/>
</form>
</body>
</html>

  success.jsp

<%--
Created by IntelliJ IDEA.
User: Dawn
Date: //
Time: :
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>SUCCESS</h1>
</body>
</html>

  2.在webapp下你jsp页面的那个包下创建一个文件夹,我的叫upload,里面随便扔个东西,编译后,如果即使这样target目录下还没有upload这个文件夹的话,就手动创建

  3.创建处理器和处理方法

package cn.dawn.day24fileupload;

import com.sun.org.glassfish.gmbal.ParameterNames;
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 javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException; /**
* Created by Dawn on 2018/4/2.
*/
@Controller
public class FileupLoad { /*最初始版本*/
@RequestMapping("/fileuploadfirst")
public String fileuploadfirst(MultipartFile upload, HttpSession session){
/*获取上传文件的简单名称例如 1.txt*/
String childrlPath = upload.getOriginalFilename();
/*获得一个真实路径*/
String parentPath = session.getServletContext().getRealPath("/day24/upload");
/*获取一个完整的文件对象*/
File file=new File(parentPath,childrlPath);
/*传输创建到本地*/
try {
upload.transferTo(file);
/*上传成功*/
return "success";
} catch (IOException e) {
e.printStackTrace();
} /*上传失败*/
return "fileupload";
}
}

  4.自己的xml配置文件:这儿其实我想删减点的,文件名中文的处理和文件大小限制放在后面讲也行,不过放在这儿,你们应该也能理解

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--包扫描器-->
<context:component-scan base-package="cn.dawn.day24fileupload"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/day24/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!--多部分文件解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--文件名的编码-->
<property name="defaultEncoding" value="UTF-8"></property>
<!--限制文件大小--><!--这个单位是byte,我这儿限制的是20mb最大容量-->
<property name="maxUploadSize" value=""></property>
</bean> <!--绑定注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven> </beans>

  5.web.xml中修改中央处理器的上下文配置参数为上面那个xml

  6.启动tomcat,访问fileupload.jsp页面

第二个案例:多文件上传

  1.jsp页面fileuploadmore.jsp:

<%--
Created by IntelliJ IDEA.
User: Dawn
Date: //
Time: :
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<h1>文件上传</h1>
<form action="${pageContext.request.contextPath}/fileuploadsecond" method="post" enctype="multipart/form-data">
文件1 <input type="file" name="upload"/>
文件2 <input type="file" name="upload"/>
文件3 <input type="file" name="upload"/>
<input type="submit"/>
</form>
</body>
</html>

  success.jsp

<%--
Created by IntelliJ IDEA.
User: Dawn
Date: //
Time: :
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>SUCCESS</h1>
</body>
</html>

  2.处理器处理方法

package cn.dawn.day24fileupload;

import com.sun.org.glassfish.gmbal.ParameterNames;
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 javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException; /**
* Created by Dawn on 2018/4/2.
*/
@Controller
public class FileupLoad { /*多文件版本*/
@RequestMapping("/fileuploadsecond")
public String fileuploadsecond(@RequestParam MultipartFile[] upload, HttpSession session){
for (MultipartFile item :upload) {
if(item.getSize()>) {
/*获取上传文件的简单名称例如 1.txt*/
String childrlPath = item.getOriginalFilename();
/*获得一个真实路径*/
String parentPath = session.getServletContext().getRealPath("/day24/upload");
/*获取一个完整的文件对象*/
File file = new File(parentPath, childrlPath);
/*传输创建到本地*/
try {
item.transferTo(file);
/*上传成功*/ } catch (IOException e) {
e.printStackTrace();
return "fileuploadmore";
}
}
} /*上传失败*/
return "success";
} /*最初始版本*/
@RequestMapping("/fileuploadfirst")
public String fileuploadfirst(MultipartFile upload, HttpSession session){
/*获取上传文件的简单名称例如 1.txt*/
String childrlPath = upload.getOriginalFilename();
/*获得一个真实路径*/
String parentPath = session.getServletContext().getRealPath("/day24/upload");
/*获取一个完整的文件对象*/
File file=new File(parentPath,childrlPath);
/*传输创建到本地*/
try {
upload.transferTo(file);
/*上传成功*/
return "success";
} catch (IOException e) {
e.printStackTrace();
} /*上传失败*/
return "fileupload";
}
}

  3.自己的xml配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--包扫描器-->
<context:component-scan base-package="cn.dawn.day24fileupload"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/day24/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!--多部分文件解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--文件名的编码-->
<property name="defaultEncoding" value="UTF-8"></property>
<!--限制文件大小--><!--这个单位是byte,我这儿限制的是20mb最大容量-->
<property name="maxUploadSize" value=""></property>
</bean> <!--绑定注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven> </beans>

  4.修改web.xml的中央调度器的上下文配置位置为上面那个xml

  5.在webapp下你jsp页面的那个包下创建一个文件夹,我的叫upload,里面随便扔个东西,编译后,如果即使这样target目录下还没有upload这个文件夹的话,就手动创建

  6.启动tomcat,访问fileuploadmore.jsp页面

SSM-SpringMVC-32:SpringMVC中灌顶传授文件上传的更多相关文章

  1. Spring中MultipartHttpServletRequest实现文件上传

    Spring中MultipartHttpServletRequest实现文件上传 转贴自:http://my.oschina.net/nyniuch/blog/185266 实现图片上传  用户必须能 ...

  2. ASP.NET 中对大文件上传的简单处理

    在 ASP.NET 开发的过程中,文件上传往往使用自带的 FileUpload 控件,可是用过的人都知道,这个控件的局限性十分大,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的 ...

  3. 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

     1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mv ...

  4. springMVC两种方式实现多文件上传及效率比较

    springMVC实现 多文件上传的方式有两种,一种是我们经常使用的以字节流的方式进行文件上传,另外一种是使用springMVC包装好的解析器进行上传.这两种方式对于实 现多文件上传效率上却有着很大的 ...

  5. springMVC+spring+mybatis整合(包括文件上传和下载)

    driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncod ...

  6. struts2中简单的文件上传

    2016-08-31 一.       文件上传 利用commons-fileupload-1.2.1.jar实现简单的上传文件,首先在页面上填写表单,记得加上enctype="multip ...

  7. SecureCRT中的ftp文件上传

    原文地址:http://www.blogbus.com/jjuan-flake-logs/59745331.html SecureCRT与SshClient不同的就是,SecureCRT没有图形化的文 ...

  8. Spring中MultipartHttpServletRequest实现文件上传 生成缩略图

    转贴自:http://my.oschina.net/nyniuch/blog/185266 实现图片上传  用户必须能够上传图片,因此需要文件上传的功能.比较常见的文件上传组件有Commons Fil ...

  9. ASP.NET中的FileUpload文件上传控件的使用

    本篇文章教大家如何将客户端的图片或者文件上传到服务器: 无论是上传图片(.jpg .png .gif等等) 文档(word excel ppt 等等). 第一步:放入以下三个控件 Image控件,Fi ...

随机推荐

  1. 滑动UITableViewCell出现多个按钮

    iOS > = 5.0使用第三方效果图 iOS> = 8.0使用系统方法效果图 MGSwipeTableCell(Github上的三方库)- iOS >= 5.0 直接使用比较简单 ...

  2. 学习C++模板,初体验

    最近,看了很多码神级人物的代码,发现其代码很炫酷,尤其对模板的使用,作为小码农,感觉已经落伍了,所以应该发奋图强,好好学习和掌握模板这个东西. 模板是什么呢?有人说一个模板就是一个创建类或函数的蓝图或 ...

  3. Linux下的 .o、.a、.so文件

    http://blog.sina.com.cn/s/blog_656681710100qzmy.html 工程里很多函数只是有声明,找不到实现的代码.因为那些实现代码已经编译成库所以看不见,我所看见的 ...

  4. 【Python】模拟radius coa报文

    Radius协议中网关设备NAS是client,实现radius协议的服务为服务端(例如freeradius),这种情况下radius server并不能主动给NAS发送信息.在 rfc3576 Dy ...

  5. 深入Lucene索引机制

    Lucene的索引里面存了些什么,如何存放的,也即Lucene的索引文件格式,是读懂Lucene源代码的一把钥匙. 当我们真正进入到Lucene源代码之中的时候,我们会发现: Lucene的索引过程, ...

  6. How tomcat works 读书笔记十五 Digester库 下

    在这一节里我们说说ContextConfig这个类. 这个类在很早的时候我们就已经使用了(之前那个叫SimpleContextConfig),但是在之前它干的事情都很简单,就是吧context里的co ...

  7. linu下C语言之BMP图片操作编程(上)

    BMP文件格式,也被称为位图图像文件或与设备无关的位图文件格式(DIB)或者只是一个位图,是 一个光栅图形 图像文件格式使用 来存储位图,数字,图片,独立的显示设备. 微软已经定义了一个特定的表示颜色 ...

  8. 基于ARM-contexA9-蜂鸣器pwm驱动开发

    上次,我们写过一个蜂鸣器叫的程序,但是那个程序仅仅只是驱动蜂鸣器,用电平1和0来驱动而已,跟驱动LED其实没什么两样.我们先来回顾一下蜂鸣器的硬件还有相关的寄存器吧: 还是和以前一样的步骤: 1.看电 ...

  9. DB Query Analyzer 6.01 is released, SQL Execute Schedule function can be used

       DB Query Analyzer is presented by Master Gen feng, Ma from Chinese Mainland. It has English versi ...

  10. LeetCode(51)- Count and Say

    题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...