springMVC实现 MultipartFile 多文件上传
1、Maven引入所需的 jar 包(或自行下载)
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
2、配置 spring 文件
<!-- 多部分文件上传 -->
<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>
3、form 添加 enctype="multipart/form-data"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>上传多个文件 实例</h2>
<form action="/upload/filesUpload" method="post" enctype="multipart/form-data">
<p>选择文件:<input type="file" name="files"></p>
<p>选择文件:<input type="file" name="files"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
5、controller 部分
import java.io.File; 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; @Controller
@RequestMapping("/upload")
public class UploadController {
//通过Spring的autowired注解获取spring默认配置的request /***
* 保存文件
* @param file
* @return
*/
private boolean saveFile(MultipartFile file, String path) {
// 判断文件是否为空
if (!file.isEmpty()) {
try {
File filepath = new File(path);
if (!filepath.exists())
filepath.mkdirs();
// 文件保存路径
String savePath = path + file.getOriginalFilename();
// 转存文件
file.transferTo(new File(savePath));
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
} @RequestMapping("/filesUpload")
public String filesUpload(@RequestParam("files") MultipartFile[] files) {
String path = "E:/upload/";
//判断file数组不能为空并且长度大于0
if(files!=null&&files.length>0){
//循环获取file数组中得文件
for(int i = 0;i<files.length;i++){
MultipartFile file = files[i];
//保存文件
saveFile(file, path);
}
}
// 重定向
return "redirect:/list.html";
} }
运行如下:

springMVC实现 MultipartFile 多文件上传的更多相关文章
- SpringMVC 使用MultipartFile实现文件上传(转)
http://blog.csdn.net/kouwoo/article/details/40507565 一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们 ...
- SpringMVC 使用 MultipartFile 实现文件上传
该代码实现了文件上传和文本字段同时传递到后台进行处理的功能. 直接贴代码,中间涉及到的实体类就不贴了,和功能没啥关系的. Controller /** * 添加活动 * * @param req * ...
- SpringMVC:学习笔记(8)——文件上传
SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...
- SpringMVC注解方式与文件上传
目录: springmvc的注解方式 文件上传(上传图片,并显示) 一.注解 在类前面加上@Controller 表示该类是一个控制器在方法handleRequest 前面加上 @RequestMap ...
- SSM框架之SpringMVC(5)文件上传
SpringMVC(5)文件上传 1.实现文件上传的前期准备 1.1.文件上传的必要前提 A form 表单的 enctype 取值必须是: multipart/form-data(默认值是:appl ...
- 利用spring的MultipartFile实现文件上传【原】
利用spring的MultipartFile实现文件上传 主要依赖jar包 spring-web-3.0.6.RELEASE.jar 用到 (org.springframework.web.multi ...
- SpringMVC 通过commons-fileupload实现文件上传
目录 配置 web.xml SpringMVC配置文件 applicationContext.xml 文件上传 Controller 上传实现一 上传实现二 测试 依赖 配置 web.xml < ...
- SpringMvc MultipartFile 图片文件上传
spring-servlet.xml <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipar ...
- SpringMVC中使用 MultipartFile 进行文件上传下载及删除
一:引入必要的包 <!--文件上传--> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fil ...
随机推荐
- java基础知识-比较运算符
演示比较运算符 == : 判断两个值是否相等 != : 判断两个数是否不相等(不能写成<>) > :判断左边值是否大于右边值 < :判断左边值是否小于右边值 >= : 判 ...
- kmp小记
以下转载自Matrix67 ************************************************************************************** ...
- Linux vi 文本代码时显示行号或不显示行号
Linux vi 文本代码时显示行号或不显示行号 前提 安装了vim $vi ~/.vimrc 显示的话加上 set nu 不想显示的话可以注释掉 "set nu 之后 $source ~ ...
- JMS学习以及jms的实现activeMq
1.JMS规范介绍: http://www.cnblogs.com/hapjin/p/5431706.html http://elim.iteye.com/blog/1893038 http://bl ...
- Implementation of WC in JAVA
Implementation of WC in JAVA github地址 相关要求 基本功能 -c [文件名] 返回文件的字符数 (实现) -w [文件名] 返回文件的词的数目 (实现) -l [文 ...
- HTML给table添加单线边框
一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...
- Flask中的before_request和after_request
1.@app.before_request 在请求(request)之前做出响应 @app.before_request 也是一个装饰器,他所装饰的函数,都会在请求进入视图函数之前执行 2.@app. ...
- Python程序的打包-上传到pypi
pypi注册与配置 在pypi的官网:https://pypi.python.org/pypi 注册自己的账号激活账号之后,我们还需要将在本地配置一份文件 在用户的根目录创建文件 : .pypirc在 ...
- Java时间类(转)
package com.chinagas.common.utils; import java.text.ParseException; import java.text.SimpleDateForma ...
- (转)【学习笔记】通过netstat+rmsock查找AIX端口对应进程
原文:http://www.oracleplus.net/arch/888.html https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72 ...