InputStream in= getUrlSource(map);
int read;
ByteArrayOutputStream byteArray= new ByteArrayOutputStream();
while ((read = in.read()) != -1)
{
    byteArray.write(read);
    byteArray.flush();
}
byte[] bt = byteArray.toByteArray();

解决方案 »

  1.   

    楼上的一个字节读也不好,设定一定的缓存就可以了
    比如 1k大小 
    InputStream in= getUrlSource(map);
    byte buffer=new byte[1024];
    int read;
    ByteArrayOutputStream byteArray= new ByteArrayOutputStream();
    while ((read = in.read(buffer))>0)
    {
        byteArray.write(buffer,0,read);
        byteArray.flush();
    }
    byte[] bt = byteArray.toByteArray();
      

  2.   

    使用ByteArrayOutputStream 的好处就是缓存大小是自动增加的
      

  3.   

    byte buffer=new byte[1024];
    read = in.read(buffer)
    如果我读进来的buffer没有达到1024b,那我程序一直在等到有1024b才结果,请问有没有什么方法可以解决这个问题?
      

  4.   

    read
    public abstract int read()
                      throws IOException
    Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. 
    A subclass must provide an implementation of this method. 
    Returns:
    the next byte of data, or -1 if the end of the stream is reached.
    应该不会出现你说的问题呀
      

  5.   

    阻塞是正常的,你用read()也会阻塞呀!