java outputstream如何转化成byte[]

java outputstream如何转化成byte[]

InputStream重用技巧(利用ByteArrayOutputStream)

InputStream is = null;
    ByteArrayOutputStream bout = new ByteArrayOutputStream(); 

    String picUrl = imgData;
    picUrl = new String(picUrl.getBytes("ISO8859-1"), "utf-8");
    URL url = new URL(enCodeLinuxUri(picUrl));
    URLConnection con = url.openConnection();
    con.setConnectTimeout(5 * 1000);
    is = con.getInputStream();

    int len;
    byte[] bs = new byte[1024];
    while ((len = is.read(bs)) != -1) {
        bout.write(bs, 0, len);
    }

    picData = bout.toByteArray();
    is.close();
    bout.flush();

你可能感兴趣的:(Java学习笔记)