使用 ObjectOutputStream 返回一个对象

发布于 2024-11-07 18:25:16 字数 315 浏览 0 评论 0原文

我发现一段java代码声称使用ObjectOutputStream

     OutputStream outstr = response.getOutputStream();
     ObjectOutputStream oos = new ObjectOutputStream(outstr);

     oos.writeObject(process);

     oos.flush();
     oos.close();

响应返回对象是一个HttpServletResponse对象。我想知道这段代码是如何工作的,以及如何测试它?

I found a segment of java code that is claimed to return object using ObjectOutputStream

     OutputStream outstr = response.getOutputStream();
     ObjectOutputStream oos = new ObjectOutputStream(outstr);

     oos.writeObject(process);

     oos.flush();
     oos.close();

response is a HttpServletResponse object. I would like to know how this segment of code works, and how to test it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽清风入怀 2024-11-14 18:25:16

下面是一个简单的示例,展示了如何通过 HTTP 读取序列化对象。

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.ObjectInputStream;

/**
 * This is a simple example to read an object. 
 *
 * This is not production ready code ;-)
 */
public class Sample {

    public static void main(final String [] pArgs) throws Exception {

        // Change SERVER:PORT/PATH to match your application.
        final URL url = new URL("http://SERVER:PORT/PATH");
        final HttpURLConnection conn = (HttpURLConnection)url.openConnection();

        conn.setRequestMethod("GET");
        conn.setReadTimeout(10000);
        conn.connect();

        final ObjectInputStream is 
        = new ObjectInputStream(conn.getInputStream());

        final Object obj = is.readObject();

        if (obj instanceof String) System.out.println((String)obj);
        else // Convert to object and do whatever.

        is.close();
        conn.disconnect();
    }
}

Below is a simple example that shows how to read a serialized object via HTTP.

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.ObjectInputStream;

/**
 * This is a simple example to read an object. 
 *
 * This is not production ready code ;-)
 */
public class Sample {

    public static void main(final String [] pArgs) throws Exception {

        // Change SERVER:PORT/PATH to match your application.
        final URL url = new URL("http://SERVER:PORT/PATH");
        final HttpURLConnection conn = (HttpURLConnection)url.openConnection();

        conn.setRequestMethod("GET");
        conn.setReadTimeout(10000);
        conn.connect();

        final ObjectInputStream is 
        = new ObjectInputStream(conn.getInputStream());

        final Object obj = is.readObject();

        if (obj instanceof String) System.out.println((String)obj);
        else // Convert to object and do whatever.

        is.close();
        conn.disconnect();
    }
}
鱼窥荷 2024-11-14 18:25:16

它使用Java 的默认二进制序列化协议(我个人不太愿意使用该协议)将对象序列化为servlet 的输出流(例如,将在HTTP 响应中提供的数据)。有关详细信息,请参阅对象序列化规范

至于如何测试它 - 这取决于您想要使用的测试级别。您可以使用伪造的 HTTP servlet 库,获取响应,然后尝试使用 ObjectInputStream 再次读取它,或者您可以运行实际的 servlet 容器,发出 HTTP 请求,然后尝试反序列化回复。

It serializes the object to the servlet's output stream (e.g. the data that would be served in the HTTP response) using Java's default binary serialization protocol (which I'm personally somewhat reluctant to use). For details, see the object serialization spec.

As for how to test it - it depends on the level of testing you want to use. You could use a fake HTTP servlet library, get the response and then try to read it again with an ObjectInputStream, or you could run up the actual servlet container, make an HTTP request and then try to deserialize the response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文