• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

http的Content-Type

武飞扬头像
大佬腿好粗
帮助4

介绍

  • MediaType,即是Internet Media Type,互联网媒体类型;也叫做MIME类型
  • 在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。
  • response.setContentType(MIME)的作用是使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。

媒体格式类型

Content-Type 媒体格式
text/html HTML,HTM格式
text/plain 纯文本格式
text/xml XML格式
image/gif gif图片格式
image/jpeg jpg,jpeg,jpe图片格式
image/png png图片格式
application/xhtml xml XHTML格式
application/xml XML数据格式
application/atom xml Atom XML聚合格式
application/json JSON数据格式,由于json规范的流行,各大浏览器都开始原生支持JSON.stringfy。而且spring对这个content-Type上传的数据有很好的支持,可以直接通过@RequestBody进行接收。也是当前完美适配当前流行的RestApi。
application/pdf pdf格式
application/msword Word文档格式
application/vnd.ms-excel excel格式
application/vnd.ms-powerpoint ppt格式
application/octet-stream 二进制流数据(如常见的文件下载,不知道下载文件类型)
application/x-www-form-urlencoded 中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)
multipart/form-data 需要在表单中进行文件上传时,就需要使用该格式
application/binary 二进制

@RequestMapping关于Content-Type的使用

  1. consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;consumes="application/json"表示方法仅处理Content-Type为“application/json”类型的请求
  2. produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回,produces="application/json"表示处理request请求中Accept头中包含了"application/json"的请求,也就是暗示返回的文件类型为application/json
  3. headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求 headers = "Accept=application/json"表示方法仅处理请求头中有Accept=application/json的请求
  4. 基于headers, consumes,produces对请求和响应中的媒体内容格式做控制和过滤

HttpServletResponse关于Content-Type的使用

  1. response.setContentType的意思就是高速客户端在解析这个返回包的时候用的是哪种解析方式
  2. 客户端根据多媒体类型,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据
  3. 下载或预览文件,常和报文头content-disposition配合使用,对报文体进行描述,规定了接收端的显示处理行为,此报文头的值有两种, attachmentinline, 分别表示保存 还是 直接显示。

下载excel文件

private void setWorkbookResponse(Workbook workbook, HttpServletResponse response) throws Exception {
    if (workbook != null) {
        ServletOutputStream out = null;

        try {
            String fileName = "Excel-"   String.valueOf(System.currentTimeMillis()).substring(4, 13)   ".xls";
            String headStr = "attachment; filename=\""   fileName   "\"";
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", headStr);
            out = response.getOutputStream();
            workbook.write(out);
            out.flush();
        } catch (IOException var16) {
        } finally {
            if (null != out) {
                out.close();
            }
        }
    }
}
学新通

预览html文件

private void setWorkbookResponseToHTML(Workbook workbook, HttpServletResponse response) throws Exception {
    if (workbook != null) {
        ServletOutputStream out = null;
        //03 版本EXCEL预览  .xls   有图片
        ExcelToHtmlParams params = new ExcelToHtmlParams(workbook,true,"yes");

        try {
            String fileName = "Excel-"   String.valueOf(System.currentTimeMillis()).substring(4, 13)   ".html";
            //String headStr = "attachment; filename=\""   fileName   "\"";
            String headStr = "inline; filename=\""   fileName   "\"";
            response.setContentType("text/html");
            response.setHeader("Content-Disposition", headStr);
            out = response.getOutputStream();
            out.write(ExcelXorHtmlUtil.excelToHtml(params).getBytes());
            out.flush();
        } catch (IOException var16) {
        } finally {
            if (null != out) {
                out.close();
            }
        }
    }
}
学新通

二进制格式

private void setWorkbookResponseWithBinary(Workbook workbook, HttpServletResponse response) {
    if (workbook != null) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            ServletOutputStream out = null;
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            workbook.write(os);
            byte[] content = os.toByteArray();
            InputStream is = new ByteArrayInputStream(content);
            // 设置response参数,可以打开下载页面
            response.reset();
            response.setContentType("application/binary;charset=UTF-8");
            String fileName = "Excel-"   String.valueOf(System.currentTimeMillis()).substring(4, 13)   ".xls";
            response.setHeader("Content-Disposition", "attachment;filename="   URLEncoder.encode(fileName, "UTF-8"));
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Credentials", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "*");

            out = response.getOutputStream();
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);

            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        }catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
            }
        }
    }
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhffffkf
系列文章
更多 icon
同类精品
更多 icon
继续加载