1
public class Socket {
    public Socket() {
        try {
            URL url = new URL("http://images2.layoutsparks.com/1/56178/castle-stone-window-grey.jpg");
            InputStream in = url.openStream();
            BufferedInputStream bin = new BufferedInputStream(in); 


            ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);
            byte[] okunan = new byte[4096];

            while(in.read()!= -1){
                bout.write(okunan, 0, in.read());

            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我收到此错误:java.io.ByteArrayOutputStream.write 处的线程“main”java.lang.IndexOutOfBoundsException 中的异常(未知来源)

我想在写入文件后将此 jpeg 文件读取到 bytearrayoutputstream 中。

对不起我的英语不好..

4

4 回答 4

2

试试这个

BufferedImage img = ImageIO.read(new URL("http://images2.layoutsparks.com/1/56178/castle-stone-window-grey.jpg"));
于 2013-03-12T10:31:18.277 回答
0

使用ImageIOAPI

BufferedImage img = ImageIO.read(new URL("http://images2.layoutsparks.com/1/56178/castle-stone-window-grey.jpg"));

ByteArrayOutputStream bout = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", bout);

检查以获取信息

于 2013-03-12T10:33:38.297 回答
0

你的意思是像

try {
        URL url = new URL("http://images2.layoutsparks.com/1/56178/castle-stone-window-grey.jpg");
        InputStream in = url.openStream();
        BufferedInputStream bin = new BufferedInputStream(in); 


        ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);
        byte[] okunan = new byte[4096];

        while(in.read()!= -1){
            in.read(okunan);
            bout.write(okunan);

        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
于 2013-03-12T10:35:49.743 回答
0

使用 javax.imageio.ImageIO 而不是 java.io 包的简单 InputStream。

看看这里,我认为它会有所帮助:

http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html

于 2013-03-12T12:54:43.080 回答