getInputStream与getOutputStream详解以及相互转换

7 篇文章 0 订阅
1 篇文章 0 订阅

客户端上的使用

1.getInputStream方法可以得到一个输入流,客户端的Socket对象上的getInputStream方法得到输入流其实就是从服务器端发回的数据。

2.getOutputStream方法得到的是一个输出流,客户端的Socket对象上的getOutputStream方法得到的输出流其实就是发送给服务器端的数据。

服务器端上的使用

1.getInputStream方法得到的是一个输入流,服务端的Socket对象上的getInputStream方法得到的输入流其实就是从客户端发送给服务器端的数据流。

2.getOutputStream方法得到的是一个输出流,服务端的Socket对象上的getOutputStream方法得到的输出流其实就是发送给客户端的数据。

 

/*inputStream与outputStream的转换*/

package com.boco.test;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;


public class ConvertUtil {
    //inputStream转outputStream
    public ByteArrayOutputStream parse(InputStream in) throws Exception
    {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = in.read()) != -1) {   
            swapStream.write(ch);   
        }
        return swapStream;
    }
    //outputStream转inputStream
    public ByteArrayInputStream parse(OutputStream out) throws Exception
    {
        ByteArrayOutputStream   baos=new   ByteArrayOutputStream();
        baos=(ByteArrayOutputStream) out;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream;
    }
    //inputStream转String
    public String parse_String(InputStream in) throws Exception
    {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = in.read()) != -1) {   
            swapStream.write(ch);   
        }
        return swapStream.toString();
    }
    //OutputStream 转String
    public String parse_String(OutputStream out)throws Exception
    {
        ByteArrayOutputStream   baos=new   ByteArrayOutputStream();
        baos=(ByteArrayOutputStream) out;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream.toString();
    }
    //String转inputStream
    public ByteArrayInputStream parse_inputStream(String in)throws Exception
    {
        ByteArrayInputStream input=new ByteArrayInputStream(in.getBytes());
        return input;
    }
    //String 转outputStream
    public ByteArrayOutputStream parse_outputStream(String in)throws Exception
    {
        return parse(parse_inputStream(in));
    }
}

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
con.getOutputStream()和con.getInputStream()是HttpURLConnection类的两个方法,用于进行HTTP请求和获取服务器响应。 - con.getOutputStream()方法:该方法返回一个OutputStream对象,用于将请求数据写入到服务器。在向服务器发送POST请求时,我们需要使用该方法将POST请求的参数写入到服务器。例如,我们可以使用如下代码将POST请求的参数写入到服务器: ``` HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); OutputStream out = con.getOutputStream(); String postData = "username=test&password=123456"; out.write(postData.getBytes()); out.flush(); out.close(); ``` 在这里,我们首先调用setDoOutput(true)方法开启输出流,然后使用getOutputStream()方法获取输出流。接着,我们将POST请求的参数写入到输出流,这里我们将“username=test&password=123456”写入到服务器。最后,我们需要记得关闭输出流,以便释放相关资源。 - con.getInputStream()方法:该方法返回一个InputStream对象,用于获取服务器响应的输入流。在向服务器发送请求后,我们可以使用该方法获取服务器返回的数据。例如,我们可以使用如下代码获取服务器响应: ``` HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); InputStream in = con.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); in.close(); ``` 在这里,我们首先使用getInputStream()方法获取服务器响应的输入流,然后使用BufferedReader按行读取服务器响应的数据。最后,我们需要记得关闭输入流和BufferedReader,以便释放相关资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值