SpringMVC学习笔记(六)
一.SpringMVC文件的上传
1.1.需要导入两个jar包

1.2在SpringMVC配置文件中加入
<!-- upload settings -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400000">
<property name="defaultEncoding" value="utf-8">
</property>
</bean>
注意:bean的id必须有而且必须叫multipartResolver
1.3方法代码
@RequestMapping(value="/upload",method=RequestMethod.POST)
public String upload(HttpServletRequest req) throws Exception{
MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;
MultipartFile file = mreq.getFile("file");
String fileName = file.getOriginalFilename();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/")+
"upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.')));
fos.write(file.getBytes());
fos.flush();
fos.close(); return "hello";
}
二. SpringMVC文件的下载.
2.1 准备下载源
在WebContent下新建files目录,放入aaa.txt,作为下载源
2.2 在index.jsp添加超链接作为下载入口
<a href="testResponseEntity" id="testJson">testResponseEntity</a><br/>
2.3 在handler SpringMVCTest中添加接口
@RequestMapping("testResponseEntity")
public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException{
byte[] body = null;
ServletContext servletContext = session.getServletContext();
InputStream in = servletContext.getResourceAsStream("/files/aaa.txt");
body = new byte[in.available()];
in.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=aaa.txt");
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> response = new ResponseEntity<>(body, headers, statusCode);
return response;
}
SpringMVC学习笔记(六)的更多相关文章
- SpringMVC学习笔记六:使用 hibernate-validator注解式数据校验
对客户端传过来的参数,在使用前一般需要进行校验. SpringMVC框架内置了Validator验证接口,但是实现起来太麻烦.我们一般使用 hibernate-validator进行数据校验. 1:j ...
- SpringMVC学习笔记六:使用Formatter解析或格式化数据
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6832903.html Converter可以将一种类型转换成另一种类型,是任意Object之间的类型 ...
- SpringMVC学习笔记六:类型转换器及类型转换异常处理
SpringMVC内部有类型转换器,当从Request中获取参数后,放入Controller中时,会根据Controller中指定的类型进行自动转换,当指的类型SpringMVC不能自动转换时,就需要 ...
- SpringMVC 学习笔记(六)拦截器
5.1.处理器拦截器简介 Spring Web MVC的处理器拦截器(如无特殊说明,下文所说的拦截器即处理器拦截器) 类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理. ...
- 史上最全的SpringMVC学习笔记
SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...
- SpringMVC学习笔记之二(SpringMVC高级参数绑定)
一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...
- springmvc学习笔记--REST API的异常处理
前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...
- springmvc学习笔记---面向移动端支持REST API
前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...
- java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)
java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...
随机推荐
- pickle序列化
通过pickle来序列化: # -*- coding: utf-8 -*- import pickle #-------------------序列化--------------------- zoo ...
- 3.3 哈尔小波空间W0
在3.2节我们学习了关于(3.8)定义的Vj的性质.特别的,我们可以乘以系数从一个Vj空间变换到另一个.我们这节学习V0和V1的关系. 将f1(t)∈V1投影至V0 我们考虑一个属于V1的函数f1(t ...
- Eclipse几个版本号的区别
查看Eclipse的版本号: 1. 找到eclipse安装目录 2. 进入readme文件夹,打开readme_eclipse.html 3. readme_eclipse.html呈现的第二行即数字 ...
- The server encountered an internal error that prevented it from fulfilling this request.
type Exception report message Request processing failed; nested exception is org.mybatis.spring.MyBa ...
- 基础14_转义字符和特殊字符ASCII
一.摘要 PSQL转义字符 二.PLSQL转义字符 PLSQL对应的字符和序号关系 二.PLSQL特殊字符 PLSQL对应的字符和序号关系 1. 转义字符为' '; )||'%'; --A&B ...
- OAF_开发系列08_实现OAF通过Popup参数式弹出窗口(案例)
20150711 Created By BaoXinjian
- Spark读取Hbase的数据
val conf = HBaseConfiguration.create() conf.addResource(new Path("/opt/cloudera/parcels/CDH-5.4 ...
- Combobox
1.方式一 <select id="cc" class="easyui-combobox" name="dept" style=&qu ...
- VS2010中的查找和替换中正则的使用
只是记下来怕以后忘记了: 查找:/news/(\d+).html 要匹配(\d+),记得用括号 替换为:/NewsDetails.aspx?id=$1 $1表示匹配的结果
- RabbitMQ(六)
集群 以两台机器为例: 10.10.43.207 10.10.244.244 分别安装好 rabbitmq,之后 1.修改集群机器 erlang 的 cookie 2.修改两台机器的 hosts 3. ...