Java --> use the URL class to download images, java -- url class Images
--> Access the image address through the get request and save the data (that is, the image data) returned by the server to the local file...
--> HttpURLConnectionUtil tool class
Package com. dragon. java. downloadpic; import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; public class HttpURLConnectionUtil {// get the data stream of the reader's response through the get request public static InputStream getInputStreamByGet (String url) {try {HttpURL Connection conn = (HttpURLConnection) new URL (url ). openConnection (); conn. setreadtimeouts (5000); conn. setConnectTimeout (5000); conn. setRequestMethod ("GET"); if (conn. getResponseCode () = HttpURLConnection. HTTP_ OK) {InputStream inputStream = conn. getInputStream (); return inputStream;} catch (IOException e) {e. printStackTrace ();} return null;} // Save the server response data stream to the local file public static void saveData (I NputStream is, File file) {try (BufferedInputStream bis = new BufferedInputStream (is); BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (file ));) {byte [] buffer = new byte [1024]; int len =-1; while (len = bis. read (buffer ))! =-1) {bos. write (buffer, 0, len); bos. flush () ;}} catch (IOException e) {e. printStackTrace ();}}}
--> Test class
Package com. dragon. java. downloadpic; import java. io. file; import java. io. inputStream;/** 1. download the image from the following address and save it in the file. Http://img.coocaa.com/www/attachment/forum/201602/16/085938u86ewu4l8z6flr6w.jpg * requirement: encapsulate the corresponding tool class */public class Test {public static void main (String [] args) {String url = "http://img.coocaa.com/www/attachment/forum/201602/16/085938u86ewu4l8z6flr6w.jpg ";
String [] split = url. split ("\\/");
String fileName = split [split. length-1];
File file = new File ("f:/", fileName );
InputStream inputStream = HttpURLConnectionUtil .getInputStreamByGet(url); HttpURLConnectionUtil.saveData(inputStream, file); }}
--> Simple application of URL class...