Java Script for Batch Uploading Thousands of Images with File Path Retrieval and Request Execution
This article demonstrates a Java solution for automatically collecting absolute file paths, reading image URLs from a text file, and sequentially uploading over 12,000 images to a server, including code for path extraction, request handling, and error logging.
The author describes a practical Java automation script used to batch upload more than 12,000 images stored in multiple sub‑folders to a server, a task encountered during API testing. The workflow first gathers absolute file paths, verifies the count, and orders them to enable resumable uploads.
File‑path collection is implemented with the following code, which recursively traverses directories, skips system files such as DS_Store , and stores each valid path in a list:
List
list = new ArrayList<>();
int aaa = 0;
File file = new File("/Users/Vicky/Desktop/aaa");
output(file.getAbsolutePath());
File[] files = file.listFiles();
int length = files.length;
for (int i = 0; i < length; i++) {
if (files[i].isDirectory()) {
File[] file1 = files[i].listFiles();
output(file1.length);
output(file1[0].getAbsolutePath());
for (int j = 0; j < file1.length; j++) {
String name = file1[j].getAbsolutePath();
// output(name);
if (!name.contains("DS_Store")) {
list.add(name);
aaa++;
}
}
}
}
output(aaa, list.size());
Concurrent.saveRequestTimes(list);The method getAllFile(String path) provides a reusable way to retrieve all file paths under a given directory, ignoring hidden entries:
public static List
getAllFile(String path) {
List
list = new ArrayList<>();
File file = new File(path);
File[] files = file.listFiles();
int length = files.length;
for (int i = 0; i < length; i++) {
File file1 = files[i];
if (file1.isDirectory()) {
List
allFile = getAllFile(file1.getAbsolutePath());
list.addAll(allFile);
continue;
}
String path1 = file1.getAbsolutePath();
if (path1.contains("/.")) continue;
list.add(path1);
}
return list;
}After obtaining the list of image URLs, the script reads them from a text file using readTxtFileByLine and then iterates over each entry, invoking an admin.update(pic) call to perform the actual POST upload. Successful indices are recorded in a secondary list for later analysis:
Admin admin = new Admin();
List
list = readTxtFileByLine("/Users/Vicky/Documents/workspace/fission/log/test.log");
List
list1 = new ArrayList<>();
int size = list.size();
output(size);
try {
for (int i = 0; i < size; i++) {
String pic = list.get(i);
admin.update(pic);
list1.add(i);
output(i + pic);
}
} catch (Exception e) {
output("", e);
}
testOver();The helper method for reading the text file handles UTF‑8 encoding, checks file existence, and returns a list of lines, while logging errors if the file cannot be read:
public static List
readTxtFileByLine(String filePath) {
List
lines = new ArrayList<>();
try {
String encoding = "utf-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader read = new InputStreamReader(fileInputStream, encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
read.close();
fileInputStream.close();
} else {
output("找不到指定的文件");
}
} catch (Exception e) {
output("读取文件内容出错");
e.printStackTrace();
}
return lines;
}The article notes that the actual upload method ( admin.update ) is a simple POST request and that logging of failed uploads is handled elsewhere. For readers interested in a multithreaded version, a link to a separate "multithreaded crawler" implementation is provided.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.