“This is the 17th day of my participation in the First Challenge 2022.

Hello, I’m looking at the mountains.

The invention of tools can save physical strength and reduce repetitive work. Software is also a kind of tool. Today’s lesson is to use IT to reduce the repetitive effort of renaming massive files.

Has been using the storage cloud disk is Baidu network disk, which collected a large number of files. The usage space of various materials and e-books has reached 2500G. Before also cleaned up some low-quality books, the results of the tool export found that in the catalog to be sorted, there are actually 1,942 records of e-books. If there is a small partner want what book, you can leave a message from the public number, as long as it is not commercial, selfless sharing.

Put forward the demand

Back to the book, so many documents, named format strange, because some of the information is saved from others to share, some will also take the url, visible website operation is also extremely. Find some batch rename tools from the Internet, mostly Windows version, and is added prefix or suffix and so on, not applicable.

What I want is to specify the name of the file myself and execute it in batches. Equivalent to a pair of hands, help me in Baidu network disk to provide the renaming of the executive. The reason not the official renaming, because it is too difficult to use, and a waste of time (behind will be specific about baidu network disk design, can also be used for reference).

There are two steps to implementing your idea:

  1. Export all file names in Excel format
  2. In the exported Excel file, define the target name. If the target name does not need to be changed, do not change it
  3. Import the file and check whether the file is renamed successfully

Design scheme

According to the requirements, we will design the scheme.

Find the key interface

First, we need to be able to export all the file names.

Baidu network disk provides web version, client version, in order to save time and trouble, we use web version check logic. Open the console and find a/API /list request when entering the directory, as shown below:

Based on the response, we can see that this interface can get a list of files for the specified directory. This request is a Get request, which contains several parameters.

Typically, simple web requests are authenticated by cookies, so we don’t think about using cookies.

Next need to find the renaming request, similarly, the implementation of baidu web disk renaming can be added to the request. The diagram below:

As you can see, there are two steps to the renaming:

  1. Submit the rename task and return the task ID
  2. The task ID is used to query the task execution

This is the reference point mentioned above. For Baidu network disk this application, although download speed limit is all kinds of criticism, and Ali cloud disk strong pursuit, but have to say, Baidu network disk or now use more cloud storage tools. Optimizations must be targeted to asynchronously task-ify certain secondary functions, such as renaming.

  1. A request is made to create a rename task. The task ID is returned after the task is successfully created. At this time, the baidu web disk back-end service listens for new tasks. If the back-end pressure is high, the tasks can be executed slowly or not.

  2. Since the task is asynchronous, clients (including web pages or clients) need to check the task execution status. Task execution can be in progress or completed, or rejected, failed, or expired according to the contract.

Export and import files

We can export Excel files with ali open source EasyExcel (specific operation, you can view the write file, write good, fill the file three).

At this time, we need to define the content of the exported file. According to the rename request, we can know that we need the file path and the new name of the file. For simple operation, we can directly export the original name. In order to check whether the web disk file is repeated, it is best to export the summary code of the file.

Now that our requirements and solutions are designed, it’s time to start coding.

Start coding

To start coding, we need to define the authentication parameters: cookie, bdstoken, and an extension parameter path. We only export the list of files in the specified directory.

Defining base classes

As defined in the design, we create the base class for the export file:

@Data
public class FileName {
    @ ExcelProperty (" path ")
    private String path;
    @ExcelProperty("MD5")
    private String md5;
    @ExcelProperty(" original name ")
    private String originName;
    @ExcelProperty(" New name ")
    private String newName;
}
Copy the code

As it relates to network requests, we need to define request parameters. Requests have some common parameters:

@Data
public abstract class BaseRequest {
    protected String channel = "chunlei";
    protected String web = "1";
    protected String appId = "250528";
    protected String bdstoken = "";
    protected String logid = "";
    protected String clienttype = "0";
}
Copy the code

The parameters of the file list are:

@EqualsAndHashCode(callSuper = true)
@Data
public class FileListRequest extends BaseRequest {
    private String order = "name";
    private String desc = "0";
    private String showempty = "0";
    private int page = 1;
    private int num = 100;
    private String dir = "/";
    private String t = "";
}
Copy the code

