OpenList API 的 Spring Boot 自动配置 Starter,通过简单的配置和自动注入即可在 Java 项目中调用 OpenList 的全部 REST API。

快速开始

1. 添加 Maven 依赖

<dependency>
    <groupId>com.echovale</groupId>
    <artifactId>openlist-spring-boot-auto-configuration</artifactId>
    <version>dev-1.0</version>
</dependency>

2. 配置 application.yaml

openlist:
  base-url: https://your-openlist-server:5244
  username: admin
  password: your-password
  # otp-code: 123456           # 如果启用了两步验证,请取消注释并填写验证码
  connect-timeout: 10s          # 连接超时,默认 10s
  read-timeout: 30s             # 读取超时,默认 30s

3. 注入并使用

@Service
public class MyService {

    @Autowired
    private OpenListClient openListClient;

    public void doSomething() {
        // 获取根目录文件列表
        FileListResponse files = openListClient.listFiles("/");
        files.getContent().forEach(item ->
            System.out.println(item.getName() + " - " + item.getSize() + " bytes"));

        // 创建目录
        openListClient.mkdir("/new-folder");

        // 列出所有存储
        List<StorageItem> storages = openListClient.listStorages();
        storages.forEach(s ->
            System.out.println(s.getMountPath() + " -> " + s.getDriver()));
    }
}

配置参考

属性

类型

必填

默认值

说明

openlist.base-url

String

OpenList 服务地址

openlist.username

String

登录用户名

openlist.password

String

登录密码

openlist.otp-code

String

两步验证码(2FA)

openlist.connect-timeout

Duration

10s

连接超时时间

openlist.read-timeout

Duration

30s

读取超时时间

API 接口

通用响应格式

所有 API 的原始 JSON 响应均遵循如下格式:

{
    "code": 200,
    "message": "success",
    "data": { ... }
}

对应的 Java 模型类为 OpenListResponse<T>

字段

类型

说明

code

int

HTTP 状态码,200 表示成功

message

String

响应消息

data

T(泛型)

响应数据,类型因接口而异


认证 API

login — 用户登录

自动登录并获取 JWT Token。Token 会缓存在内存中,后续 API 调用自动携带。

  • 接口方法LoginResponse login()

  • 对应 APIPOST /api/auth/login

请求参数(自动从配置中读取):

参数

类型

必填

说明

openlist.username

String

用户名

openlist.password

String

密码

openlist.otp-code

String

两步验证码

返回类型LoginResponse

字段

类型

说明

token

String

JWT 认证令牌

使用示例

LoginResponse response = openListClient.login();
System.out.println("Token: " + response.getToken());

首次调用任何 API 方法时,Starter 会自动执行登录,通常无需手动调用此方法。


logout — 用户登出

  • 接口方法void logout()

  • 对应 APIGET /api/auth/logout

使用示例

openListClient.logout();

用户 API

getCurrentUser — 获取当前用户信息

  • 接口方法OpenListResponse<JsonNode> getCurrentUser()

  • 对应 APIGET /api/me

返回类型OpenListResponse<JsonNode>

响应 JSON 示例:

{
    "code": 200,
    "message": "success",
    "data": {
        "id": 1,
        "username": "admin",
        "base_path": "/",
        "role": "admin",
        "enabled": true
    }
}

使用示例

OpenListResponse<JsonNode> response = openListClient.getCurrentUser();
if (response.getCode() == 200) {
    JsonNode user = response.getData();
    System.out.println("Username: " + user.get("username").asText());
    System.out.println("Role: " + user.get("role").asText());
}

文件系统 API

listFiles — 获取文件列表

  • 接口方法FileListResponse listFiles(String path)

  • 对应 APIGET /api/fs/list?path=<path>

请求参数

参数

类型

说明

path

String

目录路径,"/" 表示根目录

返回类型FileListResponse

字段

类型

说明

content

List<FileItem>

文件/目录列表

total

int

文件/目录总数

FileItem 字段:

字段

类型

说明

name

String

文件/目录名

size

long

文件大小(字节)

isDir

boolean

是否为目录

modified

String

最后修改时间

响应 JSON 示例:

