• 文件的下载,HttpMessageConverter原理


    HttpMessageConverter<T>
      1) HttpMessageConverter<T> 是 Spring3.0 新添加的一个接口,负责将请求信息转换为一个对象(类型为 T),将对象(类型为 T)输出为响应信息

      2) DispatcherServlet 默认装配 RequestMappingHandlerAdapter ,而 RequestMappingHandlerAdapter 默认装配如下 HttpMessageConverter:

      加入 jackson jar 包后, RequestMappingHandlerAdapter 装配的 HttpMessageConverter  如下:

    使用HttpMessageConverter
      1) 使用 HttpMessageConverter<T> 将请求信息转化并绑定到处理方法的入参中或将响应结果转为对应类型的响应信息,Spring 提供了两种途径:
       使用 @RequestBody / @ResponseBody 对处理方法进行标注
       使用 HttpEntity<T> / ResponseEntity<T> 作为处理方法的入参或返回值
      2) 当控制器处理方法使用到 @RequestBody/@ResponseBody 或 HttpEntity<T>/ResponseEntity<T> 时, Spring 首先根据请求头或响应头的 Accept 属性选择匹配的 HttpMessageConverter,  进而根据参数类型或泛型类型的过滤得到匹配的 HttpMessageConverter, 若找不到可用的 HttpMessageConverter 将报错
      3) @RequestBody 和 @ResponseBody 不需要成对出现
      4) Content-Disposition:attachment; filename=abc.pdf

    实现文件的下载:

    package com.atguigu.test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.UUID;
    
    import javax.servlet.http.HttpSession;
    
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.multipart.MultipartFile;
    
    @Controller
    public class TestUploadAndDownController {
    
        @RequestMapping("/down")
        public ResponseEntity<byte[]> down(HttpSession session) throws IOException{
            
            //获取下载文件的路径,是在服务器端的路径
            String realPath = session.getServletContext().getRealPath("img");//getRealPath("")表示项目路径(服务器的真实项目路径),指定文件夹名称,则表示项目下文件夹的路径
            String finalPath = realPath + File.separator + "2.jpg";  //服务器端资源的位置
            InputStream is = new FileInputStream(finalPath);
            //available():获取输入流所读取的文件的最大字节数
            byte[] b = new byte[is.available()];
            is.read(b);
            //设置请求头。格式固定
            HttpHeaders headers = new  HttpHeaders();
            headers.add("Content-Disposition", "attachment; filename=zzz.jpg"); //attachment表示附件形式,filename表示默认文件名
            //设置响应状态
            HttpStatus statusCode = HttpStatus.OK; //200
            ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(b, headers, statusCode);
            return entity;
        }
    }
  • 相关阅读:
    错误:Char 26: fatal error: reference to non-static member function must be called
    【C++】去除字符串string中的空格(两头空格、所有空格)
    【C/C++】字符串string与字符数组char*的相互转换
    【C++】if-else编程陷阱
    【数据结构与算法】《剑指offer》学习笔记----第一章、第二章 基础知识(含1-15题)
    LeetCode运行报错: reference binding to null pointer of type 'value_type'
    【深度学习机配置】Dell服务站各组件型号记录
    【C++、二分法】LeetCode744. 寻找比目标字母大的最小字母
    Python视频抽帧成图片
    Windows10自带的录屏软件,十分强大
  • 原文地址:https://www.cnblogs.com/lemonzhang/p/12937662.html
Copyright © 2020-2023  润新知