The rename parameter is:

@EqualsAndHashCode(callSuper = true)
@Data
public class FileRenameRequest extends BaseRequest {
    private String opera = "rename";
    private String async = "2";
    private String onnest = "fail";
}
Copy the code

We also need a parameter to query the task status:

@EqualsAndHashCode(callSuper = true)
@Data
public class TaskStatusRequest extends BaseRequest {
    private Long taskid;
}
Copy the code

Defining the request class

Now that we have the base class and the request class, let’s define the request interface. These classes are the templated methods. Let’s look at them briefly. If you want to get the source code, follow the public number “see mountain hut” reply “Java” to get the source code.

Define the file list request method first:

private List<FileListItem> listFileCurrentPath(FileListRequest fileListRequest) {
    final String body = HttpRequest.get("https://pan.baidu.com/api/list")
            .form(fileListRequest.paramMap())
            .header(this.headers)
            .cookie(this.cookie)
            .execute()
            .body();
    final FileListResponse response = JSONUtil.toBean(body, FileListResponse.class);
    if (response.getErrno() == 0) {
        return response.getList();
    }
    return Collections.emptyList();
}
Copy the code

In defining the file rename request method:

private Long rename(FileRenameRequest fileRenameRequest, String params) {
    final String queryParam = HttpUtil.toParams(fileRenameRequest.paramMap());
    final HttpRequest httpRequest = HttpRequest.post("https://pan.baidu.com/api/filemanager?" + queryParam)
            .header(this.headers)
            .cookie(this.cookie)
            .body(params);
    final String body = httpRequest.execute().body();
    final FileRenameResponse response = JSONUtil.toBean(body, FileRenameResponse.class);
    if (response.getErrno() == 0) {
        return response.getTaskid();
    }
    return -1L;
}
Copy the code

Finally define the request method to check the task status:

private TaskStatusResponse queryTaskStatus(TaskStatusRequest taskStatusRequest, String params) {
    TaskStatusResponse response;
    final String queryParam = HttpUtil.toParams(taskStatusRequest.paramMap());
    do {
        final String body = HttpRequest.post("https://pan.baidu.com/share/taskquery?" + queryParam)
                .header(this.headers)
                .cookie(this.cookie)
                .body(params)
                .execute()
                .body();

        response = JSONUtil.toBean(body, TaskStatusResponse.class);
    } while(response.getErrno() ! =0 || StringUtils.equalsAny(response.getStatus(), "running"."pending"));
    return response;
}
Copy the code

Experience to upgrade

Once all the class definitions are complete, we can run them directly from the IDE. However, since it is a tool, every time to use also have to open the IDE, is not a little low. To upgrade the experience, we can type the JAR package and run the jar package directly when using it. Maven-assembly-plugin is a FatJar plugin that combines all of our source code and third-party libraries into a jar package.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>cn.howardliu.effectjava.rename.TaskRunner</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>assembly</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Copy the code

Finish, manual.

At the end of the article to summarize

This paper from scratch to achieve the production of a small network tool, baidu network disk file batch renaming. This tool is an example of a tool that can be implemented in any web application with HTTP requests. If you want to obtain the source code, pay attention to the public number “see mountain hut” reply “Java” to obtain.

Green hills never change, green waters always flow. See you next time.

Recommended reading

  • This article describes 24 operations for Java8 Stream Collectors
  • Java8 Optional 6 kinds of operations
  • Use Lambda expressions to achieve super sorting functions
  • Java8 Time Library (1) : Describes the time class and common apis in Java8
  • Java8 time library (2) : Convert Date to LocalDate or LocalDateTime
  • Java8 Time Library (3) : Start using Java8 time classes
  • Java8 time library (4) : check if the date string is valid
  • New features in Java8
  • New features in Java9
  • New features in Java10
  • Optimization of access control based on nested relationships in Java11
  • New features for Java11
  • New features in Java12

Hello, I’m looking at the mountains. Swim in the code, play to enjoy life. If this article is helpful to you, please like, bookmark, follow.