shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
package com.ishop.merchant.util;
 
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.StringUtils;
 
import java.util.ArrayList;
import java.util.List;
 
public class ImageUtils {
 
    /**
     * 把给定的图片路径集合,分别拼接上前缀,在商品轮播图字段中常用。
     * @param images ["img/001/demo.png", "img/002/demo.png", ""]
     * @param prefix 前缀,如:http://localhost:8082/admin/ 就是方法 getCdnUrl()
     * @return 返回路径中统一拼接前缀
     * @date 2023-06-17
     */
    public static final String combineMultiImageUrl(String images, String prefix){
        List<String> combinedImageList = new ArrayList<>(4);
        try {
            ArrayNode nodes = JsonUtils.toJsonArray(images);
            for(int i=0; i< nodes.size(); i++){
//                nodes.set(i, prefix + nodes.get(i).asText());
                combinedImageList.add(prefix + nodes.get(i).asText());
            }
            return JsonUtils.objectToJsonString(combinedImageList);
        } catch (Exception e) {
            throw new RuntimeException("图片集合转json数组错误:" + images, e);
        }
    }
 
    /**
     * 把数组中多个图片路径,替换后,重新组成json数组。清除掉前缀。
     * @param images ["http://local/a.png","http://local/b.png"]
     * @param prefix 前缀
     * @return 替换后,消除前缀。
     * @date 2023-07-20
     */
    public static final String clearCdnMultiImageUrl(String images, String prefix){
        List<String> clearImageList = new ArrayList<>(4);
        try {
            ArrayNode nodes = JsonUtils.toJsonArray(images);
            for(int i=0; i< nodes.size(); i++){
                String img = nodes.get(i).asText().replace(prefix, StringUtils.EMPTY_STRING);
                clearImageList.add(img);
            }
            return JsonUtils.objectToJsonString(clearImageList);
        } catch (Exception e) {
            throw new RuntimeException("图片集合转json数组错误:" + images, e);
        }
    }
}