cdn添加图片域名
网上看到许多需要配置的,这里没有附上配置文件,也不知道要不要用到。
package com.utils.util;
import com.quwei.util.UtilDate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Random;
@Slf4j
@Service("commonFuncton")
public class CommonAgentFunction {
public final static String DEFAULT_IMG_WH = "600*600";
/**
* 商品图片服务器配置
**/
public final static String[] IMG_SERVER_URL = {"https://ossim.com.cn", "https://ossmg2.com.cn"};
/**
* @return java.lang.String 返回类型
* @Title: getImgCDNUrl
* @Description: (图片cdn地址, 高度默认)
* @Param url 参数说明
* @date 2020/7/21 14:36
*/
public static String getImgCDNUrl(String url) {
if (StringUtils.isBlank(url)) {
return null;
}
String[] wdSs = StringUtils.split(DEFAULT_IMG_WH, '*');
String width = wdSs[0];
String height = wdSs[1];
return getImgUrl(url, width, height);
}
/**
* @return java.lang.String 返回类型
* @Title: getImgCDNUrl
* @Description: (图片cdn地址)
* @Param url 参数说明 图片地址
* @Param px 参数说明 宽高 默认600*600
* @date 2020/7/21 14:35
*/
public static String getImgCDNUrl(String url, String widthHeightStr) {
if (StringUtils.isBlank(url)) {
return null;
}
if (StringUtils.isBlank(widthHeightStr)) {
widthHeightStr = DEFAULT_IMG_WH;
}
String[] wdSs = StringUtils.split(widthHeightStr, '*');
String width = wdSs[0];
String height = wdSs[1];
return getImgUrl(url, width, height);
}
private static String getImgUrl(String url, String width, String height) {
StringBuffer sb = new StringBuffer("");
Random ran = new Random();
int index = ran.nextInt(IMG_SERVER_URL.length);
String serverPath = IMG_SERVER_URL[index];
int last = StringUtils.lastIndexOf(url, '.');
String end = StringUtils.substring(url, last + 1, url.length());
int start_str_int = StringUtils.indexOf(url, '/');
String str = "";
if (start_str_int != 0) {
str = "/";
}
if (StringUtils.startsWith(url, "http://") || StringUtils.startsWith(url, "https://")) {
sb.append(url);
} else {
sb.append(serverPath).append(str).append(url);
}
if (!StringUtils.endsWith("png", end) && url.lastIndexOf(",w_") < 0 && url.lastIndexOf(",h_") < 0 && (StringUtils.isNotBlank(width) || StringUtils.isNotBlank(height))) {
if (url.lastIndexOf("?") < 0) {
sb.append("?");
} else {
sb.append("&");
}
sb.append("x-oss-process=image/resize,m_fill");
if (StringUtils.isNotBlank(width)) {
sb.append(",w_").append(width);
}
if (StringUtils.isNotBlank(height)) {
sb.append(",h_").append(height);
}
}
return sb.toString();
}
/**
* 数组以逗号拼接
*
* @param parrArrays
* @return
*/
public static String forArraystoString(String[] parrArrays) {
StringBuffer sb_String = new StringBuffer("");
if (parrArrays.length > 0) {
for (String s : parrArrays) {
sb_String.append(s).append(",");
}
sb_String.deleteCharAt(sb_String.length() - 1);
}
return sb_String.toString();
}
/**
* 拼接字符串
*
* @param separator
* @param strgs
* @return
*/
public static String implode(char separator, String[] strgs) {
if (strgs == null || strgs.length <= 0) {
return "";
}
if (strgs.length == 1) {
return strgs[0];
}
StringBuffer sb = new StringBuffer("");
for (String s : strgs) {
sb.append(s).append(separator);
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* 判断字符串的编码
*
* @param str
* @return
*/
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s = encode;
return s;
}
} catch (Exception exception) {
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s1 = encode;
return s1;
}
} catch (Exception exception1) {
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s2 = encode;
return s2;
}
} catch (Exception exception2) {
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s3 = encode;
return s3;
}
} catch (Exception exception3) {
}
encode = "BIG5";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
String s4 = encode;
return s4;
}
} catch (Exception exception3) {
}
return "";
}
// 获取IP
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("client-ip");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Forwarded-For");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (ip != null && ip.trim().length() > 0) {
if (ip.indexOf(",") > 0) {
String ips[] = ip.split(",");
return ips[ips.length - 1];
} else {
return ip;
}
} else {
return "0.0.0.0";
}
}
}
然后需要用到的地方调用一下就好了
if(StringUtils.isNotBlank(resultgoodsGallerys.getImageUrl())){
resultgoodsGallerys.setImageUrl(CommonAgentFunction.getImgCDNUrl(resultgoodsGallerys.getImageUrl()));
}
我这里是在impl里调用的