response write data to the client

1, write the text:

    protected  void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { 
        response.setCharacterEncoding ( " UTF-. 8 " ); // encoding settings server, the default-8859-1 is the ISO 
        the response.setContentType ( " text / HTML; charset . 8-UTF = " ); // tell the browser encoding format server 
        response.getWriter () Write (. " Hello, the JAVA " ); 
    }

 

 2. Pictures:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletOutputStream out=response.getOutputStream();
        File file=new File("图片路径");
        FileInputStream fi = new FileInputStream(file);
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = fi.read(buf)) != -1) {
            out.write(buf, 0 , len); // write data length specified in the array to the output stream. 
        } 
        Fi.close (); 
        OUT .close (); 
    }

Using a buffer array reading image data, reducing the consumption of time. ServletOutputStream create an object, and outputs the data to the client.

 

Guess you like

Origin www.cnblogs.com/zhai1997/p/11484889.html