123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package com.caimei.www.utils;
- import org.springframework.util.StringUtils;
- /**
- * Description
- *
- * @author : Charles
- * @date : 2020/7/1
- */
- public class ImageUtil {
- public static String getImageURL(String dirName, String src, String domain) {
- return getImageURL(dirName, src, 0,domain);
- }
- /***
- * 获取图片地址
- *
- * @param src
- * 保存在数据库中的图片文件名
- * @param type
- * 图片的前缀 (如 type = 200 那么则获取的图片是 200_XXX的图片)
- * @param dirName
- * 图片保存的文件夹名 如 (league)
- * @param domain
- * 加上域名拼成完整路径
- * @return
- */
- public static String getImageURL(String dirName, String src, int type, String domain) {
- //正式环境 域名 http --- https处理
- if (domain != null && !"".equals(domain) && domain.startsWith("http:") && domain.toLowerCase().lastIndexOf("config/beta")== -1 && domain.toLowerCase().lastIndexOf("localhost")== -1) {
- domain = domain.replace("http:", "https:");
- }
- //正式环境 图片地址 http --- https处理
- if(src != null && src.startsWith("https:")){
- //非正式环境 使用http
- if (domain !=null && !"".equals(domain) && domain.toLowerCase().lastIndexOf("config/beta")>-1 || domain.toLowerCase().lastIndexOf("localhost")>-1){
- src = src.replace("https:","http:");
- }
- return src;
- }
- //正式环境 图片地址 http --- https处理
- if(src != null && src.startsWith("http:")){
- //非正式环境 使用http
- if (domain !=null && !"".equals(domain) && domain.toLowerCase().lastIndexOf("config/beta")==-1 && domain.toLowerCase().lastIndexOf("localhost")==-1){
- src = src.replace("http:","https:");
- }
- return src;
- }
- type = 0 ;
- dirName = dirName.trim();
- if (dirName == null) {dirName = "";}
- if(src == null || src.equalsIgnoreCase("null")) {src = "";}
- if (src.indexOf(",") > 0) {
- String tmp = src;
- src = tmp.substring(0, tmp.indexOf(","));
- }
- String image = "/img/default/none.jpg";
- if (dirName.equals("user")) {
- image = "/img/default/HeaderImg.png";
- } else if (dirName.equals("club")) {
- image = "/img/default/default_club.jpg";
- } else if (dirName.equals("shopLogo")) {
- image = "/img/base/placeholder.png";
- }else if (dirName.equals("caiMeiImage")) {
- image = "/img/default/caiMeiImage.jpg";
- }else {
- image = "/img/default/none.jpg";
- }
- if (src != null && !src.equals("")) {
- if (type != 0 || dirName.equals("product")) {
- src = src.replace("\\", "/");
- String srcActual = src;
- String subDirName = "";
- int index = src.lastIndexOf("/");
- if (index != -1) {
- subDirName = src.substring(0, index + 1);
- srcActual = src.substring(index + 1);
- }
- boolean b = src.startsWith("/uploadFile");
- if(b){
- image = src;
- }else{
- image = "/uploadFile/" + dirName + "/" + subDirName
- + (type == 0 ? "" : type + "_") + srcActual;
- }
- } else {
- boolean b = src.startsWith("/uploadFile");
- if(b){
- image = src;
- }else{
- image = "/uploadFile/" + dirName + "/" + src;
- }
- }
- }else {
- if(StringUtils.isEmpty(image)) {
- image = "/img/default/none.jpg";
- }
- }
- return domain + image;
- }
- public static String getProductImageURL(String image, int type, String domain) {
- if (image == null) {
- return getImageURL("product", "", type, domain);
- }
- return getImageURL("product", image, type, domain);
- }
- }
|