项目中的编码问题:
案例: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("<html><head><title>这是标题</title></head><body>这里是body的内容</body></html>");
- response.getOutputStream().write("<html><head><title>这是标题</title></head><body>这里是body的内容</body></html>".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);
- }*/
- }
- }