1、单个文件上传步骤:

添加Apache文件上传jar包

首先需要下载两个apache上传文件的jar包,commons-fileupload-1.3.1jar,commons-io-2.4.jar

具体使用版本,清根据项目进行选择

2、配置MultipartResolver处理文件

Spring mvc用的是MultipartFile来进行文件上传,所以我们需要配置MultipartResolver,用于处理表单中的file

<bean id = "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5400000">
<property name="defaultEncoding" value="UTF-8" >
</bean>

  

属性介绍:

maxUploadSize:最大上传文件的大小,单位为字节;

defaultEncoding:请求的编码格式,默认为iso-8859-1

3、编写文件上传控制器:

@Controller
public class FileUploadController {
private static final UPLOAD_DIRECTORY = propertiesUtil.get("fileupload.directory,"""); @RequestMapping(value="uploadFile",method=RequestMethod.POST);
public ModelAndView uploadFile(@RequestParam("file") MultipartFile file) {
// 判断文件是否为空
if(!file.isEmpty()) {
try{
// 判断文件目录是否存在,否则则自动生成
File directory = new File(UPLOAD_DIRECTORY);
if(!directory.exists()) {
directory.mkdir();
}
// 失败跳转视图
if(file.getSize() > 30000)
return new ModelAndView("uploadFail","msg",file)
}
}
}
}

  

Spring Mvc:用MultiPartFile上传单个文件,多个文件的更多相关文章

  1. Spring Mvc + Maven + yuicompressor 使用 profile 来压缩 javascript ,css 文件; (十)

    profile相关知识点: 在开发项目时,设想有以下场景: 你的Maven项目存放在一个远程代码库中(比如github),该项目需要访问数据库,你有两台电脑,一台是Linux,一台是Mac OS X, ...

  2. spring MVC 的MultipartFile转File读取

    转自:http://www.cnblogs.com/hahaxiaoyu/p/5102900.html 第一种方法:   MultipartFile file = xxx;         Commo ...

  3. 在上已个Java Spring MVC项目基础上加MyBatis

    代码目录: /Users/baidu/Documents/Data/Work/Code/Self/HelloSpringMVC 1. 首先在resource目录加上jdbc.properties: d ...

  4. spring MVC 的MultipartFile转File??

    MultipartFile file = xxx;         CommonsMultipartFile cf= (CommonsMultipartFile)file;         DiskF ...

  5. maven新建Spring MVC + MyBatis + Oracle的Web项目中pom.xml文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. 关于使用spring mvc前后端上传数据日期格式

    前端页面传入数据到后台,String,int等等类型都很友好,但是Date类型不能直接上传到服务器.否则会报异常![在这里插入图片描述](https://img-blog.csdnimg.cn/201 ...

  7. Spring MVC 笔记 —— Spring MVC 文件上传

    文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...

  8. Spring MVC的文件上传

    1.文件上传 文件上传是项目开发中常用的功能.为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data.只有在这种情况下,浏览器才会把用户 ...

  9. Spring MVC的文件上传和下载

    简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...

随机推荐

  1. linux下递归删除指定后缀名的文件

    以删除当前目录到所有子目录下的后缀名为rej的文件为例: find . -name "*.rej" |xargs rm -f

  2. CentOS7.2 切换成iptables规则

    关闭firewall service firewalld stop systemctl disable firewalld.service #禁止firewall开机启动 安装iptables规则: ...

  3. MVC 返回对象换成json

    错误界面: 这个就是返回对象没有转换成json 就是要再返回的头部添加application/json 代码: using System; using System.Collections.Gener ...

  4. CentOS日常维护及常用脚本

    [root@-.x.x xiewenming]# curl myip.ipip.net 当前 IP:42.62.x.x 来自于:中国 北京 北京 联通/电信 www.17ce.com  cdn解析网站 ...

  5. POJ Stockbroker Grapevine(floyd)

    https://vjudge.net/problem/POJ-1125 题意: 题意不是很好理解,首先输入一个n,表示有n个股票经纪人,接下来输入n行,每行第一个数m为该股票经纪人认识的经纪人数,然后 ...

  6. python 去除字符串两端的引号

    a='"srting"' print(a) b=eval(a) print(b) 输出 "srting" srting

  7. 如何以Root权限在Pycharm上Run、Debug

    Pycharm官网提问:https://intellij-support.jetbrains.com/hc/en-us/community/posts/206587695-How-to-run-deb ...

  8. php---------正则判断字符串中是否由汉字 数字 英文字母组成

    开发中常常用到正则表达式,分享两个常用的正则表达式,php检查字符串是否由汉字,数字,英文字母,下划线组成, 注意这里只是针对utf-8字符集的字符串检查. 数字 汉字 英文字母: if (!preg ...

  9. Linux命令详解-info

    info是一种文档格式,也是阅读此格式文档的阅读器:我们常用它来查看Linux命令的info文档.它以主题的形式把几个命令组织在一起,以便于我们阅读:在主题内以node(节点)的形式把本主题的几个命令 ...

  10. cf812B 搜索

    B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input s ...