一、简述

一个javaWeb项目中,文件上传功能几乎是必不可少的,本人在项目开发中也时常会遇到,以前也没怎么去理它,今天有空学习了一下这方面的知识,于是便将本人学到的SpringMVC中单文件与多文件上传这部分知识做下笔记。

二、单文件上传

1、页面

这里以一个简单的表单提交为例子,文件上传需要将表单的提交方法设置为post,将enctype的值设置为"multipart/form-data"。

<form action="${pageContext.request.contextPath}/test/upload.do" method="post" enctype="multipart/form-data"><input type="file" name="img"><br /> <input type="submit" name="提交">
</form>复制代码

2、控制器

在Controller的处理方法中,使用MultipartFile对象作为参数接收前端上传过来的文件,具体说明请看代码注释。

@Controller
@RequestMapping("/test")
public class MyController {@RequestMapping(value = "/upload.do", method = RequestMethod.POST)// 这里的MultipartFile对象变量名跟表单中的file类型的input标签的name相同,所以框架会自动用MultipartFile对象来接收上传过来的文件,当然也可以使用@RequestParam("img")指定其对应的参数名称public String upload(MultipartFile img, HttpSession session)throws Exception {// 如果没有文件上传,MultipartFile也不会为null,可以通过调用getSize()方法获取文件的大小来判断是否有上传文件if (img.getSize() > 0) {// 得到项目在服务器的真实根路径,如:/home/tomcat/webapp/项目名/imagesString path = session.getServletContext().getRealPath("images");// 得到文件的原始名称,如:美女.pngString fileName = img.getOriginalFilename();// 通过文件的原始名称,可以对上传文件类型做限制,如:只能上传jpg和png的图片文件if (fileName.endsWith("jpg") || fileName.endsWith("png")) {File file = new File(path, fileName);img.transferTo(file);return "/success.jsp";}}return "/error.jsp";}
}复制代码

3、springmvc.xml配置

使用MultipartFile对象接收前端上传过来的文件,还需要在springmvc的配置文件中进行如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">...<!-- 注意:CommonsMultipartResolver的id是固定不变的,一定是multipartResolver,不可修改 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 如果上传后出现文件名中文乱码可以使用该属性解决 --><property name="defaultEncoding" value="utf-8"/><!-- 单位是字节,不设置默认不限制总的上传文件大小,这里设置总的上传文件大小不超过1M(1*1024*1024) --><property name="maxUploadSize" value="1048576"/><!-- 跟maxUploadSize差不多,不过maxUploadSizePerFile是限制每个上传文件的大小,而maxUploadSize是限制总的上传文件大小 --><property name="maxUploadSizePerFile" value="1048576"/></bean><!-- 设置一个简单的异常解析器,当文件上传超过大小限制时跳转 --><bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="defaultErrorView" value="/error.jsp"/></bean>
</beans>复制代码

上面配置文件中的CommonsMultipartResolver下的属性值配置不是必须的,你也可以全部不写。到这里就可以实现单个文件上传了,下面来看看多文件上传。

三、多文件上传

其实多文件上传也很简单,单文件上传是在Controller的处理方法中使用MultipartFile对象作为参数接收前端上传过来的文件,而多文件上传则使用MultipartFile对象数组来接收。

1、页面

该页面中有几个name值一样的file类型的input标签,其他跟单文件上传的页面没差。

<form action="${pageContext.request.contextPath}/test/upload.do" method="post" enctype="multipart/form-data">file 1 : <input type="file" name="imgs"><br /> file 2 : <input type="file" name="imgs"><br /> file 3 : <input type="file" name="imgs"><br /> <input type="submit" name="提交">
</form>复制代码

2、控制器

控制器中的处理方法使用MultipartFile[]数组作为接收参数,并不能直接使用,需要校正参数,具体说明请看代码注释。

@Controller
@RequestMapping("/test")
public class MyController {@RequestMapping(value = "/upload.do", method = RequestMethod.POST)// 这里的MultipartFile[] imgs表示前端页面上传过来的多个文件,imgs对应页面中多个file类型的input标签的name,但框架只会将一个文件封装进一个MultipartFile对象,// 并不会将多个文件封装进一个MultipartFile[]数组,直接使用会报[Lorg.springframework.web.multipart.MultipartFile;.<init>()错误,// 所以需要用@RequestParam校正参数(参数名与MultipartFile对象名一致),当然也可以这么写:@RequestParam("imgs") MultipartFile[] files。public String upload(@RequestParam MultipartFile[] imgs, HttpSession session)throws Exception {for (MultipartFile img : imgs) {if (img.getSize() > 0) {String path = session.getServletContext().getRealPath("images");String fileName = img.getOriginalFilename();if (fileName.endsWith("jpg") || fileName.endsWith("png")) {File file = new File(path, fileName);img.transferTo(file);}}}return "/success.jsp";}
}复制代码

