码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC第二天

时间:2019-12-24 20:27:33      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:bean   文件大小   osi   连接   copy   局部类   end   drive   turn   

1. 全局类型转换器

    写一个类实现实现converter<S,T>接口,springmvc的配置文件中配置一个FormattingConversionServiceFactoryBean 类,在类里面给converters属性赋值,converters是set类型的集合,用set标签赋 值,值为实现converter<S,T>接口的全类名,最后在Mvc:annotation-driven标签配置一个converservice-service属性,属性名是FormattingConversionServiceFactoryBean 所在bean的id

2.局部类型转换器

    使用注解@DateTimeFormat:时间类型的类型转换器,指定pattern属性值,可以放在参数上或者bean类中的属性名上

        @NumberFormat:数值类型的类型转换器,指定pattern属性值,可以放在参数上或者bean类中的属性名上

3.文件上传

    前端:需要在表单上设置encype 属性,值为“multipart/form-data”。

    后台:方法参数上写MultipartFile file参数接收文件。

    导包:commons-fileupload.jar commons-io.jar 

    配置SpringXMl: 配置一个多媒体解析器:CommonsMultiPartResolver,设置defaultEncoding (编码)和MaxUploadSize (文件大小)属性值

    实现:

    //获取jsp域对象

    ServletContext context = request.getServletContext();

    //获取真实路径

    String realPath = context.getRealPath("/upload");

    //获取路径

    File directory = new File(realPath);

     //没有就创建

    if(!directory.exists()) {

    directory.mkdirs();

    }

    //处理文件名

    String fileName = UUID.randomUUID().toString().replaceAll("-","")+file.getOriginalFilename();

    //写入文件

    FileOutputStream fos = new FileOutputStream(new File(realPath+"/"+fileName));

    //获取文件的输入流

    InputStream in = file.getInputStream();

    //把上传输入流copy给输出流

    IOUtils.copy(in, fos);

    //关流

    in.close();

    fos.close();

    //完成

 

4.文件下载

    1.方法返回值为ResponseEntity 

    2.设置响应头信息

    实现:

    //获取文件真是路径

    String realPath = request.getServletContext().getRealPath("/WEB-INF/"+fileName);

    //输入流读取文件

    FileInputStream fis = new FileInputStream(new File(realPath));

    //把文件读到字节数组

    byte[] body = new byte[fis.available()];(fis.available())为文件的大小

    fis.read(body);

    //获取头

    MultiValueMap<String, String> headers = new HttpHeaders();

    //处理文件编码

    fileName = new String(fileName.getBytes("gbk"),"iso8859-1");

    //设置头信息

    headers.add("Content-Disposition", "attachment;filename="+fileName);

    //设置状态码

    HttpStatus statusCode = HttpStatus.OK;

    //创建ResponseEntity对象

    ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(body, headers, statusCode);

    //返回ResponseEntity 对象

    return responseEntity;

    //完成

5.拦截器(Interceptor)

    写一个类实现HanderInterceptor接口

    实现方法  * preHandle:在目标方法执行之前执行

          *     返回值:boolean类型,true表示放行该请求,false表示拦截该请求

          *     作用:日志、权限验证、获取连接

          postHandle:在目标handler方法执行之后,在视图渲染(render)之前【给页面填充数据】执行

 

         *    作用:修改域中的值,用的不多

 

         */

        * afterCompletion:在目标handler方法执行之后,在视图渲染(render)之后【给页面填充数据】执行

         *     作用:释放资源,用的不多。

    在SpringXML中配置mvc:interceptors,并在创建内部bean配置该拦截器(实现HandlerIntercepter的类)

    拦截器执行顺序:preHandle--->目标方法--->postHandle?渲染视图?afterCompletion

    多拦截器执行顺序:按照mvc:interceptors中前后顺序执行

     mvc:interceptors的子标签:mvc:intercepto 属性有<mvc:mapping  path=""> 拦截路径  <mvc:excude-mapping path=""> 直接放行的资源 (放行的优先级高 ) 

    多个拦截器方法执行顺序 :按顺序先执行所有拦截器的 preHandle,执行目标方法,倒序执行postHandle,渲染视图,倒叙执行afterCompletion

6.SpringMVC的运行流程图

    技术图片

 

 

 

 

      

    

    

SpringMVC第二天

标签:bean   文件大小   osi   连接   copy   局部类   end   drive   turn   

原文地址:https://www.cnblogs.com/sunners/p/12093287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!