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. HBuilder 获取通话记录 (Android)

    代码: Date.prototype.Format = function (fmt) { var o = { , //月份 "d+": this.getDate(), //日 == ...

  2. Vue.js图片预览插件

    vue-picture-preview-extend vue-picture-preview的扩展版本,本文中插件是由其他大神开发,我做了一些扩展,原文链接:https://segmentfault. ...

  3. Java虚拟机体系结构分析

    下图是JAVA虚拟机的结构图: 每个Java虚拟机都有一个类装载子系统,它根据给定的全限定名来装入类型(类或接口).同样,每个Java虚拟机都有一个执行引擎,它负责执行那些包含在被装载类的方法中的指令 ...

  4. UVALive 5903 Piece it together 二分匹配,拆点 难度:1

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. 网路防火墙iptables

    linux操作系统自身可以充当交换机,还可以当路由器,也就是说linux多网卡之间拥有互相转发数据包的能力,这种能力的实现主要依靠的是防火墙的功能进行数据包的转发和入站. 路由选择点,就是在一个点分辨 ...

  6. input type="file"在各个浏览器下的默认样式,以及修改自定义样式

    一.<input type="file"/>在各个浏览器中的默认样式: 系统 浏览器 样式效果 点击效果 mac google 点击按钮和输入框都可以打开文件夹 mac ...

  7. maven编译报错 -source 1.5 中不支持 lambda 表达式(转)

    原文链接:http://blog.csdn.net/kai161/article/details/50379418 在用maven编译项目是由于项目中用了jdk 1.8, 编译是报错  -source ...

  8. java远程下载文件到本地

    方法一 ** * 下载远程文件并保存到本地 * * @param remoteFilePath-远程文件路径 * @param localFilePath-本地文件路径(带文件名) */ public ...

  9. Java判断String类型变量是否可以转换数字类型

    正则表达式 首先要import java.util.regex.Pattern 和 java.util.regex.Matcher public boolean isNumeric(String st ...

  10. Sql 基础问题

    Ref Projection and Selection 联结查询的原理(笛卡尔积) 设计 MySQL 数据表的时候一般都有一列为自增 ID,这样设计原因是什么,有什么好处?