同样的,使用MultipartFile数组接收前端上传过来的多个文件,也需要在springmvc的配置文件进行配置,具体配置与上述单文件上传的springmvc.xml配置没差,直接拷贝过来就行。这样,就可以进行多文件上传了。

四、多种文件上传情景综合

当然,项目开发中,场景可能并不是这么简单,上述的多文件上传是一个个文件选择后一起上传(即多个name相同的input标签),那要是我项目中只要一个input标签就可以一次性多个文件呢?又或者一个页面中既要一个个选择的多文件上传,又要一次性选择的多文件上传,还要有单文件上传呢?没问题,MultipartFile[]通吃,代码也很easy,下面直接上代码。

1、页面

这里的 “一次选择多个文件的多文件上传” 只是在input标签中加上了multiple属性而已。

<form action="${pageContext.request.contextPath}/test/upload.do" method="post" enctype="multipart/form-data">一次选择多个文件的多文件上传 : <br /> <input type="file" name="imgs1" multiple><br /> <br /> 一次选择一个文件的多文件上传 : <br /> <input type="file" name="imgs2"><br /> <input type="file" name="imgs2"><br /><br /> 单文件上传 : <br /> <input type="file" name="imgs3"><br /><br /> <input type="submit" name="提交">
</form>复制代码

2、控制器

@Controller
@RequestMapping("/test")
public class MyController {@RequestMapping(value = "/upload.do", method = RequestMethod.POST)public String upload(@RequestParam MultipartFile[] imgs1,@RequestParam MultipartFile[] imgs2,@RequestParam MultipartFile[] imgs3, HttpSession session)throws Exception {String path = session.getServletContext().getRealPath("images");for (MultipartFile img : imgs1) {uploadFile(path, img);}for (MultipartFile img : imgs2) {uploadFile(path, img);}for (MultipartFile img : imgs3) {uploadFile(path, img);}return "/success.jsp";}private void uploadFile(String path, MultipartFile img) throws IOException {if (img.getSize() > 0) {String fileName = img.getOriginalFilename();if (fileName.endsWith("jpg") || fileName.endsWith("png")) {File file = new File(path, fileName);img.transferTo(file);}}}
}复制代码

MultipartFile[]就是如此强大,不管单个多个,逻辑处理一样,所以建议在项目开发中使用MultipartFile[]作为文件的接收参数。

五、拓展

1、MultipartFile类常用的一些方法:

String getContentType()//获取文件MIME类型
InputStream getInputStream()//获取文件流
String getName() //获取表单中文件组件的名字
String getOriginalFilename() //获取上传文件的原名
long getSize()  //获取文件的字节大小,单位byte
boolean isEmpty() //是否为空
void transferTo(File dest) 复制代码

2、CommonsMultipartResolver的属性解析

defaultEncoding:表示用来解析request请求的默认编码格式,当没有指定的时候根据Servlet规范会使用默认值ISO-8859-1。当request自己指明了它的编码格式的时候就会忽略这里指定的defaultEncoding。
uploadTempDir:设置上传文件时的临时目录,默认是Servlet容器的临时目录。
maxUploadSize:设置允许上传的总的最大文件大小,以字节为单位计算。当设为-1时表示无限制,默认是-1。
maxUploadSizePerFile:跟maxUploadSize差不多,不过maxUploadSizePerFile是限制每个上传文件的大小,而maxUploadSize是限制总的上传文件大小。
maxInMemorySize:设置在文件上传时允许写到内存中的最大值,以字节为单位计算,默认是10240。
resolveLazily:为true时,启用推迟文件解析,以便在UploadAction中捕获文件大小异常。复制代码

六、注意

  1. 在开发过程中,建议把配置文件中的异常解析器(SimpleMappingExceptionResolver)先注释掉,方便我们查看错误。
  2. 有时候上传出错,是因为我们在配置文件中限制了上传文件的大小,你可以不加这个限制,但个人建议这个限制最好还是加上,具体文件大小限制请根据公司项目情况而定。
  3. SpringMVC中使用MultipartFile接收上传文件需要依赖两个jar包,分别是:commons-fileupload-1.3.3.jar、commons-io-2.5.jar。

