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 ();}