liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.xiaopeng.account.util;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xiaopeng.evi.oss.service.AliOssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.UnsupportedEncodingException;
import java.util.Objects;
 
/**
 * @author MiTao
 * @date 2026-03-25 11:24
 *
 */
@Slf4j
@Component
public class AliOssDownloadUtil {
 
    @Autowired
    private AliOssService aliOssService;
 
    public String getPictureJsonArray(String json) {
        JSONArray res = new JSONArray();
        if (json == null || json.isEmpty()) {
            return res.toJSONString();
        }
 
        if (json.startsWith("{")) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (Objects.isNull(jsonObject)) {
                return res.toJSONString();
            }
            String downloadUrl = "";
            try {
                downloadUrl = aliOssService.getDownloadUrl(
                        jsonObject.getString("id"), jsonObject.getString("name"), true);
            } catch (UnsupportedEncodingException e) {
                log.error("获取图片下载地址失败", e);
            }
            jsonObject.put("url", downloadUrl);
            res.add(jsonObject);
 
        } else if (json.startsWith("[")) {
            JSONArray jsonArray = JSONArray.parseArray(json);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (Objects.isNull(jsonObject)) {
                    continue;
                }
                String downloadUrl = "";
                try {
                    downloadUrl = aliOssService.getDownloadUrl(
                            jsonObject.getString("id"), jsonObject.getString("name"), true);
                } catch (UnsupportedEncodingException e) {
                    log.error("获取图片下载地址失败", e);
                }
                jsonObject.put("url", downloadUrl);
            }
            res = jsonArray;
        }
        return res.toJSONString();
    }
 
    /**
     * JSONObject类型图片
     * @param json
     * @return
     */
    public String getPictureJSONObject(String json){
        JSONObject jsonObject = JSONObject.parseObject(json);
        if (Objects.isNull(jsonObject)) {
            return new JSONObject().toJSONString();
        }
        String downloadUrl = jsonObject.getString("url");
        try {
            downloadUrl = aliOssService.getDownloadUrl(
                    jsonObject.getString("id"), jsonObject.getString("name"), true);
        } catch (UnsupportedEncodingException e) {
            log.error("获取图片下载地址失败", e);
        }
        jsonObject.put("url", downloadUrl);
        return jsonObject.toJSONString();
    }
}