ImageUtil.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.caimei.www.utils;
  2. import org.springframework.util.StringUtils;
  3. /**
  4. * Description
  5. *
  6. * @author : Charles
  7. * @date : 2020/7/1
  8. */
  9. public class ImageUtil {
  10. public static String getImageURL(String dirName, String src, String domain) {
  11. return getImageURL(dirName, src, 0,domain);
  12. }
  13. /***
  14. * 获取图片地址
  15. *
  16. * @param src
  17. * 保存在数据库中的图片文件名
  18. * @param type
  19. * 图片的前缀 (如 type = 200 那么则获取的图片是 200_XXX的图片)
  20. * @param dirName
  21. * 图片保存的文件夹名 如 (league)
  22. * @param domain
  23. * 加上域名拼成完整路径
  24. * @return
  25. */
  26. public static String getImageURL(String dirName, String src, int type, String domain) {
  27. //正式环境 域名 http --- https处理
  28. if (domain != null && !"".equals(domain) && domain.startsWith("http:") && domain.toLowerCase().lastIndexOf("config/beta")== -1 && domain.toLowerCase().lastIndexOf("localhost")== -1) {
  29. domain = domain.replace("http:", "https:");
  30. }
  31. //正式环境 图片地址 http --- https处理
  32. if(src != null && src.startsWith("https:")){
  33. //非正式环境 使用http
  34. if (domain !=null && !"".equals(domain) && domain.toLowerCase().lastIndexOf("config/beta")>-1 || domain.toLowerCase().lastIndexOf("localhost")>-1){
  35. src = src.replace("https:","http:");
  36. }
  37. return src;
  38. }
  39. //正式环境 图片地址 http --- https处理
  40. if(src != null && src.startsWith("http:")){
  41. //非正式环境 使用http
  42. if (domain !=null && !"".equals(domain) && domain.toLowerCase().lastIndexOf("config/beta")==-1 && domain.toLowerCase().lastIndexOf("localhost")==-1){
  43. src = src.replace("http:","https:");
  44. }
  45. return src;
  46. }
  47. type = 0 ;
  48. dirName = dirName.trim();
  49. if (dirName == null) {dirName = "";}
  50. if(src == null || src.equalsIgnoreCase("null")) {src = "";}
  51. if (src.indexOf(",") > 0) {
  52. String tmp = src;
  53. src = tmp.substring(0, tmp.indexOf(","));
  54. }
  55. String image = "/img/default/none.jpg";
  56. if (dirName.equals("user")) {
  57. image = "/img/default/HeaderImg.png";
  58. } else if (dirName.equals("club")) {
  59. image = "/img/default/default_club.jpg";
  60. } else if (dirName.equals("shopLogo")) {
  61. image = "/img/base/placeholder.png";
  62. }else if (dirName.equals("caiMeiImage")) {
  63. image = "/img/default/caiMeiImage.jpg";
  64. }else {
  65. image = "/img/default/none.jpg";
  66. }
  67. if (src != null && !src.equals("")) {
  68. if (type != 0 || dirName.equals("product")) {
  69. src = src.replace("\\", "/");
  70. String srcActual = src;
  71. String subDirName = "";
  72. int index = src.lastIndexOf("/");
  73. if (index != -1) {
  74. subDirName = src.substring(0, index + 1);
  75. srcActual = src.substring(index + 1);
  76. }
  77. boolean b = src.startsWith("/uploadFile");
  78. if(b){
  79. image = src;
  80. }else{
  81. image = "/uploadFile/" + dirName + "/" + subDirName
  82. + (type == 0 ? "" : type + "_") + srcActual;
  83. }
  84. } else {
  85. boolean b = src.startsWith("/uploadFile");
  86. if(b){
  87. image = src;
  88. }else{
  89. image = "/uploadFile/" + dirName + "/" + src;
  90. }
  91. }
  92. }else {
  93. if(StringUtils.isEmpty(image)) {
  94. image = "/img/default/none.jpg";
  95. }
  96. }
  97. return domain + image;
  98. }
  99. public static String getProductImageURL(String image, int type, String domain) {
  100. if (image == null) {
  101. return getImageURL("product", "", type, domain);
  102. }
  103. return getImageURL("product", image, type, domain);
  104. }
  105. }