准备工作: 需要先搭建一个spirngmvc的maven项目

  • 1、加入jar包
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
  • 2、在springmvc的配置文件中,加入如下配置:
  <!--SpringMVC上传文件时,需要配置MultipartResolver处理器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="200000"/>
<!-- 指定上传文件的临时路径 -->
<!-- <property name="uploadTempDir" value="uploadTempDirectory" /> -->
</bean>
  • 3、创建Controller
package cn.van.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import java.io.File;
import java.io.IOException; /**
* Created by van on 2017-07-18.
*/
@Controller
@RequestMapping("/upload")
public class MultipartFileController { //单文件上传
@RequestMapping("/toFileUpload")
public String toUpload(){
return "fileUpload/fileUpload";
} @RequestMapping("fileUpload")
@ResponseBody
public String upload(MultipartFile multipartFile){
if(!multipartFile.isEmpty()){
//设置文件的保存路径
String filePath = "D:\\MultipartFile\\" + multipartFile.getOriginalFilename();
//转存文件
try {
multipartFile.transferTo(new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
}
return "success";
} //多文件上传
@RequestMapping("/toFileUploadFiles")
public String toUploadFiles(){
return "fileUpload/fileUploadFiles";
} @RequestMapping("fileUploadFiles")
@ResponseBody
//此处用@RequestParam("xx")来指定参数名,不加会报错
public String uploadFiles(@RequestParam("multipartFile") MultipartFile[] multipartfiles) throws IOException {
String savePath = "D:\\MultipartFile\\";
if(multipartfiles != null && multipartfiles.length != 0){
if(null != multipartfiles && multipartfiles.length > 0){
//遍历并保存文件
for(MultipartFile file : multipartfiles){
file.transferTo(new File(savePath + file.getOriginalFilename()));
}
}
}
return "success";
}
}
  • 4、写两个简单的上传页面(单文件和多文件)

单文件:

<%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/jsp/common/common.jsp"%>
<html>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<style type="text/css">
.upload {
margin-top: 100px;
margin-left: 100px;
text-align: center;
} </style> </head>
<body>
<h1 style="text-align: center;margin-top: 20px">test</h1>
<div>
<form class="upload" action="${path}/upload/fileUpload" method="post" enctype="multipart/form-data">
<p>
选择文件:<input type="file" name="multipartFile"/>
</p>
<p></p>
<p style="margin-top: 20px;">
<input style="" type="submit" value="上传并检测"/>
</p>
</form>
</div>
</body>
</html>

多文件:

<%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/jsp/common/common.jsp"%>
<html>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<style type="text/css">
.upload {
margin-top: 100px;
margin-left: 100px;
text-align: center;
} </style> </head>
<body>
<h1 style="text-align: center;margin-top: 20px">test</h1>
<div>
<form class="upload" action="${path}/upload/fileUploadFiles" method="post" enctype="multipart/form-data">
<p>
选择文件:<input type="file" name="multipartFile"/>
<input type="file" name="multipartFile"/>
<input type="file" name="multipartFile"/>
</p>
<p></p>
<p style="margin-top: 20px;">
<input style="" type="submit" value="上传并检测"/>
</p>
</form>
</div>
</body>
</html>

5、访问页面,选择本地文件,上传成功。

Spring MVC - MultipartFile实现文件上传(单文件与多文件上传)的更多相关文章

  1. Spring MVC MultipartFile实现图片上传

    <!--Spring MVC xml 中配置 --><!-- defaultEncoding 默认编码;maxUploadSize 限制大小--><!-- 配置Multi ...

  2. spring mvc源码-》MultipartReques类-》主要是对文件上传进行的处理,在上传文件时,编码格式为enctype="multipart/form-data"格式,以二进制形式提交数据,提交方式为post方式。

    spring mvc源码->MultipartReques类-> MultipartReques类主要是对文件上传进行的处理,在上传文件时,编码格式为enctype="multi ...

  3. Spring MVC(十四)--SpringMVC验证表单

    在Spring MVC中提供了验证器可以进行服务端校验,所有的验证都必须先注册校验器,不过校验器也是Spring MVC自动加载的,在使用Spring MVC校验器之前首先要下载相关的jar包,下面是 ...

  4. spring mvc 避免IE执行AJAX时,返回JSON出现下载文件

    <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" c ...

  5. Spring MVC 用post方式提交表单到Controller乱码问题,而get方式提交没有乱码问题

    在web.xml中添加一个filter,即可解决post提交到Spring MVC乱码问题 <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码--> <filter> ...

  6. spring mvc MultipartFile 上传文件错误解决

    Field error in object 'xxxx' on field 'xxxx': rejected value [20129259128131.jpg]; codes [typeMismat ...

  7. spring mvc MultipartFile 上传文件 当文件较小时(10k) ,无法上传成功 。

    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipartResolver" cla ...

  8. spring MVC 项目 WEB-INF下的jsp不能加载css文件

    一.项目目录 二.解决方法(已解决) 1. jsp文件加入 <link href="<c:url value="/css/main.css" />&qu ...

  9. Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)

    <bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...

随机推荐

  1. c#运用反射获取属性和设置属性值

    /// <summary> /// 获取类中的属性值 /// </summary> /// <param name="FieldName">&l ...

  2. VS中的调试相关的技巧

    1. 可以设置断点的命中条件:

  3. hdu 2688

    这题,因为要统计之前有多少个数比当前的数小,所以我用的树状数组 基本代码就是这样,还是比较好想的,只不过我没想出来罢了 #include <iostream> #include <c ...

  4. 二分图匹配-HK算法

    先把代码贴上,其他南京回来再补了.. #include <cstdio> #include <cstdlib> #include <cstring> #includ ...

  5. Python自动化开发 - 进程、线程(一)

    本节内容 1.操作系统发展史 2.进程和线程 3.Python threading 模块 一.操系统发展史 手工操作(无操作系统) 1946年第一台计算机诞生--20世纪50年代中期,还未出现操作系统 ...

  6. Web应用安全之Response Header里的敏感信息

    Web应用安全之Response Header 文/玄魂 目录 Web应用安全之Response Header 前言 1.1  那些敏感的header 1.2 删除敏感的header 1.2.1 删除 ...

  7. asp.net core sdk & runtime 镜像[已更新至2.2.0]

    在官方镜像的脚本上, 增加了System.Drawing相关的依赖库 以北京时间为默认的时间 2.2.0 Windows SDK地址: 官方: https://dotnetcli.blob.core. ...

  8. 2019年微服务实践第一课,网易&谐云&蘑菇街&奥思技术大咖深度分享

    微服务的概念最早由Martin Fowler与James Lewis于2014年共同提出,核心思想是围绕业务能力组织服务,各个微服务可被独立部署,服务间是松耦合的关系,以及数据和治理的去中心化管理.微 ...

  9. Color the ball(HDU1556)树状数组

    每次对区间内气球进行一次染色,求n次操作后后所有气球染色次数. 树状数组,上下区间更新都可以,差别不大. 1.对于[x,y]区间,对第x-1位减1,第y位加1,之后向上统计 #include<b ...

  10. 代码审计| HDWiki 漏洞(一)

    i春秋核心白帽:yanzm 0×00 背景 最近拿到一个源码素材于是对这个源码进行分析发现了不少漏洞本篇先将一些漏洞进行分析下一篇再进行GetShell方法的分析期待和师傅们的交流. 0×01 漏洞分 ...