📜  Servlet – 输出流类

📅  最后修改于: 2022-05-13 01:54:44.274000             🧑  作者: Mango

Servlet – 输出流类

ServletOutputStream类是Java包javax.servlet的一个组件,是一个抽象类,它提供输出流以将二进制数据发送到客户端。 ServletOutputStream 继承了 OutputStream,它是表示字节输出流的所有类的超类。 ServletOutputStream 类的子类必须实现Java.io.OutputStream.write(int) 方法。

签名

public abstract class ServletOutputStream extends OutputStream 

构造函数

ServletOutputStream() :由于 ServletOutputStream 是一个抽象类,因此它不能被初始化。

方法

S.No

Method                   

Description                                          

Return Type                   

1.print(boolean b)    print(boolean b) method is used to write a boolean value to the client with no CRLF character at the end.void
2.print(char c)print(char c) method is used to write a character to the client with no CRLF character at the end.void
3.print(double d)print(double d) method is used to write a double value to the client with no CRLF character at the end.void
4.print(float f)print(float f) method is used to write a float value to the client with no CRLF character at the end.void
5.print(int i)print(int i) method is used to write an int value to the client with no CRLF character at the end.void
6.print(long l)print(long l) method is used to write a long value to the client with no CRLF character at the end.void
7.print(String s)print(String s) method is used to write a String to the client with no CRLF character at the end.void
8.println()println() method is used to write a CRLF character at the end.void
9.println(boolean b)println(boolean b) method is used to write a boolean value to the client, followed by a CRLF at the end.void
10.println(char c)println(char c) method is used to write a character to the client, followed by a CRLF at the end.void
11.println(double d)println(double d) method is used to write a double value to the client, followed by a CRLF at the end.void
12.println(float f)println(float f) method is used to write a float value to the client, followed by a CRLF at the end.void
13.println(int i)println(int i) method is used to write an int value to the client, followed by a CRLF at the end.void
14.println(long l)println(long l) method is used to write a long value to the client, followed by a CRLF at the end.void
15.println(String s)println(String s) method is used to write a String to the client, followed by a CRLF at the end.void

ServletOutputStream 的抽象方法

S.No

Method                                                             

Description                                                           

Return Type     

1.setWriteListener(WriteListener writeListener)

setWriteListener(WriteListener writeListener) method is used  

to instructs the ServletOutputStream to invoke the provided

WriteListener when it is possible to write.

abstract void
2.isReady()isReady() method is used to determine if data can be written without blocking.abstract boolean

ServletOutputStream 实现的接口

  1. Java.io.Closeable。
  2. Java.io.Flushable。
  3. Java.lang.AutoCloseable。

Java程序创建一个 servlet,然后发送一个图像作为响应——

Java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GeeksForGeeks extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException
    {
        try {
            // set response content type
            response.setContentType("image/jpg");
            // get the reference of servletOutputStream
            ServletOutputStream servletOutputStream;
            servletOutputStream
                = response.getOutputStream();
            BufferedInputStream bufferedInputStream
                = new BufferedInputStream(
                    new FileInputStream(
                        "c:/gfg/geeks.jpg"));
            BufferedOutputStream bufferedOutputStream
                = new BufferedOutputStream(
                    servletOutputStream);
            // read from buffer and 
            // then write that data to
            // bufferedOutputStream
            int tmp;
            while (1) {
                tmp = bufferedInputStream.read();
                if (tmp == -1)
                    break;
                bufferedOutputStream.write(tmp);
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
    {
        doGet();
    }
}