HttpServletResponse基础知识

一、通过字符流输出

     方式1(建议)

response.setCharacterEncoding("UTF-8");//设置response码表,控制response向浏览器写数据

response.setHeader("Content-type","text/html;charset=UTF-8");//控制浏览器打开的码表

PrintWriter out = response.getWriter();//得到输出字符流

out.print("中国");//输出字符

     方式2(建议)

response.setContentType("text/html;charset=UTF-8");//直接控制全程码表

PrintWriter out = response.getWriter();//得到输出字符流

out.print("中国");//输出字符

     方式3

//使用标签模拟控制码表

response.setCharacterEncoding("UTF-8");//设置response码表,控制response向浏览器写数据

PrintWriter out = response.getWriter();//得到输出字符流

out.write("");

out.print("中国");//输出字符

 

二、通过字节流输出字符串

     方式1(不建议使用)

//不控制码表,直接输出

OutPutStream out = response.getOutputStream();获取字节流

out.write("中国".getBytes());//输出字节流,默认gb2312

     方式2(建议)

response.setHeader("Content-type","text/html;charset=UTF-8");//控制浏览器打开的码表

OutputStream out = response.getOutputStream();获取字节流

out.write("中国".getBytes("UTF-8"));//输出字节流,转换为UTF-8

     方式3

//使用标签模拟控制码表

OutPutStream out = response.getOutputStream();//获取字节流

out.write("".getBytes());

out.write("中国".getBytes("UTF-8"));//输出字节流,转换为UTF-8

三、输出图片

BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);//创建图片

OutputStream outputStream = response.getOutputStream();//获取输出字节流

ImageIO.write(image, "jpg", outputStream);//输出图片

四、文件下载


import java.io.FileInputStream;

import java.io.IOException;

import java.net.URLEncoder;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



@WebServlet("/ResponseDemo7")

public class Test_Image extends HttpServlet {

    private static final long serialVersionUID = 1L;



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //通过路径将图片以流的形式得到

        //通过相对路径得到绝对路径

        String path = this.getServletContext().getRealPath("Images/a1.jpg");

        //有了路径可以得到输入流(图片,要用字节输出流)

        FileInputStream fis = new FileInputStream(path); //使用绝对路径将图片用输入流变成一个流  fis(图片)

        //用response创建一个输出流

        ServletOutputStream outputStream = response.getOutputStream();



        /*我们要将图片下载下来 */

        //得到要下载的文件名

        String fileName = path.substring(path.lastIndexOf("\\")+1);

        System.out.println(fileName);

        //设置文件的编码

        fileName = URLEncoder.encode(fileName,"UTF-8");

        //告知客户端要下载文件

        response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));//编码

        response.setHeader("content-type", "image/jpeg");



        //执行fileOutputStream的输出操作

        int len = 1;

        byte[] b = new byte[1024];

        while((len=fis.read(b))!=-1){

            outputStream.write(b, 0, len);

        }

        outputStream.close();

        fis.close();

    }



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // TODO Auto-generated method stub

        doGet(request, response);

    }



}

(进入博客查看HttpServletRequest基础知识)

 

你可能感兴趣的:(J2EE)