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. ubuntu 18.04 64bit如何安装GPU版本tensorflow

    注:笔者的ubuntu18.04 64bit已经安装好了显卡驱动,因此没有此步操作 1.获取cuda(https://developer.nvidia.com/cuda-downloads,选择ubu ...

  2. openwrt下使用wget出现Failed to allocate uclient context

    一.场景重现 root@OpenWrt:/# wget www.baidu.com Downloading 'www.baidu.com' Failed to allocate uclient con ...

  3. 将vi打造成IDE

    一.环境 发行版:Ubuntu 18.04 LTS 代号:bionic 内核版本:4.15.0-33-generic 二.步骤 2.1 准备工作 sudo apt-get install python ...

  4. HDU 1317 XYZZY(floyd+bellman_ford判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=1317 题意: 给出一个有向图,每到达一个点,都会加上或减去一些能量,我们要做的就是判断从1出发是否能到达n.初始 ...

  5. SQL Server 跨服务器 不同数据库之间复制表的数据

    不同数据库之间复制表的数据的方法: 当表目标表存在时: insert into 目的数据库..表 select * from 源数据库..表 当目标表不存在时: select * into 目的数据库 ...

  6. III USP Freshmen ContestH. MaratonIME gets candies

    这题挺有意思的,刚开始不会这交互题,模仿着做了一题就会了,蛮简单 的 这题我用2分,结果wa了,想了一下发现,1到1e9二分50次完全不够用啊,那就转换一下思维,先求出在10^n~10^(n+1)的n ...

  7. 2-12-配置squid代理服务器加快网站访问速度

    本节所讲内容: squid服务器常见概念 squid服务器安装及相关配置文件 实战:配置squid正向代理服务器 实战:配置透明squid代理提升访问速度 实战:配置squid反向代理加速度内网web ...

  8. linux 网络测试命令 长期更新

    一.网络测试命令 1.测试 网络连接 发送两包后停发 [oracle@hadoop ~]$ PING www.a.shifen.com (() bytes of data. bytes from tt ...

  9. FireFox和IE下使用Date来构造新Date对象的BUG

    正常方式 我们都知道可以使用new Date()来创建一个Date对象. new Date();//Date {Mon Jun 15 2015 15:53:16 GMT+0800} 也可以用new D ...

  10. Bitdefender Total Security 2014 Free 6 Months & 12 month License Key

    German Only – Bitdefender Total Security 2014 Free 6 Months Serial License Keyhttp://www.bitdefender ...