递归遍历json,获取里面的图片地址,将图片,压缩,发送到的指定位置Url,并用Url替换原json中图片url

/**
	 * 遍历json,获取里面的图片地址,获取图片,压缩,发送到指定位置,并替换json
	 * @param str 
	 * @param replaceList 
	 * @param url 图片存放的未知
	 * @return
	 * @throws IOException 
	 */
	public static JSONObject h5PublishImageDispose(String str, String replaceUrl, String url) throws IOException{
        JSONObject jsonObject=JSONObject.parseObject(str);
        //遍历
        Set<String> set=jsonObject.keySet();
        for (String string : set) {
        	if (replaceUrl.equals(string)) {
        		
        		
            	String imgUrl = jsonObject.getString(string);
            	if(!imgUrl.startsWith("http")) {
        			continue;
        		}
            	
            	//FileUtil.downLoadFromUrl("http://t.wx.yunovo.cn/group1/M00/00/2C/wKgD8V77_cuARjftAAAi3T-4_kU421.jpg","wKgD8V77_cuARjftAAAi3T-4_kU421.jpg","C:\\Users\\Administrator\\Desktop");
            	String[] imgStr = imgUrl.split("\\.");
            	if ("png".equalsIgnoreCase(imgStr[imgStr.length-1]) || "jpg".equalsIgnoreCase(imgStr[imgStr.length-1])) {
            		String imgName = UUID.randomUUID().toString().replaceAll("-", "")+"."+imgStr[imgStr.length-1]; 
                	FileUtil.downLoadCompressSave(imgUrl, imgName, url);
                	
                	//得到图片,同时压缩,并发送到指定位置
                    jsonObject.put(replaceUrl, "image/"+imgName);
            	}
            } else if (isJsonArray(jsonObject.getString(string))){// ]可以判断该字段值为一个数组,数组就需要转成JsonArray才行
                JSONArray array=JSONObject.parseArray(jsonObject.getString(string));
              
                for (int i = 0; i < array.size() ;i++) {
                    Object o = array.get(i);
                    array.set(i,h5PublishImageDispose(o.toString(),replaceUrl,url));
                }
                jsonObject.put(string, array);
            }else if (isJsonObject(jsonObject.getString(string))) {
                JSONObject jsonObject2=h5PublishImageDispose(jsonObject.getString(string),replaceUrl,url);
                jsonObject.put(string, jsonObject2);
            }
        }
        return jsonObject;
    }
	
	public static boolean isJsonObject(String obj) {
    	if(StringUtils.isBlank(obj)) {
    		return false;
    	}
    	try {
    		JSONObject.parseObject(obj);
		} catch (Exception e) {
			return false;
		}
    	return true;
    }
    
    public static boolean isJsonArray(String obj) {
    	if(StringUtils.isBlank(obj)) {
    		return false;
    	}
    	try {
    		JSONObject.parseArray(obj);
		} catch (Exception e) {
			return false;
		}
    	return true;
    }
    
    public static void main(String[] args) {
    	String str="{'pages':[{'elements':[{'uuid':'842ddb19-b480-4207-82a9-9dac0cc2cf3d','elName':'qk-image','animations':[],'commonStyle':{'position':'absolute','width':1024,'height':600,'top':0,'left':0,'rotate':0,'paddingTop':0,'paddingLeft':0,'paddingRight':0,'paddingBottom':0,'marginTop':0,'marginLeft':0,'marginRight':0,'marginBottom':0,'borderWidth':0,'borderColor':'','borderStyle':'solid','borderRadius':0,'boxShadow':'','fontSize':16,'fontWeight':'normal','lineHeight':1.4,'letterSpacing':0,'textAlign':'center','color':'#000000','backgroundColor':'','backgroundImage':'','backgroundSize':'cover','backgroundRepeat':'no-repeat','fontStyle':'normal','opacity':1,'zIndex':4,'textDecoration':'none'},'events':[],'propsValue':{'imageSrc':'http://t.wx.yunovo.cn/group1/M00/00/2B/wKgD8V7wV8SAYLZYAAI6zNrBzVc236.jpg'},'valueType':'String','isForm':false},{'uuid':'403a0fe0-d363-4db8-8ea4-aceda435a41a','elName':'qk-music','animations':[],'commonStyle':{'position':'absolute','width':194,'height':212,'top':23,'left':55,'rotate':0,'paddingTop':0,'paddingLeft':0,'paddingRight':0,'paddingBottom':0,'marginTop':0,'marginLeft':0,'marginRight':0,'marginBottom':0,'borderWidth':0,'borderColor':'','borderStyle':'solid','borderRadius':0,'boxShadow':'','fontSize':15,'fontWeight':'normal','lineHeight':1.5,'letterSpacing':1,'textAlign':'center','color':'rgba(92, 134, 176, 1)','backgroundColor':'','backgroundImage':'','backgroundSize':'cover','backgroundRepeat':'no-repeat','fontStyle':'normal','opacity':1,'zIndex':5,'textDecoration':'none','fontFamily':'华文细黑'},'events':[],'propsValue':{'fontColor':'rgba(92, 134, 176, 1)','posterSrc':'http://t.wx.yunovo.cn/group1/M00/00/2C/wKgD8V74WNWAeQICAAJa0CgirKE150.png','showLyric':false,'controllerSize':15,'controllerStyle':1,'musicInfo':[{'src':'http://t.wx.yunovo.cn/group1/M00/00/27/wKgD8V7GGsqAbqg6AIzVc0DxTiQ878.mp3','title':'等你爱我','author':'陈奕迅','lyric':'','img':'http://t.wx.yunovo.cn/group1/M00/00/2C/wKgD8V7x14mATVqyAACt_b_k4ZA020.jpg'},{'src':'http://t.wx.yunovo.cn/group1/M00/00/16/wKgD8V6zy6KAC58KAJ0lESXoWM0871.mp3','title':'青春肩并肩','author':'未知','img':'http://t.wx.yunovo.cn/group1/M00/00/2C/wKgD8V7x1caAbTY0AAEeNWHsz1c821.jpg'}]},'valueType':'String','isForm':false},{'uuid':'e572e3b1-ef0c-483e-9593-34d8a8207cc7','elName':'qk-doi-voice','animations':[],'commonStyle':{'position':'absolute','width':240,'height':40,'top':43,'left':789,'rotate':0,'paddingTop':0,'paddingLeft':0,'paddingRight':0,'paddingBottom':0,'marginTop':0,'marginLeft':0,'marginRight':0,'marginBottom':0,'borderWidth':0,'borderColor':'','borderStyle':'solid','borderRadius':0,'boxShadow':'','fontSize':12,'fontWeight':'normal','lineHeight':1.4,'letterSpacing':0,'textAlign':'center','color':'rgba(255, 255, 255, 1)','backgroundColor':'rgba(0, 0, 0, 0)','backgroundImage':'','backgroundSize':'cover','backgroundRepeat':'no-repeat','fontStyle':'normal','opacity':0.5,'zIndex':4,'textDecoration':'none'},'events':[],'propsValue':{'text':'您好呀,好久没有听到您的声音了,试着说“你好小杰”,您的需求,一呼就应!','labelText':'进入页面播报内容:','switchBtn':true,'labelSwitchBtn':'是否进入页面开始播报:','count':3,'labelCount':'进入页面延迟播报(ms):'},'valueType':'String','isForm':false},{'uuid':'3d941f39-a099-4191-8aa2-8f77c4d0422c','elName':'qk-text','animations':[],'commonStyle':{'position':'absolute','width':310,'height':209,'top':305,'left':161,'rotate':0,'paddingTop':0,'paddingLeft':0,'paddingRight':0,'paddingBottom':0,'marginTop':0,'marginLeft':0,'marginRight':0,'marginBottom':0,'borderWidth':0,'borderColor':'','borderStyle':'solid','borderRadius':0,'boxShadow':'','fontSize':15,'fontWeight':'normal','lineHeight':1.5,'letterSpacing':0,'textAlign':'center','color':'rgba(255, 255, 255, 1)','backgroundColor':'','backgroundImage':'','backgroundSize':'cover','backgroundRepeat':'no-repeat','fontStyle':'normal','opacity':0.8,'zIndex':4,'textDecoration':'none','fontFamily':'华文细黑'},'events':[],'propsValue':{'text':'等你爱我\\n也许只有一次才能永久\\n是否爱情都会有折磨\\n可我不承认这么说\\n注定等待你我已足够\\n所以放心才能更快乐\\n当你有一天对我说\\n我依然会在这里等着\\n'},'valueType':'String','isForm':false}],'name':'','commonStyle':{'backgroundColor':'rgba(157, 139, 139, 1)','backgroundImage':'','backgroundSize':'cover','backgroundRepeat':'no-repeat'},'uuid':'d74e5fa7-922a-4e0e-92f1-1bd4146ff3d6','config':{}}],'pageMode':'pc','coverImage':'','name':'','auther':'','width':1024,'description':'我用可视化编辑器做了一个超酷炫的H5,快来看看吧。','title':'插屏弹窗','script':'','height':600,'flipType':0}";
    	List<String> replaceList = new ArrayList<>();
    	replaceList.add("imageSrc");
    	replaceList.add("img");
    	try {
    		JSONObject jsonObject = h5PublishImageDispose(str, "imageSrc", "E:\\a\\b\\c\\d");
    		jsonObject = h5PublishImageDispose(jsonObject.toJSONString(), "img", "E:\\a\\b\\c\\d");
    		System.out.println(jsonObject.toJSONString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值