Here you can find the source of getFileAndDownload(String urlString, String folder)
private static File getFileAndDownload(String urlString, String folder)
//package com.java2s; //License from project: Apache License import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; public class Main { private static File getFileAndDownload(String urlString, String folder) { try {/*from ww w . ja va 2 s . c o m*/ String[] splittedUrl = urlString.split("/"); String fileName = splittedUrl[splittedUrl.length - 1]; final URL url = new URL(urlString); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // connection.setRequestProperty( // "User-Agent", // "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"); String mimeType = connection.getContentType(); final BufferedImage img = ImageIO.read(connection.getInputStream()); if (img != null) { return getTempFileByName(folder, fileName, img, "jpg"); } else { System.out.println("Incorrect URL of image. Mime type:" + mimeType); } } catch (IOException ex) { System.out.println(ex); } return null; } public static File getTempFileByName(String path, String fileName, BufferedImage img, String imageFormat) throws IOException { ImageIO.write(img, imageFormat, new File(path, fileName)); return new File(path, fileName).isFile() ? new File(path, fileName) : null; } }