Download images or url images individually or in batches Based on the url address

Source: Internet
Author: User

Download images or url images individually or in batches Based on the url address
During java Development, we may encounter downloading images through URLs. For convenience, I have encapsulated the tool class by downloading images through the url address for later use 1. According to: http://abc.com/hotels/a.jpg url download file (image) // call the following method

download(urlString, filename, savePath+"/"+String.valueOf(imgList.get(i).get("hotelId")));

 

 
/*** Download the image according to the path and save it to the corresponding directory * @ param urlString download source address path author @ param filename file name * @ param savePath save path/jdylog/pic/jdy1_01 * @ return * @ throws Exception */public void download (String urlString, string filename, String savePath) throws Exception {// construct a URL // System. setProperty ("http. proxySet "," true "); // System. setProperty ("http. proxyHost "," 192. 168.2.138 "); // System. setProperty ("http. proxyPort "," 1081 "); URL url = new URL (urlString); // open the connection URLConnection con = url. openConnection (); // set the Request Path con. setConnectTimeout (5*1000); // input stream InputStream is = con. getInputStream (); // 1 k Data Buffer byte [] bs = new byte [1024]; // read Data Length int len; // output File stream File sf = new File (savePath); if (! Sf. exists () {sf. mkdirs ();} OutputStream OS = new FileOutputStream (sf. getPath () + "/" + filename); // start to read while (len = is. read (bs ))! =-1) {OS. write (bs, 0, len) ;}// close all links to OS. close (); is. close ();}

 

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.