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();
}
}
}
我收到了这个错误: 线程“main”java.lang.IndexOutOfBoundsException中的异常 在java.io.ByteArrayOutputStream.write(未知来源)
我希望在写入文件之后将此jpeg文件读入bytearrayoutputstream。
Sory因为我的英语不好..
答案 0 :(得分:2)
试试这个
BufferedImage img = ImageIO.read(new URL("http://images2.layoutsparks.com/1/56178/castle-stone-window-grey.jpg"));
答案 1 :(得分:0)
使用ImageIO
API
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);
答案 2 :(得分: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();
}
答案 3 :(得分:0)
使用javax.imageio.ImageIO而不是java.io包的简单InputStream。
看看这里,我认为它会有所帮助:
http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html