how Mark used simple scripts to download the entire catalog of student directory posts. I will show you how to do the same in Java.
Requirements:
- A text editor (Notepad or Notepad++ will do)
- A Mac with Terminal
- Basic Java knowledge
Procedure
How this program will work is it will basically be for downloading a large succession of images from a database. The database will often have a template such as www.example.com/folderone/foldertwo/folderthree/J8031736345854221.JPG, with the next picture being J8031736345854222.JPG. The program will increment the last digit by 1 through a specified range.
Step 1:
First, in the Java editor, we add the basic configuration code to a file you created titled "Main.java":
import java.lang.Runtime;
import java.io.IOException;
import javax.swing.JOptionPane;
import java.lang.Integer;
import java.lang.String;
public class Main {
public static void main(String[] args) {
}
}
You should probably be able to tell the classes we will be using.
Step 2:
Let's say we want www.example.com/folderone/foldertwo/folderthree/J8031736345854221.JPG through www.example.com/folderone/foldertwo/folderthree/J8031736345854353.JPG
Now, we display three dialogue boxes asking for a string. Basically, the first box will take in the left unchanging part of the URL (www.example.com/folderone/foldertwo/folderthree/J8031736345854, for example), the second asking for the range of the variable part (221-353), and the last asking for the unchanging ending (.JPG).
String firstURL = JOptionPane
.showInputDialog(null,
"Enter the first part of the static URL",
"http://www.example.com/firstFolder/SecondFolder/firstpartoffiles");
String secondURL = JOptionPane.showInputDialog(null,
"Enter the variable part of the URL in a range", "x-y");
String thirdURL = JOptionPane.showInputDialog(null,
"Enter the first part of the static URL", ".jpg");
Step 3:
Now, we need to split the range into an array of values at the dash
String[] range = secondURL.split("-");
Step 4:
Finally, we need to add a loop downloading the image using the Unix wget command, then update the changing part, then download it again (we also add some logs to the terminal for fun):
for (int i = Integer.parseInt(range[0]); i <= Integer.parseInt(range[1]); i++) {
try {
System.out.println("wget " + firstURL + i + thirdURL);
Process p = r.exec("wget "+ firstURL + i + thirdURL);
} catch (IOException e) {
e.printStackTrace();
}
Reconciliation:
If you were following along so far, you're code should look like this:
import java.lang.Runtime;
import java.io.IOException;
import javax.swing.JOptionPane;
import java.lang.Integer;
import java.lang.String;
public class Main {
public static void main(String[] args) {
String firstURL = JOptionPane
.showInputDialog(null,
"Enter the first part of the static URL",
"http://www.example.com/firstFolder/SecondFolder/firstpartoffiles");
String secondURL = JOptionPane.showInputDialog(null,
"Enter the variable part of the URL in a range", "x-y");
String thirdURL = JOptionPane.showInputDialog(null,
"Enter the first part of the static URL", ".jpg");
String[] range = secondURL.split("-");
Runtime r = Runtime.getRuntime();
for (int i = Integer.parseInt(range[0]); i <= Integer.parseInt(range[1]); i++) {
try {
System.out.println("wget " + firstURL + i + thirdURL);
Process p = r.exec("wget "+ firstURL + i + thirdURL);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Running the application
Move the Java file (Main.java) into the folder you wish to download the images into.
Now open Terminal and type to navigate to the file location:
cd (then the directory you placed Main.java into)
Now, compile the program by typing:
javac Main.java
Finally, run the program by typing:
java Main
Now, just enter in the information into the dialogue boxes, in the range one separating your values by a hyphen. Once in process, the System.out.println statement kicks in, printing the web addresses of each downloaded image so you can see that everything is working all right.
Please subscribe for more tips.