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

高洛峰
Release: 2017-01-11 13:58:20
Original
2237 people have browsed it

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();
    }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template