package com.weaver.seconddev.chapanda.test;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.*;

/**
 * packageName com.weaver.seconddev.chapanda.test
 *
 * @ClassName testFile
 * @Author shil
 * @Date 2025/8/11 11:19
 * @Description TODO
 */

public class testFile2 {

    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");

//        "http://110.185.172.132:8086/esignproweb/rest/file-system/operation/download?fileKey=$8d83de01-eac1-4057-8857-a052d8a7fa18$2199386656&signature=q0hcfHvCCkY6cs1BicF7Uo0S3X4%3D%0A&expire=1757474306624&type=pdf"
        // http://110.185.172.132:8086/esignproweb/rest/file-system/operation/download?fileKey=$8d83de01-eac1-4057-8857-a052d8a7fa18$2199386656&signature=MdzA2KBWz2oEPQXG3k2fJraq1U8%3D%0A&expire=1757495652233&type=pdf

        try {
            Request request = new Request.Builder()
                    .url("http://110.185.172.132:8086/esignproweb/rest/file-system/operation/download?fileKey=$8d83de01-eac1-4057-8857-a052d8a7fa18$2199386656&signature=MdzA2KBWz2oEPQXG3k2fJraq1U8%3D%0A&expire=1757495652233&type=pdf")
                    .method("GET", null)
                    .build();
            Response response = client.newCall(request).execute();
            if(response.isSuccessful()){

                InputStream is = null;
                FileOutputStream fos = null;
                File file = new File("/Users/apple/Downloads/测试66.pdf");
                try {
                    is = response.body().byteStream();
                    System.out.println(is.available());

//                    byte[] fileByte = inputStreamToByteArray(is);
                    fos = new FileOutputStream(file);
                    byte[] fileByte = response.body().bytes();
//                    System.out.println("fileByte:{}"+fileByte.length);
                    fos.write(fileByte);
                    fos.flush();

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (is != null) {
                            is.close();
                        }
                        if (fos != null) {
                            fos.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


//                String content = response.body().string();
//                Files.write(new File("/Users/apple/Downloads/测试22.pdf").toPath(), content.getBytes(StandardCharsets.UTF_8));
//                System.out.println("下载完成！");
        } else {
            System.out.println("下载失败：" + response.message());
        }



    } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] inputStreamToByteArray(InputStream inputStream) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
        outputStream.flush();
        return outputStream.toByteArray();
    }

}
