Java downloads the image file from the remote http address to the local device-automatically processes whether the image is compressed by gzip on the server.
Directly run the Code:
/**
* Download an object to a local device.
*
* @ Param urlString
* Address of the downloaded file
* @ Param filename
* Local file name
* @ Throws Exception
* Various exceptions
*/
Public static void download (String urlString, String filename)
Throws Exception {
// Construct a URL
URL url = new URL (urlString );
// Open the connection
URLConnection con = url. openConnection ();
// Input stream
InputStream is = con. getInputStream ();
String code = con. getHeaderField ("Content-Encoding ");
System. out. println ("cdoe:" + code );
If (null! = Code) & code. equals ("gzip "))
{
GZIPInputStream gis = new GZIPInputStream (is );
// 1 k Data Buffer
Byte [] bs = new byte [1024];
// Read Data Length
Int len;
// Output file stream
OutputStream OS = new FileOutputStream (filename );
// Start reading
While (len = gis. read (bs ))! =-1 ){
OS. write (bs, 0, len );
}
// Close all links
Gis. close ();
OS. close ();
Is. close ();
}
Else
{
// 1 k Data Buffer
Byte [] bs = new byte [1024];
// Read Data Length
Int len;
// Output file stream
OutputStream OS = new FileOutputStream (filename );
// Start reading
While (len = is. read (bs ))! =-1 ){
OS. write (bs, 0, len );
}
// Close all links
OS. close ();
Is. close ();
}
}