Java -- & gt; Use the URL class to download images, java -- url class Images

Source: Internet
Author: User

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...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.