content-type

项目中的编码问题:


案例:content-type作用

[java]  view plain  copy
  1. package com.servlet;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.IOException;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. /** 
  10.  * 案例:content-type作用 
  11.  * @author zhiyong 
  12.  * 
  13.  */  
  14. public class ResponseDemo4 extends HttpServlet {  
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  16.             throws ServletException, IOException {  
  17.           
  18.         /* 
  19.          * 设置响应实体内容的编码 
  20.          */  
  21. //      response.setCharacterEncoding("utf-8");  
  22.           
  23.         /* 
  24.          * 1.服务器发送给浏览器的数据类型 
  25.          */  
  26. //      response.setContentType("text/xml; charset=utf-8");  
  27.           
  28.         response.setContentType("text/html; charset=utf-8");  
  29.           
  30. //      response.getWriter().write("<html><head><title>这是标题</title></head><body>这里是body的内容</body></html>");  
  31.         response.getOutputStream().write("<html><head><title>这是标题</title></head><body>这里是body的内容</body></html>".getBytes("utf-8"));  
  32.           
  33.           
  34.           
  35.         /* 
  36.          * 下载图片 
  37.          */  
  38. /*      File file = new File("f:/cool.png"); 
  39.          
  40.          * 设置头,以下载方式打开文件 
  41.           
  42.         response.setHeader("content-disposition", "attachment; filename=" + file.getName()); 
  43.          
  44.          
  45.          * 发送图片,字节 
  46.           
  47.         response.setContentType("image/png"); 
  48.         FileInputStream in = new FileInputStream(file); 
  49.         byte[] buff = new byte[1024]; 
  50.         int len = 0; 
  51.         //把图片的内容写出到浏览器 
  52.         while((len = in.read(buff)) != -1){ 
  53.             response.getOutputStream().write(buff, 0, len); 
  54.         }*/  
  55.     }  
  56. }  
Content-Type是HTTP协议中的一个头部字段,用于指示发送方或接收方对数据的媒体类型。在HTTP请求中,Content-Type用于指示请求体中的数据类型;在HTTP响应中,Content-Type用于指示响应体中的数据类型。常见的Content-Type有application/x-www-form-urlencoded、multipart/form-data、application/json、application/xml和text/xml等。 Content-Type的格式为type/subtype;parameter,其中type表示主类型,可以是任意字符串,如text,如果是*号代表所有;subtype表示子类型,也是任意字符串,如html,如果是*号代表所有,用“/”与主类型隔开;parameter是可选参数,如charset、boundary等。 例如,Content-Type可以是text/html、application/json;charset:utf-8等形式。在请求中,Content-Type可以指定请求体中数据的格式,而在响应中,Content-Type可以告知接收方响应体中数据的格式。通过Content-Type的设置,发送方和接收方可以正确解析和处理数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Content-Type 详解](https://blog.youkuaiyun.com/qq_14869093/article/details/86307084)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值