int bfr = 128;
try {
URLConnection uc = u.openConnection();
String ct = uc.getContentType();
int cl = uc.getContentLength();
if (ct.startsWith("text/") || cl == -1 ) {
System.err.println("This is not a binary file.");
return;
}
InputStream theImage = uc.getInputStream();
byte[] b = new byte[cl];
int bytesread = 0;
int offset = 0;
while (bytesread >= 0) {
bytesread = theImage.read(b, offset, bfr);
if (bytesread == -1) break;
offset += bytesread;
}
if (offset != cl) {
System.err.println("Error: Only read " + offset + " bytes");
System.err.println("Expected " + cl + " bytes");
}
String theFile = u.getFile();
theFile = theFile.substring(theFile.lastIndexOf('/") + 1);
FileOutputStream fout = new FileOutputStream(theFile);
fout.write(b);
} // end try
catch (Exception e) {
System.err.println(e);
}
return;