webx笔记-ServletResponse

直接通过HttpServletResponse对浏览器返回Response

这里的技术点在于理解HttpServletResponse,跟webx关系不大

1.输出文本

public class SayHi {
    @Autowired
    private HttpServletResponse response;

    public void execute() throws Exception {
        // 设置content type,但不需要设置charset。框架会设置正确的charset。
        response.setContentType("text/plain");
        // 如同servlet一样:取得输出流。
        PrintWriter out = response.getWriter();
        out.println("Hi there, how are you doing today?");
    }
}

2.输出图片

public class SayHiImage {
    @Autowired
    private HttpServletResponse response;

    @Autowired
    private BufferedRequestContext brc;

    public void execute() throws Exception {
        // 为了节省内存,关闭buffering。
        brc.setBuffering(false);
        // 只要设置了正确的content type,你就可以输出任何文本或二进制的内容:
        // HTML、JSON、JavaScript、JPG、PDF、EXCEL等。
        response.setContentType("image/jpeg");
        OutputStream out = response.getOutputStream();
        writeImage(out);
    }

    private void writeImage(OutputStream out) throws IOException {
        BufferedImage img = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
        //...
        ImageIO.write(img, "jpg", out);
    }
}

3.输出html

public class Count {
    @Autowired
    private HttpServletResponse response;

    @Autowired
    private BufferedRequestContext brc;

    public void execute(@Param("to") int toNumber) throws Exception {
        // 必须关闭buffering,未完成的页面才会被显示在浏览器上。
        brc.setBuffering(false);
        // 设置content type,但不需要设置charset,框架会设置正确的charset。
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("");
        out.println("");
        out.println("  Count to " + toNumber + "");
        out.println("");
        out.println("");
        for (int i = 1; i <= toNumber; i++) {
            for (int j = 0; j < 10000; j++) {
                out.print(i);
            }
            out.println();
            out.flush(); // 将当前的结果立即显示到浏览器上
            Thread.sleep(1000); // 特意等待1秒,仅用于演示。
        }

        out.println("");
        out.println("");
    }
}

4. Download

public class Download {
    @Autowired
    private HttpServletResponse response;

    @Autowired
    private BufferedRequestContext brc;

    public void execute(@Param("filename") String filename) throws Exception {
        // 为了增强用户体验,关闭buffering,让下载立即开始,而不是等待整个文件生成完才通知用户下载。
        brc.setBuffering(false);
        // 设置headers,下载文件名必须避免非us-ascii字符,因为各浏览器的兼容程度不同。
        filename = defaultIfNull(trimToNull(filename), "image") + ".txt";
        filename = "\"" + escapeURL(filename) + "\"";

        response.setHeader("Content-disposition", "attachment; filename=" + filename);

        // 只要设置了正确的content type,你就可以让用户下载任何文本或二进制的内容:
        // HTML、JSON、JavaScript、JPG、PDF、EXCEL等。
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        for (int i = 0; i < 100; i++) {
            out.flush(); // 立即提示用户下载
            for (int j = 0; j < 100000; j++) {
                out.print(i);
            }
            out.println();
            Thread.sleep(100); // 故意延迟,以减缓下载速度
        }
    }
}

你可能感兴趣的:(webx笔记-ServletResponse)