fhx
2023-04-06 c1a9eee9c9aa0d3c7592fa75e70243e0297b3a0d
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import net.sf.json.JSONArray;
4
5 /**图片路径加上域名
6  * @author ChenJiaHe
7  * @Date 2020-07-07
8  */
9 public class ImagesAddDomain {
10
11     /**
12      * 图片路径加上域名
13      * @param arrString 图片JSON字符串数组
14      * @param domainName 域名
15      * @return
16      */
17     public static JSONArray imagesAddDomain(String arrString, String domainName){
18         JSONArray arr = new JSONArray();
19         if(SimpleTool.checkNotNull(arrString)){
20             JSONArray images = JSONArray.fromObject(arrString);
21             for(int i = 0;i<images.size();i++){
db7fc9 22                 if(images.getString(i).startsWith("http")){
5c5945 23                     arr.add(images.getString(i));
E 24                 }else{
25                     arr.add(urlAddDomain(images.getString(i),domainName));
26                 }
27             }
28         }
29         return arr;
30     }
31
32     /**
33      * 图片路径加上域名
34      * @param imagesArr 图片数组
35      * @param domainName 域名
36      * @return
37      */
38     public static JSONArray imagesAddDomain(JSONArray imagesArr, String domainName){
39         JSONArray arr = new JSONArray();
40         if(SimpleTool.checkNotNull(imagesArr)){
41             for(int i = 0;i<imagesArr.size();i++){
db7fc9 42                 if(imagesArr.getString(i).startsWith("http")){
5c5945 43                     arr.add(imagesArr.getString(i));
E 44                 }else{
45                     arr.add(urlAddDomain(imagesArr.getString(i),domainName));
46                 }
47             }
48         }
49         return arr;
50     }
51
52     /**
53      * 图片路径加上域名
54      * @param imagesUrl 图片路径
55      * @param domainName 域名
56      * @return
57      */
58     public static String urlAddDomain(String imagesUrl, String domainName){
59         if(StringUtils.isEmpty(domainName)){
60             return imagesUrl;
61         }
62         if(SimpleTool.checkNotNull(imagesUrl)){
db7fc9 63             if(imagesUrl.startsWith("http")){
5c5945 64                 return imagesUrl;
E 65             }
66             if(domainName.endsWith("/")){
67                 if(imagesUrl.startsWith("/")){
68                     imagesUrl = imagesUrl.replaceFirst("/","");
69                     imagesUrl = domainName+imagesUrl;
70                 }else{
71                     imagesUrl = domainName+imagesUrl;
72                 }
73             }else{
74                 if(imagesUrl.startsWith("/")){
75                     imagesUrl = domainName+imagesUrl;
76                 }else{
77                     imagesUrl = domainName+"/"+imagesUrl;
78                 }
79             }
80         }
81         return imagesUrl;
82     }
83
84 }