Java:常用的将InputStream转化为String的方法

目录

一、Google Guava IO

1、使用com.google.common.io.ByteSource

2、使用com.google.common.io.CharStreams

二、Apache Commons IO

1、使用org.apache.commons.io.IOUtils.copy()

2、使用org.apache.commons.io.IOUtils.toString()

三、JDK

1、使用java.util.Scanner

2、使用java.io.ByteArrayOutputStream

3、使用java.io.InputStream.readAllBytes() (since JDK9)


由于将InputStream转化为String使用很频繁,所以将常用的几种方式列举如下:

一、Google Guava IO

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.2-jre</version>
</dependency>

1、使用com.google.common.io.ByteSource

// com.google.common.io.ByteSource
public static String toString(InputStream inputStream) throws IOException {

    ByteSource byteSource = new ByteSource() {

        public InputStream openStream() throws IOException {
            return inputStream;
        }
    };

    return byteSource.asCharSource(StandardCharsets.UTF_8).read();
}

2、使用com.google.common.io.CharStreams

// com.google.common.io.CharStreams
public static String toString(InputStream inputStream) throws IOException {

    String text = null;

    try (final Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
        text = CharStreams.toString(reader);
    }

    return text;
}


二、Apache Commons IO

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

1、使用org.apache.commons.io.IOUtils.copy()

public static String toString(InputStream inputStream) throws IOException {

    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8);
    return writer.toString();
}

2、使用org.apache.commons.io.IOUtils.toString()

public static String toString(InputStream inputStream) throws IOException {

    return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}

三、JDK

1、使用java.util.Scanner

public static String toString(InputStream inputStream) throws IOException {

    // \A The beginning of the input
    // \Z The end of the input but for the final terminator, if any
    // \z The end of the input
    try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name());) {
        scanner.useDelimiter("\\A");
        return scanner.hasNext() ? scanner.next() : "";
    }
}

2、使用java.io.ByteArrayOutputStream

public static String toString(InputStream inputStream) throws IOException {

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    byte[] buf = new byte[256];
    int len = 0;
    while ((len = inputStream.read(buf)) != -1) {
        bout.write(buf, 0, len);
    }

    return bout.toString(StandardCharsets.UTF_8.name());
}

3、使用java.io.InputStream.readAllBytes()(since JDK9)

public static String toString(InputStream inputStream) throws IOException {
        // since JDK9
	 return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}

参考文档

Convert InputStream to String In Java

How to convert InputStream to String in Java

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值