SpringMVC 单文件上传与多文件上传相关推荐

  1. Dropzone单文件上传、多文件上传、文件夹上传,springmvc接收,上传至Minio的一系列问题

    0 前言 1.项目需要上传文件和大量的文件夹,页面只有一个input file标签会很丑,偶然间得知dropzone类库, 决定使用. 2. 项目后端采用springmvc接收,调用minio代码上传 ...

  2. SSM之SpringMVC 04 —— Ajax、拦截器、文件上传和下载

    系列文章 SSM之SpringMVC 01 -- SpringMVC原理及概念.Hello SpringMVC 注解版和配置版 SSM之SpringMVC 02 -- Controller和RestF ...

  3. elementui 按钮 表单_前后端分离,文件上传下载(springBoot+vue+elementUI)

    1.介绍 本文主要是介绍前后端分离的上传下载,后端使用的是SpringBoot,持久层用的是mybatis-plus,前端用的Vue,UI用的elementUI,测试了一下,文本,图片,excel,都 ...

  4. nginx delete form表单 收不到参数_HTTP 文件上传的一个后端完善方案(NginX)

    (给PHP开发者加星标,提升PHP技能) 转自:林伯格 https://breeze2.github.io/blog/scheme-nginx-php-js-upload-process 前言 很多网 ...

  5. java批量上传文件_Spring Boot2(十四):单文件上传/下载,文件批量上传

    文件上传和下载在项目中经常用到,这里主要学习SpringBoot完成单个文件上传/下载,批量文件上传的场景应用.结合mysql数据库.jpa数据层操作.thymeleaf页面模板. 一.准备 添加ma ...

  6. springboot文件上传,单文件上传和多文件上传,以及数据遍历和回显

    springboot文件上传,单文件上传和多文件上传 项目结构及pom.xml 创建文件表单页面 编写javabean 编写controller映射 MultipartFile类 @RequestPa ...

  7. python flask上传文件_flask 文件上传(单文件上传、多文件上传)--

    文件上传 在HTML中,渲染一个文件上传字段只需要将标签的type属性设为file,即. 这会在浏览器中渲染成一个文件上传字段,单击文件选择按钮会打开文件选择窗口,选择对应的文件后,被选择的文件名会显 ...

  8. 原生input标签实现ajax单文件上传和多文件上传

    自己还是一个菜鸟的时候,有次项目经理让我用Java做一个多文件上传的功能.那时候技术学得很渣,最多只能够实现单文件上传.做了一个星期都没有做出来,于是项目经理不留半点情面,当着办公室所有人的面痛批我一 ...

  9. Ruoyi实现单文件上传和多文件打包压缩包下载

    目录 单文件上传篇 1.html 2.JS 3.Controller 3.1 Global.getProfile() 3.3.1 JarBasePath.getBaseJarPathStr() 3.2 ...

最新文章

  1. MIT媒体实验室主任辞去一切职务:拿了爱泼斯坦170万美金,涉及程序违规,麻省理工宣布彻查...
  2. golang signal 信号处理
  3. 机器学习算法加强——回归
  4. boost::coroutine模块实现parallel的测试程序
  5. Nacos源码发送心跳
  6. git 代理 git_生日快乐,Git
  7. pyqt5 -——菜单和工具栏
  8. python中开平方如何表示_python平方怎么表示
  9. 张小龙2018微信公开课超时演讲,总结微信8年
  10. 【TAGE】分支预测
  11. (转发)RJ45水晶头网线的做法
  12. mybatis 自动填充无效_mybatisPlus踩坑之--自动填充
  13. 中英文排版字符间距不一致,英文自动断字
  14. 计算机考研网课平台哪个好,考研网课哪家排名好
  15. @Validated和@Valid使用
  16. Windows 开发之VC++垃圾清理程序软件
  17. 15. python-es-8.3.3-稀有词项聚合rare_tems
  18. 软件与设备交互时的查询和设置参数的功能项算是EIF还是EI呢?
  19. 中国动画腾飞的关键所在 (转)
  20. 基于java的MVC手术室预约管理系统

热门文章

  1. 第五:Python发送邮件时获取最新测试报告并发送邮件
  2. spss正态性检验_SPSS和R中的正态分布的确定和几何均值的计算
  3. python datetime和字符串如何相互转化?
  4. Spring Cloud Ribbon 负载均衡客户端调用示例
  5. 富文本编辑器CKEditor 5开发环境搭建
  6. 通过函数名字符串调用函数【C语言版】
  7. SpringCloudGateway(一) 概览
  8. 计算机网络 校园网规划,计算机网络课程校园网规划设计
  9. java保护访问,Java中的受保护的访问修饰符
  10. 计算机博士一年看多少篇文献,博士生真的要一天看20篇文献吗?