图片过大,图片服务器提供不同格式图片,服务器获取url进行转换;
package com.lvtu.dao.common; import org.apache.commons.lang3.StringUtils; public class Test { public static void main(String[] args) { String ss="http://pic.lvmama.com/580x290//uploads/pc/place2/sss.jpg"; String dd= getFavSmallPictureUrl(ss,"_120_60"); System.out.println(dd); } /** * 获取小图片的url * @param url * @param size * @return */ public static String getSmallPictureUrl(String url, String size){ if (StringUtils.isEmpty(url)){ // 返回小驴图片 return "http://pic.lvmama.com/img/cmt/img_120_60.jpg"; } // 拼接图片大小 int lastDotPos = url.lastIndexOf("."); if (lastDotPos > -1 && StringUtils.isNotEmpty(size)) { StringBuilder newUrlBuilder = new StringBuilder(); newUrlBuilder.append(url.substring(0, lastDotPos)); newUrlBuilder.append(size); newUrlBuilder.append(url.substring(lastDotPos, url.length())); url = newUrlBuilder.toString(); } return url; } /** * 转换收藏列表的图片,由于收藏图片都是来至于其他接口,所以在转换前,先将图片转换成原图格式 * @param url 图片URL * @param size 需要拼接的尺寸 * @return */ public static String getFavSmallPictureUrl(String url, String size) { if(StringUtils.isNotBlank(url) && url.startsWith("http://pic.lvmama.com/")) { // http://pic.lvmama.com/580x290//uploads/pc/place2/sss.jpg String regex1 = "http://pic.lvmama.com/\\d+x\\d+/.+"; // _580_290.jpg String regex2 = "http://pic.lvmama.com/.+_\\d+_\\d+\\.\\w+"; // _1028_.jpg String regex3 = "http://pic.lvmama.com/.+_\\d+_\\.\\w+"; if(url.matches(regex1)) { String tempUrl = url.substring("http://pic.lvmama.com/".length()); tempUrl = tempUrl.substring(tempUrl.indexOf("/") + 1); url = "http://pic.lvmama.com/" + tempUrl; } else if(url.matches(regex2) || url.matches(regex3)) { String tempUrl = url.substring(0, url.lastIndexOf("_")); tempUrl = tempUrl.substring(0, tempUrl.lastIndexOf("_")); String suffix = url.substring(url.lastIndexOf(".")); url = tempUrl + suffix; } } return getSmallPictureUrl(url, size); } }