I have a text file containing lots of image urls line by line. I need to get a Java code for automatically extracting those images and saving those images into a file. I know how to save the image from a single URL, but how could I modify the code to do multi threading? I want to get all the images under a single folder with its original file name. I tried to google out many codes, but everything was a failure. Please help me to find a solution. Answers will be highly appreciated.
The code I used to save a single image is:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class SaveImageFromUrl {
public static void main(String[] args) throws Exception {
String imageUrl = "http://http://img.emol.com/2015/04/25/nepalterremoto02ok_2260.jpg";
String destinationFile = "/home/abc/image.jpg";
saveImage(imageUrl, destinationFile);
}
public static void saveImage(String imageUrl, String destinationFile) throws IOException {
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
}