{
    "code": 200,
    "message": "success",
    "data": {
        "content": [
            { "name": "Documents", "size": 0, "is_dir": true, "modified": "2024-01-15T10:30:00Z" },
            { "name": "readme.txt", "size": 1024, "is_dir": false, "modified": "2024-01-10T08:00:00Z" }
        ],
        "total": 2
    }
}

使用示例

FileListResponse response = openListClient.listFiles("/");
for (FileItem item : response.getContent()) {
    String type = item.isDir() ? "[DIR]" : "[FILE]";
    System.out.println(type + " " + item.getName() + " (" + item.getSize() + " bytes)");
}

getFile — 获取文件详情

  • 接口方法OpenListResponse<JsonNode> getFile(String path)

  • 对应 APIGET /api/fs/get?path=<path>

请求参数

参数

类型

说明

path

String

文件路径

使用示例

OpenListResponse<JsonNode> response = openListClient.getFile("/readme.txt");
System.out.println("File info: " + response.getData());

mkdir — 创建目录

  • 接口方法void mkdir(String path)

  • 对应 APIPOST /api/fs/mkdir

请求参数

参数

类型

说明

path

String

要创建的目录路径

请求体 JSON 示例:

{ "path": "/new-directory" }

使用示例

openListClient.mkdir("/new-directory");

rename — 重命名文件/目录

  • 接口方法void rename(String oldPath, String newPath)

  • 对应 APIPOST /api/fs/rename

请求参数

参数

类型

说明

oldPath

String

原路径

newPath

String

新路径

请求体 JSON 示例:

{ "old_path": "/old-name.txt", "new_path": "/new-name.txt" }

使用示例

openListClient.rename("/old-name.txt", "/new-name.txt");

remove — 删除文件/目录

  • 接口方法void remove(String path)

  • 对应 APIPOST /api/fs/remove

请求参数

参数

类型

说明

path

String

要删除的路径

请求体 JSON 示例:

{ "path": "/file-to-delete.txt" }

使用示例

openListClient.remove("/file-to-delete.txt");

uploadFile — 上传文件

  • 接口方法void uploadFile(String path, byte[] content)

  • 对应 APIPUT /api/fs/put?path=<path>

请求参数

参数

类型

说明

path

String

上传目标路径

content

byte[]

文件二进制内容

请求头:Content-Type: application/octet-stream

使用示例

byte[] fileContent = Files.readAllBytes(Paths.get("local-file.pdf"));
openListClient.uploadFile("/uploads/local-file.pdf", fileContent);

addOfflineDownload — 添加离线下载任务

  • 接口方法void addOfflineDownload(String url, String path)

  • 对应 APIPOST /api/fs/add_offline_download

请求参数

参数

类型

说明

url

String

下载链接

path

String

保存目录路径

请求体 JSON 示例:

{ "url": "https://example.com/large-file.zip", "path": "/downloads" }

使用示例

openListClient.addOfflineDownload("https://example.com/file.zip", "/downloads");

decompressArchive — 解压归档文件

  • 接口方法void decompressArchive(String path, String targetPath)

  • 对应 APIPOST /api/fs/archive/decompress

请求参数

参数

类型

说明

path

String

压缩包路径

targetPath

String

解压目标目录

请求体 JSON 示例:

{ "path": "/downloads/archive.zip", "target_path": "/extracted" }

使用示例

openListClient.decompressArchive("/downloads/archive.zip", "/extracted");

batchRename — 批量重命名

  • 接口方法void batchRename(List<BatchRenameOperation> operations)

  • 对应 APIPOST /api/fs/batch_rename

请求参数

参数

类型

说明

operations

List<BatchRenameOperation>

重命名操作列表

BatchRenameOperation 字段:

字段

类型

说明

oldPath

String

原路径

newPath

String

新路径

请求体 JSON 示例:

{
    "operations": [
        { "old_path": "/a.txt", "new_path": "/a-renamed.txt" },
        { "old_path": "/b.txt", "new_path": "/b-renamed.txt" }
    ]
}

使用示例

List<BatchRenameOperation> ops = new ArrayList<>();

BatchRenameOperation op1 = new BatchRenameOperation();
op1.setOldPath("/a.txt");
op1.setNewPath("/a-renamed.txt");
ops.add(op1);

BatchRenameOperation op2 = new BatchRenameOperation();
op2.setOldPath("/b.txt");
op2.setNewPath("/b-renamed.txt");
ops.add(op2);

openListClient.batchRename(ops);