返回

content-type

发布时间:2022-10-17 18:06:56 276
# html# java# java# 服务器# 服务器

项目中的编码问题:

content-type_html

案例:content-type作用

package com.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 案例:content-type作用
* @author zhiyong
*
*/
public class ResponseDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

/*
* 设置响应实体内容的编码
*/
// response.setCharacterEncoding("utf-8");

/*
* 1.服务器发送给浏览器的数据类型
*/
// response.setContentType("text/xml; charset=utf-8");

response.setContentType("text/html; charset=utf-8");

// response.getWriter().write("这里是body的内容");
response.getOutputStream().write("这里是body的内容".getBytes("utf-8"));



/*
* 下载图片
*/
/* File file = new File("f:/cool.png");

* 设置头,以下载方式打开文件

response.setHeader("content-disposition", "attachment; filename=" + file.getName());


* 发送图片,字节

response.setContentType("image/png");
FileInputStream in = new FileInputStream(file);
byte[] buff = new byte[1024];
int len = 0;
//把图片的内容写出到浏览器
while((len = in.read(buff)) != -1){
response.getOutputStream().write(buff, 0, len);
}*/
}
}

 

 

 

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
Servlet的多线程并发问题 2022-10-17 17:55:59