Skip to content

ApiOne 文件处理指南

文件上传

java
import cn.com.digitalhainan.apione.sdk.AttachFile;
import cn.com.digitalhainan.apione.sdk.HttpCaller;
import cn.com.digitalhainan.apione.sdk.HttpParameters;
import cn.com.digitalhainan.apione.sdk.HttpReturn;
import okhttp3.MediaType;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

public class FileUploadExample {

    public static void main(String[] args) throws IOException {
        String requestUrl = "https://api-one-dev.digitalhainan.com.cn/apione";
        String apiName = "file.upload";
        String ak = "your-access-key";
        String sk = "your-secret-key";
        String region = "INTRA";

        // 准备 form 参数
        Map<String, String> formParam = new HashMap<>();
        formParam.put("fileName", "测试文档.pdf");
        formParam.put("fileType", "application/pdf");

        // 读取待上传文件
        File file = new File("/path/to/local/file.pdf");
        byte[] fileBytes = Files.readAllBytes(file.toPath());

        // 准备待上传文件
        Map<String, AttachFile> attachFileMap = new HashMap<>();
        attachFileMap.put("file", new AttachFile(
            MediaType.parse("application/octet-stream"),
            file.getName(),
            fileBytes
        ));

        // 拼装业务信息
        HttpParameters parameters = HttpParameters.builder()
            .api(apiName)
            .region(region)
            .attachFileMaps(attachFileMap)
            .formParamsMap(formParam)
            .mediaType(MediaType.parse("multipart/form-data"))
            .accessKey(ak)
            .secretKey(sk)
            .requestUrl(requestUrl)
            .build();

        // 请求服务,获取 response
        HttpReturn call = HttpCaller.getInstance().call(parameters);
        String response = call.getResponse();
        System.out.println("上传结果:" + response);
    }
}

二进制文件下载

java
import cn.com.digitalhainan.apione.sdk.ContentBody;
import cn.com.digitalhainan.apione.sdk.HttpByteReturn;
import cn.com.digitalhainan.apione.sdk.HttpCaller;
import cn.com.digitalhainan.apione.sdk.HttpParameters;
import okhttp3.MediaType;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class BinaryDownloadExample {

    public static void main(String[] args) {
        String requestUrl = "https://api-one-dev.digitalhainan.com.cn/apione";
        String apiName = "file.download";
        String ak = "your-access-key";
        String sk = "your-secret-key";
        String region = "INTRA";

        ContentBody contentBody = new ContentBody("{\"fileId\":\"12345\"}");

        // 拼装业务信息
        HttpParameters parameters = HttpParameters.builder()
            .api(apiName)
            .region(region)
            .mediaType(MediaType.parse("application/json"))
            .accessKey(ak)
            .secretKey(sk)
            .contentBody(contentBody)
            .requestUrl(requestUrl)
            .build();

        // 请求服务,获取二进制 response
        HttpByteReturn call = HttpCaller.getInstance().callByte(parameters);

        try {
            // 从 response 中获取二进制字节数组
            byte[] bytes = call.getResponse().body().bytes();

            // 保存到文件
            File outputFile = new File("downloaded_file.pdf");
            try (FileOutputStream fos = new FileOutputStream(outputFile)) {
                fos.write(bytes);
            }
            System.out.println("文件下载成功,保存至:" + outputFile.getAbsolutePath());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

关键类说明

用途
AttachFile封装待上传的文件信息(MediaType、文件名、字节数组)
HttpByteReturn二进制响应结果,用于文件下载

Power By 数字海南