1、单个文件上传步骤:

添加Apache文件上传jar包

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

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

2、配置MultipartResolver处理文件

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

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

  

属性介绍:

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

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

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

  1. @Controller
  2. public class FileUploadController {
  3. private static final UPLOAD_DIRECTORY = propertiesUtil.get("fileupload.directory,""");
  4.  
  5. @RequestMapping(value="uploadFile",method=RequestMethod.POST);
  6. public ModelAndView uploadFile(@RequestParam("file") MultipartFile file) {
  7. // 判断文件是否为空
  8. if(!file.isEmpty()) {
  9. try{
  10. // 判断文件目录是否存在,否则则自动生成
  11. File directory = new File(UPLOAD_DIRECTORY);
  12. if(!directory.exists()) {
  13. directory.mkdir();
  14. }
  15. // 失败跳转视图
  16. if(file.getSize() > 30000)
  17. return new ModelAndView("uploadFail","msg",file)
  18. }
  19. }
  20. }
  21. }

  

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. [fastjson] - fastjson中 JSONObject 和 JSONArray

    /** * 对jsonObject对象进行key的获取 * @param jsonObject */ public ArrayList<String> jsonKeyRecursion(J ...

  2. HDU 1712 ACboy needs your help(分组背包入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 题意: 有个人学习n门课程,a[i][j]表示用j分钟学习第i门课程所能获得的价值,背包容量为一共有m时间 ...

  3. php 格式化时间

    <?php echo date("Y/m/d") . "<br>"; echo date("Y.m.d") . " ...

  4. 微信分享签名Java代码实现

    最近写了一个小微信签名功能,记录一下希望用到的朋友可以参考下. RestController @RequestMapping("/api/wx") public class Wei ...

  5. codefroce385E矩阵快速幂

    状态变化  (x,y,dx,dy,i) 表示i时刻熊站在(x,y)处速度向量(dx,dy)下一个状态是 ( 2x+y+dx+i , x+2y+dy+i , x+y+dx , x+y+dy , i+1 ...

  6. 移动前端兼容性笔记 - 安卓2.x 自带原生浏览器箭头问题

    这样的箭头用CSS-3实现,整段代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  7. java日志体系的思考(转)

    Java 日志缓存机制的实现 Java 日志管理最佳实践 混乱的 Java 日志体系 log日志远程统一记录 浅谈后端日志系统 Java异常处理和接口约定 用SLF4j/Logback打印日志-1 用 ...

  8. hdu 3694 10 福州 现场 E - Fermat Point in Quadrangle 费马点 计算几何 难度:1

    In geometry the Fermat point of a triangle, also called Torricelli point, is a point such that the t ...

  9. hdu 3689 杭州 10 现场 J - Infinite monkey theorem 概率dp kmp 难度:1

    J - Infinite monkey theorem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  10. ubuntu16.04 NVIDIA CUDA8.0 以及cuDNN安装

    下载CUDA 官网下载按照自己的实际情况进行选择,下载合适的版本. 官方安装指南 注意这里下载的是cuda8.0的runfile(local)文件. 安装CUDA 下载完成后,解压到当前目录,切换到该 ...