这里记录下,方便以后复制粘贴。

maven配置

    <dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>

maven将自动引入   commons-fileupload-1.3.3.jar 和 commons-io-2.2.jar

springmvc配置

<!-- 文件上传表单的视图解析器 ,名字不要变-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="104857600"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean>

页面表单

<form method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/data/importdata">
<input type="file" name="mobiles"/>
<button type="submit">导入</button>
</form>

controller层处理,保存文件到本地

public String importData(MultipartFile mobiles) throws IOException {
//判断是否有上传文件
if (mobiles.getSize()>0) {
//存储到本地的路径
String path = "/app/updata/";
File filepath = new File(path);
//目录不存在则创建目录
if (!filepath.exists()) {
filepath.mkdirs();
}
//路径+随机生成一个不容易重复的文件名+原文件名
String pathandfile = path+IdrandomUtil.getStringRandom(10)+mobiles.getOriginalFilename();
//将上传文件写入本地
mobiles.transferTo(new File(pathandfile));
}else{
return "请选择上传文件";
}
return "成功";
}

嗯,我觉得文章就得这么写,简单明了。 复制粘贴就能用。

springmvc 文件上传(粘贴即用)的更多相关文章

  1. SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库

    SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库  /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...

  2. springmvc文件上传下载简单实现案例(ssm框架使用)

    springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...

  3. springMVC文件上传大小超过限制的问题

    [转自]https://my.oschina.net/ironwill/blog/646762 springMVC是一个非常方便的web层框架,我们使用它的文件上传也非常的方便. 我们通过下面的配置来 ...

  4. 解决springMVC文件上传报错: The current request is not a multipart request

    转自:https://blog.csdn.net/HaHa_Sir/article/details/79131607 解决springMVC文件上传报错: The current request is ...

  5. 18 SpringMVC 文件上传和异常处理

    1.文件上传的必要前提 (1)form 表单的 enctype 取值必须是:multipart/form-data(默认值是:application/x-www-form-urlencoded) en ...

  6. springmvc文件上传AND jwt身份验证

    SpringMVC文件上传 思路:1.首先定义页面,定义多功能表单(enctype=“multipart/form-data”)2.在Controller里面定义一个方法,用参数(MultipartF ...

  7. TZ_06_SpringMVC_传统文件上传和SpringMVC文件上传方式

    1.传统文件上传方式 <!-- 文件上传需要的jar --> <dependency> <groupId>commons-fileupload</groupI ...

  8. SpringMVC文件上传下载(单文件、多文件)

    前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...

  9. 踩坑の SpringMVC文件上传

    环境准备 添加两个jar包   commons-fileupload-1.2.2.jar   commons-io-2.4.jar 配置要求 在springmvc.xml中配置multipart类型解 ...

随机推荐

  1. Spring Security中html页面设置hasRole无效的问题

    Spring Security中html页面设置hasRole无效的问题 一.前言 学了几天的spring Security,偶然发现的hasRole和hasAnyAuthority的区别.当然,可能 ...

  2. 封装一个 员工类 使用preparedStatement 查询数据 (2) 使用 arrayList 集合

    创建 员工=类生成 有参构造 get set 方法 toString 方法 package cn.hph; public class emp1 { //创建员工类的属性 private int id; ...

  3. Web前端-JavaScript基础教程上

    Web前端-JavaScript基础教程 将放入菜单栏中,便于阅读! JavaScript是web前端开发的编程语言,大多数网站都使用到了JavaScript,所以我们要进行学习,JavaScript ...

  4. [Swift]LeetCode914.一副牌中的X | X of a Kind in a Deck of Cards

    In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...

  5. VMware虚拟机安装Linux系统

    许多新手连 Windows 的安装都不太熟悉,更别提 Linux 的安装了:即使安装成功了,也有可能破坏现有的 Windows 系统,比如导致硬盘数据丢失.Windows 无法开机等.所以一直以来,安 ...

  6. linux静态ip的设置

    我们经常使用虚拟机安装(我使用的linux版本是CentOS6.5),然后配置服务器的web环境,用于程序的调试.默认情况下,linux使用动态ip,每次启动linux时,它的ip地址都有可能发生变化 ...

  7. Linux自启动执行脚本方法

    1. 在/etc/rc.d/init.d/下创建脚本,要遵守service script的标准: 例如: vi /etc/rc.d/init.d/gfs #!/bin/bash#case " ...

  8. Redis学习——Windows环境下Redis的安装(二)

    一.说明 之前介绍了Linux环境下Redis的安装,这次介绍一下Windows环境下Redis的安装,首先要讲的是,Redis官方只支持Linux,还好 Microsoft Open Tech gr ...

  9. TypeScript 基础知识点整理

    一.TypeScript的特点 1.支持ES6规范 2.强大的IDE支持(集成开发环境) 允许为变量指定类型,减少你在开发阶段犯错误的几率. 语法提示,在IDE编写代码时,它会根据你所处的上下文把你能 ...

  10. C#2.0 委托

    委托 委托是一个非常不错的设计,允许我们把方法做为参数传递,实现了开放閉放原则.在方法中我们只要有一个委托占位,调用者就可以传入符合签名的方法来做不同的操作,这也面向对象开发中多态的魅力. 但是在C# ...