i using the code below to get the image at the url:
URL url=new URL("http://www.google.com/images/logos/ps_logo2.png");
InputStream in=url.openStream();
ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
byte[] buf = new byte[512];
int len;
while (true) {
len = in.read(buf);
if (len == -1) {
break;
}
tmpOut.write(buf, 0, len);
}
tmpOut.close();
byte[] picture=tmpOut.toByteArray();
System.out.println(picture.length);
this code is ok, but my internet connection is very bad,
So maybe I get a broken picture:

How can I guarantee that the image file is complete?
I think you can add this code to try and verify this:
if (len == -1) { change to if (len == -1 || (int)(Math.random()*100)==1 ) {
full test code:
URL url=new URL("http://www.google.com/images/logos/ps_logo2.png");
InputStream in=url.openStream();
ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
byte[] buf = new byte[512];
int len;
while (true) {
len = in.read(buf);
if (len == -1 || (int)(Math.random()*100)==1 ) {
break;
}
tmpOut.write(buf, 0, len);
}
tmpOut.close();
byte[] picture =tmpOut.toByteArray();
System.out.println(picture.length);
thanks for the help:)
source
share