目 录CONTENT

文章目录

兰空图床 采用Hutool+jackson 调用Api方法笔记

Gz
Gz
2023-08-15 / 2 评论 / 0 点赞 / 202 阅读 / 1,093 字 / 正在检测是否收录...

兰空图床 采用Hutool+jackson 调用Api方法笔记

  1. 兰空图床api接口地址:https://lk.zinzin.cc/api

  2. 获取兰空图床的Post请求Token

    public void getToken() {
        // 1.获取token接口
        String url = "https://lk.zinzin.cc/api/v1/tokens";
        // 2.创建携带参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("email", Buddha.email); //Buddha.email 换成自己的邮箱
        map.put("password", Buddha.password); //Buddha.password 换成自己的密码
        // 3.创建POST请求
        HttpResponse response = HttpRequest.post(url).body(JSON.toJSONString(map)).execute();

        // 4.解析数据
        int status = response.getStatus();
        System.out.println("请求响应状态码:" + status);

        String json = response.body();
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(json);
        // 提取URL链接
        String token = rootNode.path("data").path("token").asText();
        System.out.println("token: " + token);
    }

3.获取个人信息

    public void getProfile() {
        String url = "https://lk.zinzin.cc/api/v1/profile";
        String token = "21|zhjeXWLjCG0O6R8DGrdzebQBsERsY32Zo1djhH3z"; //这里的token可以通过调用上面的函数获取


        HttpResponse response = HttpRequest
                .get(url)
                .header("Authorization","Bearer "+ token)
                .header("Accept", "application/json")
                .execute();
         // 4.解析数据
        int status = response.getStatus();
        System.out.println("请求响应状态码:" + status);
        System.out.println(response);
    }
  1. 上传文件
    public void upload() throws JsonProcessingException {
        String url = "https://lk.zinzin.cc/api/v1/upload";
        String token = "21|zhjeXWLjCG0O6R8DGrdzebQBsERsY32Zo1djhH3z"; //这里的token可以通过调用上面的函数获取
        File file = new File("E:\\workspace\\spring-web-mvc-1\\target\\spring-web-mvc-1-1.0-SNAPSHOT\\upload\\aa\\4c8a6279-c15e-4148-8467-373dfdb3980a_1692091478976.jpg");
        HttpResponse response = HttpRequest
                .post(url)
                .header("Authorization","Bearer "+ token)
                .header("Accept", "application/json")
                .header("Content-Type", "multipart/form-data")
                .form("file", file)
                .execute();

        int status = response.getStatus();
        System.out.println("请求响应状态码:" + status);
        String json = response.body();

        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(json);

        // 提取URL链接
        String imgUrl = rootNode.path("data").path("links").path("url").asText();
        System.out.println("URL链接: " + imgUrl);
        
    }
  1. 依赖
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.20</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
        <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.39</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.4</version>
        </dependency>
  1. 给打包一下当工具类使用
public class Buddha {
    public static String email = "XXXXX"; // 换成自己的邮箱和密码
    public static String password = "XXXXX";

    public static String getToken() {
//        // 1.获取token接口
        String url = "https://lk.zinzin.cc/api/v1/tokens";
        // 2.创建携带参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("email", Buddha.email);
        map.put("password", Buddha.password);
        // 3.创建POST请求
        HttpResponse response = HttpRequest.post(url).body(JSON.toJSONString(map)).execute();
        System.out.println(response);

        // 4.解析数据
        int status = response.getStatus();
        System.out.println("请求响应状态码:" + status);
        String json = response.body();
        
		ObjectMapper mapper = new ObjectMapper(); // 创建一个ObjectMapper对象,用于解析JSON
		JsonNode rootNode = mapper.readTree(json); // 解析JSON字符串并将其转换为JsonNode对象
        // 提取URL链接
        String token = rootNode.path("data").path("token").asText();
        
        return token;
    }

    public static String upload(String token, File file) throws JsonProcessingException {
        String url = "https://lk.zinzin.cc/api/v1/upload";
        // 创建上传文件的POST请求
        HttpResponse response = HttpRequest
                .post(url)
                .header("Authorization","Bearer "+ token)
                .header("Accept", "application/json")
                .header("Content-Type", "multipart/form-data")
                .form("file", file)
                .execute();
		// 获取状态码
        int status = response.getStatus();
        System.out.println("请求响应状态码:" + status);
        String json = response.body();

        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(json);

        // 提取URL链接
        String imgUrl = rootNode.path("data").path("links").path("url").asText();
//        System.out.println("URL链接: " + imgUrl);
        return imgUrl;
    }

    public static void print() {
        String bigBuddhaArt =
                "                             _ooOoo_\n" +
                        "                            o8888888o\n" +
                        "                            88\" . \"88\n" +
                        "                            (| -_- |)\n" +
                        "                            O\\  =  /O\n" +
                        "                         ____/`---'\\____\n" +
                        "                       .'  \\\\|     |//  `.\n" +
                        "                      /  \\\\|||  :  |||//  \\ \n" +
                        "                     /  _||||| -:- |||||-  \\ \n" +
                        "                     |   | \\\\\\  -  /// |   |\n" +
                        "                     | \\_|  ''\\---/''  |   |\n" +
                        "                     \\  .-\\__  `-`  ___/-. /\n" +
                        "                   ___`. .'  /--.--\\  `. . __\n" +
                        "                .\"\" '<  `.___\\_<|>_/___.'  >'\"\".\n" +
                        "               | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |\n" +
                        "               \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /\n" +
                        "          ======`-.____`-.___\\_____/___.-`____.-'======\n" +
                        "                             `=---='\n" +
                        "          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
                        "                     佛祖保佑       永不宕机     永无BUG";

        System.out.println(bigBuddhaArt);
    }
}

7.简易演示

    @PostMapping("uploadImg")
    public ResponseEntity<String> uploadImg(@RequestPart Part part, HttpServletRequest request) throws ServletException, IOException {
        // 获取到文件流
        InputStream inputStream = part.getInputStream();
        // 获取token
        String token = Buddha.getToken();
        // 获取文件名
        String imgName = part.getSubmittedFileName();
        // 上传图像获取返回的图片访问url
        String url = Buddha.upload(token, FileUtil.writeFromStream(inputStream, imgName));
        return ResponseEntity.ok(url);
    }
0

评论区