Spring MVC上传文件需要如下步骤:

  1、前台页面,form属性 method设置为post,enctype="multipart/form-data"  input的type类型设置为file。

   <h>添加用户</h>
<form name="userForm" action="/file/upload2" method="post" enctype="multipart/form-data" >
选择文件:<input type="file" name="file"> <input type="submit" value="上传" >
</form>

  2、引入jar包commons-fileupload.jar以及commons-io.jar包

  3、在spring配置文件bean 注入MultipartResolver处理器

  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
  </bean>

  4.1、controller里面方法的参数处使用@RequestParam("file") CommonsMultipartFile file,指定传过来的file是CommonsMultipartFile类型
     然后即可使用IO读写文件。

    @RequestMapping("/upload")
public String addUser(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) throws IOException{
System.out.println("fileName---->" + file.getOriginalFilename()); if(!file.isEmpty()){
try {
FileOutputStream os = new FileOutputStream("D:/" + new Date().getTime() + file.getOriginalFilename());
InputStream in = file.getInputStream();
int b = 0;
while((b=in.read()) != -1){
os.write(b);
}
os.flush();
os.close();
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "/success";
}

  4.2、或者在controller直接使用spring自带的封装了的文件上传类org.springframework.web.multipart.commons.CommonsMultipartFile以及
      org.springframework.web.multipart.commons.CommonsMultipartResolver

    @RequestMapping("/upload2")
public String upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{
//
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if(multipartResolver.isMultipart(request)){
//需要有表单域name属性,否则将取不到对应的file
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request; Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
MultipartFile file = multiRequest.getFile((String)iter.next());
if(file != null){
String fileName = "demoUpload" + file.getOriginalFilename();
String path = "D:/" + fileName; File localFile = new File(path); file.transferTo(localFile);
}
}
}
return "/success";
}

Spring MVC 上传文件的更多相关文章

  1. Spring MVC上传文件

    Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...

  2. Spring MVC上传文件原理和resolveLazily说明

    问题:使用Spring MVC上传大文件,发现从页面提交,到进入后台controller,时间很长.怀疑是文件上传完成后,才进入.由于在HTTP首部自定义了“Token”字段用于权限校验,Token的 ...

  3. 解析Spring MVC上传文件

    新建一个普通的maven工程 在pom.xml文件中引入相应的坐标 <?xml version="1.0" encoding="UTF-8"?> & ...

  4. spring MVC上传文件演示

    //相比smartUpload功能上感觉确实有点心有意力不足的感觉,就安全性判断后缀,smartUpload就非常方便. public ModelAndView addFileUp(HttpServl ...

  5. Spring Mvc 上传文件Demo 实例

    返得利购物. 淘宝.京东500家商城合作,包括全面的商城返利网.注冊就送5元,购物就有返利.随时提现. 同学们,新一轮的返利大潮正在慢慢靠近,让购物都认为自己在赚钱.购物,机票.游戏.酒店旅游,地方特 ...

  6. Spring框架学习(8)spring mvc上传下载

    内容源自:spring mvc上传下载 如下示例: 页面: web.xml: <?xml version="1.0" encoding="UTF-8"?& ...

  7. MVC上传文件

    ASP.NET MVC上传文件是必段撑握的知识.加强训练才是.以前Insus.NET曾使用第三方MyAjaxForm.js :http://www.cnblogs.com/insus/p/378548 ...

  8. springboot(十七):使用Spring Boot上传文件

    上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...

  9. asp.net MVC 上传文件 System.Web.HttpException: 超过了最大请求长度

    APS.NET MVC 上传文件出现  System.Web.HttpException: 超过了最大请求长度 这个问题 原因是 默认最大上传文件大小为4096,而我提交的文件太大了. 解决方案:修改 ...

随机推荐

  1. “NOT_IN”与“NULL”的邂逅

    今天处理了一个因“NOT IN”与“NULL”邂逅导致的问题,值得思考和总结,记录在此,供参考.(感谢John抛出的问题) 我们以实验的形式先再现一下这个问题,然后对其分析,最后给出几种解决方案. 1 ...

  2. silverlight简单数据绑定2

    <Grid x:Name="LayoutRoot" Background="white" Loaded="LayoutRoot_Loaded&q ...

  3. python 将字典的键&值从byte类型转换为str类型

    def convert(data): if isinstance(data, bytes): return data.decode('ascii') if isinstance(data, dict) ...

  4. linux 搭建 nexus 私服及配置

    安装篇 1.tar -zxvf nexus-latest-bundle.tar.gz 2.cd nexus-2.13.0-01/bin 3../nexus start 这时可能提示 ********* ...

  5. Search history in "Maps"

    A friend of mine came to me with her iPhone yesterday. She wanted to know how to clear search histor ...

  6. java, poi, excel

    工作需要用java操作Excel,现在网上搜索了一下,决定选取POI包来操作.pom内容如下: <dependency> <groupId>org.apache.poi< ...

  7. WebForm---增删改(内置对象)

    一.添加 前台代码: <body> <form id="form1" runat="server"> <h1>用户添加< ...

  8. HDU 2222 Keywords Search(AC自动机入门)

    题意:给出若干个单词和一段文本,问有多少个单词出现在其中.如果两个单词是相同的,得算两个单词的贡献. 分析:直接就是AC自动机的模板了. 具体见代码: #include <stdio.h> ...

  9. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

  10. Highcharts 的实际实践一

    题记: 原先是想用chart.js 这个轻量级来完成我的需求的,结果基于我的数据不规则,所以实现不了. 我的需求: XX后台系统会产生有些报警日志. 我负责把这些数据按照图标的方式来展示. 这写报警日 ...