spring mvc文件上传和下载
首先要导入2个包(上传文件包和io的包)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkoAAAAuCAIAAABYu6GrAAAOS0lEQVR4nO2dz28aRxTH+bf4B/qfcPS951Y1UqUiq3GrHEo3caIcKnBCV9QbaidOYveA5XJJLVqpsg+hSt0osfm5LAvqYX+9mXnzAwwY1u8jH8wwP96sZ9935814J+MTBEEQROrI3LYBBEEQBDF/SN4IgiCIFELyRhAEQaQQkjeCIAgihZC8EQRBECmE5I0gCIJIIRp5O5Tz4sWLWq22HCsJgiAIYir08uaPRr4/Go/98difTMaTydj3R5PJuF6vO45DCkcQBEGsIBp5e/ny5WjkiQo3mYyPj4/7/f7e3t7+/v5STCUIgiAIUzTydnBw4HlDVOFqtZrjOGmcwDUL2Uwmk8kVCtlMttD0fd/OZXL2fCqfR1V2LrRraTTjS0EQ6abl5DeshpjesNDkJdBy8ht5p3WrNqwnGnnb39/3hu6r02b97Pzk7OLk7KL+x/nJ2UX97Pz173/+02pdnJ+nTN6ahWwoQM31lDc7l8lI2giFO4NnUHxL8tZy8hshwME0rDAt8D5MMuKHWk6ezwrcadIEnwXLg5oh932iSYrmJHXGSdJCNwV0T92dBTIneWtYs14i9iLkndYayhs+VDRDju1ekNlqSGszQCNvtVpt6A4OT94GHz3P+/Sb5Xme7/uHJ2+DaZzjOMbNrQGYdqyLvDUL2Uwmm8tl0TbsXFwq0XDDb+84DYu5RYN7MPE6fsPaAIkbectCXGTLyefzef72bDn5vNOCVYieMcmDm5GUk4sqZ5KqOVmds3tsU2TSsswmVkLeJC2tk7wJvVcPuTAZjLWkqzC7ZJDjaOTNcRx30N97c+q67mAw+M/5+vLI+tT42XXdvTenJG8zVb/o4KSJOjULWUUV6m/vOJHWMF6Ic0mIhwqKCfd1lMB8ETXB5UHNgKhcKmuSqjlpnSRvJG+GIL03GHJR9xoWO0mDRacaIhp5q1arg36/VDtqt9tXV1f/Vr+63P7sw4cPHz9+LNWOgnW4arWKFbVz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YM8gY3skr1D5M3g56z9WlbUV4EVN5uW9BYVOF0TdweEHUPxhZ09SnRyFupVOq221/cs759WGJ+dspffvcg+Je4UqkklIMO3y6EKsTG+sIPN5Y3o5ojcWoWCszKlmC4IjiJqxu2ux9LSyoQI6Wg8mYhS7O31QMNxoEblb/jpPewySqCPA9mRrLJUR0yNGguLo/Xif86V3AjAwtBm9wsOnb9ySTZYdd9gL/E/2pcBvbZRAwMiq0jHREjqbgZ2ouABye1NiwTMfQt2+poIm8+F6Bf1OytWCx22u1uu93rdHrdbr/XHfR6g37fHQyGrusNXc8bFotFpGQydVHv1Li5vJnUnMy9kqyyPfxTbS2BWZgI5zTyBi5WNpej2dvKwceC+AUqcL/xczXuRpTvQ0wKqnZQYmYgGxcYUJMkzWnqFDdczBfO1DDoKBEpwRLhzxEnwO3oYWK0qCVkaDn5DcsSOwp8r/KKh9cIM4tvZXZ50//VlwU+4GGqLIyBXjo+WB9tyFKWk6KRt62trc71dad9HSlch1W4gTd0t7a2jJsjAKRfdxiTCMvKhaAIYq3QyNvm5mb76ipWuC6mcJubm8uxNWXQS0vuMKRuBLFwNPL2uRnLsTU9hCFNmroRBEEsCjrOlCAIgkghJG8EQRBECvkfblQnzOr5hB8AAAAASUVORK5CYII=" alt="" />
配置文件spring-servlet.xml文件中
<!-- 配置文件上传MultipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5000000"></property>
</bean>
如果id="multipartResolver"没传,就会报以下的错(小心小心)
Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
添加文件的显示页面add.sjp
a、enctype="multipart/form-data"这句就是告诉,要添加文件,很重要
b、 attach:<input type="file" name="attach"><br/>
<form:form modelAttribute="user" method="post" enctype="multipart/form-data">
ID:<form:input path="id"/><br/>
userName:<form:input path="userName"/><form:errors path="userName"/><br/>
password:<form:input path="password"/><form:errors path="password"/><br/>
email:<form:input path="email"/><form:errors path="email"/><br/>
attach:<input type="file" name="attach"><br/>
<input type="submit" value="提交"/>
</form:form>
修改添加的方法
a、指定路径
b、上传文件,就要文件保存的目标位置
@RequestMapping(value="/add",method=RequestMethod.POST)
public String add(@Validated User user,BindingResult bindingResult,MultipartFile attach,HttpServletRequest request){
//上传文件就是要上传的路径所以就需要request对象
if(bindingResult.hasErrors()){
return "user/add";
}
//指定上传位置
String uploadFilePath = request.getSession().getServletContext().getRealPath("statics/upload");
System.out.println(uploadFilePath); System.out.println(attach.getOriginalFilename());//原始名
System.out.println(attach.getContentType());//类型
System.out.println(attach.getName());//页面表单控件name的值 if(!attach.isEmpty()){
try {
//"/" 更换成自动识别分隔符File.separator
File saveFile = new File(uploadFilePath+File.separator+attach.getOriginalFilename());
//第一个是拿到输入流,第二个参数是保存到哪里
FileUtils.copyInputStreamToFile(attach.getInputStream(), saveFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//把user对象放进去key(id)
userList.put(user.getId(), user); // return "user/userlist";//返回到逻辑视图名
//(添加后,不能把添加过的数据展示出来)服务器端行为
return "redirect:/user/list";//客户端重新发送请求(把添加进去的展示出来)
}
多文件的上传
因为是多文件所以一定要加@RequestParam不加的话报如下异常
//Could not instantiate bean class [[Lorg.springframework.web.multipart.MultipartFile;]: No default constructor found;nested exception is java.lang
//不能实例化bean类[[Lorg.springframework.web.multipart。MultipartFile;]:没有默认构造函数发现,嵌套异常. lang。
//页面500
//文件上传(多文件)
@RequestMapping(value="/add",method=RequestMethod.POST)
public String add(@Validated User user,BindingResult bindingResult,@RequestParam("attachs") MultipartFile[] attachs,HttpServletRequest request){
//上传文件就是要上传的路径所以就需要request对象
if(bindingResult.hasErrors()){
return "user/add";
}
//指定上传位置
String uploadFilePath = request.getSession().getServletContext().getRealPath("statics/upload");
try {
for(MultipartFile attach:attachs){ if(!attach.isEmpty()){ //"/" 更换成自动识别File.separator
File saveFile = new File(uploadFilePath+File.separator+attach.getOriginalFilename());
//第一个是拿到输入流,第二个参数是保存到哪里
FileUtils.copyInputStreamToFile(attach.getInputStream(), saveFile);
}
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//把user对象放进去key(id)
userList.put(user.getId(), user); // return "user/userlist";//返回到逻辑视图名
//(添加后,不能把添加过的数据展示出来)服务器端行为
return "redirect:/user/list";//客户端重新发送请求(把添加进去的展示出来)
}
因为多文件是一个数组,attach:<input type="file" name="attachs"><br/>
这边的name要是attachs,否则就找不到
<form:form modelAttribute="user" method="post" enctype="multipart/form-data">
ID:<form:input path="id"/><br/>
userName:<form:input path="userName"/><form:errors path="userName"/><br/>
password:<form:input path="password"/><form:errors path="password"/><br/>
email:<form:input path="email"/><form:errors path="email"/><br/>
attach:<input type="file" name="attachs"><br/>
attach1:<input type="file" name="attachs"><br/>
attach2:<input type="file" name="attachs"><br/>
<input type="submit" value="提交"/>
</form:form>
spring mvc文件上传和下载的更多相关文章
- MVC文件上传与下载
MVC文件上传与下载 MVC文件上传与下载 想想自己从毕业到工作也有一年多,以前公司的任务的比较重,项目中有的时候需要用到什么东西都去搜索一下,基础知识感觉还没有以前在学校中的好.最近开始写博客,真的 ...
- Spring MVC 笔记 —— Spring MVC 文件上传
文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...
- Spring MVC文件上传教程 commons-io/commons-uploadfile
Spring MVC文件上传教程 commons-io/commons-uploadfile 用到的依赖jar包: commons-fileupload 1.3.1 commons-io 2.4 基于 ...
- 【Java Web开发学习】Spring MVC文件上传
[Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...
- Spring mvc文件上传实现
Spring mvc文件上传实现 jsp页面客户端表单编写 三个要素: 1.表单项type="file" 2.表单的提交方式:post 3.表单的enctype属性是多部分表单形式 ...
- spring boot文件上传、下载
主题:Spring boot 文件上传(多文件上传)[从零开始学Spring Boot]http://www.iteye.com/topic/1143595 Spring MVC实现文件下载http: ...
- Spring mvc 文件上传到文件夹(转载+心得)
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- spring mvc 文件上传 ajax 异步上传
异常代码: 1.the request doesn't contain a multipart/form-data or multipart/mixed stream, content type he ...
- Spring MVC文件上传下载(转载)
原文地址: http://www.cnblogs.com/WJ-163/p/6269409.html 上传参考 http://www.cnblogs.com/lonecloud/p/5990060.h ...
随机推荐
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 移动统计工具Flurry
网址:http://www.flurry.com/ 1注册和下载对应SDK 2集成SDK 3自定义统计项 至于怎么使用,后期会更新
- Operators一句话介绍(RxJava版)
Cold Observables 在第一个subscriber订阅后才执行事件发送的Observables,默认普通Observables都是这个类型 Cold Observables对于每个订阅的s ...
- C#出题库项目的总结(1)
前记: 截止18点05分(4月9号),完成了代码部分的编写,明天争取把文档完成,毕竟在这个上面花的时间太多了,还有单词和书需要背,好吧,不说废话啦,下面进入正题. (PS:因为学院的模拟招聘笔试出题和 ...
- js对象的两种写法
<script> //定义一个对象,提供对应的方法或者属性 var s = { sd1: function () { }, sd2: fun ...
- python3 入门 (四) 类与继承
Python 类 Python中的类提供了面向对象编程的所有基本功能:类的继承机制允许多个基类,派生类可以覆盖基类中的任何方法,方法中可以调用基类中的同名方法. 对象可以包含任意数量和类型的数据. p ...
- Coding the Matrix (1):向量
1. list 画点 >>> from plotting import plot >>> L = [[2, 2], [3, 2], [1.75, 1], [2, 1 ...
- Grovvy之解析XML文件
假设现有customer.xml 文件内容如下: <?xml version="1.0" ?> <customers> <corporate> ...
- 配置ipvsadm服务
LVS的三种包转发方式 LVS提供了三种包转发方式:NAT(网络地址映射).IP Tunneling(IP隧道).Direct Routing(直接路由) 不同的转发模式决定了不同的 ...
- tomcat 简介
一,Tomcat简介 Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项 ...