Home >Java >javaTutorial >java get data from input stream and return byte array example

java get data from input stream and return byte array example

高洛峰
高洛峰Original
2017-01-11 13:58:202212browse

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
//从输入流中获取数据并以字节数组返回
public class StreamTool {
    /**
     * 从输入流获取数据
     * @param inputStream
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inputStream) throws Exception{
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1){
            outputStream.write(buffer, 0, len);
        }
        outputStream.close();
        inputStream.close();
        return outputStream.toByteArray();
    }
}

For more Java-related articles on obtaining data from the input stream and returning a byte array example, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn