Przeglądaj źródła

修改图片路径

xusonglin 5 lat temu
rodzic
commit
c79da5c464

+ 4 - 11
src/main/java/com/jkcredit/sysnews/service/links/LinksServiceImpl.java

@@ -17,6 +17,7 @@ import com.jkcredit.sysnews.spi.lang.constant.CommonConstant;
 import com.jkcredit.sysnews.spi.lang.exception.ServiceException;
 import com.jkcredit.sysnews.util.AssertUtils;
 import com.jkcredit.sysnews.util.BeanUtil;
+import com.jkcredit.sysnews.util.CommonUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.BeanUtils;
@@ -45,11 +46,6 @@ public class LinksServiceImpl extends BaseService implements LinksService {
     @Autowired
     PhotoService photoService;
 
-    @Value("${photo.accessPath}")
-    private String accessPath;
-    @Value("${photo.accessResource}")
-    private String accessResource;
-
     @Override
     @Transactional(rollbackFor = ServiceException.class)
     public void saveLinks(LinksDto linksDto) {
@@ -135,8 +131,7 @@ public class LinksServiceImpl extends BaseService implements LinksService {
         LinksVo vo = new LinksVo();
         BeanUtils.copyProperties(po, vo);
         if (po.getPhotoUrl() != null) {
-            String url = accessPath + accessResource + po.getPhotoUrl().replace("/","");
-            vo.setPhotoUrl(url);
+            vo.setPhotoUrl(CommonUtil.getDummyPath(po.getPhotoUrl()));
         }
         return vo;
     }
@@ -148,8 +143,7 @@ public class LinksServiceImpl extends BaseService implements LinksService {
         LinksVo linksVo = new LinksVo();
         BeanUtil.copyProperties(linksVo, linksPo);
         if (linksPo.getPhotoUrl() != null) {
-            String url = accessPath + accessResource + linksPo.getPhotoUrl().replace("/","");
-            linksVo.setPhotoUrl(url);
+            linksVo.setPhotoUrl(CommonUtil.getDummyPath(linksPo.getPhotoUrl()));
         }
         return linksVo;
     }
@@ -163,8 +157,7 @@ public class LinksServiceImpl extends BaseService implements LinksService {
                 LinksVo linksVo = new LinksVo();
                 BeanUtil.copyProperties(linksVo, po);
                 if (po.getPhotoUrl() != null) {
-                    String url = accessPath + accessResource + po.getPhotoUrl().replace("/","");
-                    linksVo.setPhotoUrl(url);
+                    linksVo.setPhotoUrl(CommonUtil.getDummyPath(po.getPhotoUrl()));
                 }
                 linksVoList.add(linksVo);
             }

+ 3 - 6
src/main/java/com/jkcredit/sysnews/service/newsArticle/NewsArticleServiceImpl.java

@@ -71,8 +71,7 @@ public class NewsArticleServiceImpl extends BaseService implements NewsArticleSe
         NewsArticleVo vo = new NewsArticleVo();
         BeanUtils.copyProperties(po, vo);
         if (po.getPhotoUrl() != null) {
-            String url = accessPath + accessResource + po.getPhotoUrl().replace("/","");
-            vo.setPhotoUrl(url);
+            vo.setPhotoUrl(CommonUtil.getDummyPath(po.getPhotoUrl()));
         }
         return vo;
     }
@@ -83,8 +82,7 @@ public class NewsArticleServiceImpl extends BaseService implements NewsArticleSe
         NewsArticleVo newsArticleVo = new NewsArticleVo();
         BeanUtil.copyProperties(newsArticleVo, newsArticlePo);
         if (newsArticlePo.getPhotoUrl() != null) {
-            String url = accessPath + accessResource + newsArticlePo.getPhotoUrl().replace("/","");
-            newsArticleVo.setPhotoUrl(url);
+            newsArticleVo.setPhotoUrl(CommonUtil.getDummyPath(newsArticlePo.getPhotoUrl()));
         }
         // 查询新闻所属导航
         List<Long> navigationBarIdList = newsArticleNavigationBarService.getNavigationBarIdByNewsId(id);
@@ -363,8 +361,7 @@ public class NewsArticleServiceImpl extends BaseService implements NewsArticleSe
         NewsArticleBizVo vo = new NewsArticleBizVo();
         BeanUtils.copyProperties(po, vo);
         if (po.getPhotoUrl() != null) {
-            String url = accessPath + accessResource + po.getPhotoUrl().replace("/","");
-            vo.setPhotoUrl(url);
+            vo.setPhotoUrl(CommonUtil.getDummyPath(po.getPhotoUrl()));
         }
         return vo;
     }

+ 8 - 14
src/main/java/com/jkcredit/sysnews/service/photo/PhotoServiceImpl.java

@@ -13,6 +13,7 @@ import com.jkcredit.sysnews.spi.lang.constant.CommonConstant;
 import com.jkcredit.sysnews.spi.lang.exception.ServiceException;
 import com.jkcredit.sysnews.util.AssertUtils;
 import com.jkcredit.sysnews.util.BeanUtil;
+import com.jkcredit.sysnews.util.CommonUtil;
 import com.jkcredit.sysnews.util.UploadUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
@@ -39,10 +40,6 @@ import java.util.*;
 public class PhotoServiceImpl extends BaseService implements PhotoService {
     @Value("${photo.uploadPath}")
     private String uploadPath;
-    @Value("${photo.accessPath}")
-    private String accessPath;
-    @Value("${photo.accessResource}")
-    private String accessResource;
 
     @Resource
     PhotoMapper photoMapper;
@@ -56,7 +53,8 @@ public class PhotoServiceImpl extends BaseService implements PhotoService {
         int month = cal.get(Calendar.MONTH) + 1;
         int year = cal.get(Calendar.YEAR);
         String directoryName = year+"-"+month;
-        String filePath = uploadPath + File.separator + directoryName + File.separator + fileName;
+        String relativePath = File.separator + directoryName + File.separator + fileName;
+        String filePath = uploadPath + relativePath;
         try {
             UploadUtil.upload(photo, filePath);
         } catch (IOException ioe) {
@@ -68,14 +66,14 @@ public class PhotoServiceImpl extends BaseService implements PhotoService {
             PhotoPo photoPo = new PhotoPo();
             photoPo.setCreateTime(new Date());
             photoPo.setStatus(PhotoStatusEnum.UPLOAD_SUCCESS_NOT_USE.getValue());
-            photoPo.setUrl(File.separator+ directoryName + File.separator + fileName);
+            photoPo.setUrl(relativePath);
             photoMapper.insert(photoPo);
         } catch (Exception e) {
             log.error("图片上传-新增图片失败, 图片地址:{}", filePath);
             log.error("图片上传-新增图片失败,失败原因:{}", e.getMessage());
             throw new ServiceException("图片上传-新增图片失败");
         }
-        return accessPath + accessResource + File.separator + directoryName + File.separator + fileName;
+        return CommonUtil.getDummyPath(relativePath);
     }
 
     @Override
@@ -195,9 +193,7 @@ public class PhotoServiceImpl extends BaseService implements PhotoService {
         PhotoVo photoVo = new PhotoVo();
         BeanUtil.copyProperties(photoVo, photoPo);
         if (photoPo.getUrl() != null) {
-            String[] photoUrlArgs = photoPo.getUrl().split("/");
-            String url = accessPath + accessResource + photoUrlArgs[1] + File.separator + photoUrlArgs[2];
-            photoVo.setUrl(url);
+            photoVo.setUrl(CommonUtil.getDummyPath(photoPo.getUrl()));
         }
         return photoVo;
     }
@@ -215,8 +211,7 @@ public class PhotoServiceImpl extends BaseService implements PhotoService {
         if (middlePhoto != null) {
             BeanUtil.copyProperties(middlePhotoVo, middlePhoto);
             if (middlePhoto.getUrl() != null) {
-                String url = accessPath + accessResource + middlePhoto.getUrl().replace("/","");
-                middlePhotoVo.setUrl(url);
+                middlePhotoVo.setUrl(CommonUtil.getDummyPath(middlePhoto.getUrl()));
             }
         }
 
@@ -226,8 +221,7 @@ public class PhotoServiceImpl extends BaseService implements PhotoService {
             PhotoVo vo = new PhotoVo();
             BeanUtil.copyProperties(vo, po);
             if (po.getUrl() != null) {
-                String url = accessPath + accessResource + po.getUrl().replace("/","");
-                vo.setUrl(url);
+                vo.setUrl(CommonUtil.getDummyPath(po.getUrl()));
             }
             mainPhotos.add(vo);
         }

+ 27 - 0
src/main/java/com/jkcredit/sysnews/util/CommonUtil.java

@@ -1,5 +1,10 @@
 package com.jkcredit.sysnews.util;
 
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
@@ -10,7 +15,23 @@ import java.util.Set;
  * @create: 2020/3/11 10:04
  * @version: V1.0
  **/
+@Component
 public class CommonUtil {
+    @Value("${photo.accessPath}")
+    private String accessPath;
+    @Value("${photo.accessResource}")
+    private String accessResource;
+    private static String ACCESS_PATH;
+    private static String ACCESS_RESOURCE;
+
+    @PostConstruct
+    public void getAccessPath() {
+        ACCESS_PATH = accessPath;
+    }
+    @PostConstruct
+    public void getAccessResource() {
+        ACCESS_RESOURCE = accessResource;
+    }
     // 获取差集
     public static List<Long> getSubtraction(Set<Long> primary, Set<Long> secondary) {
         primary.removeAll(secondary);
@@ -22,4 +43,10 @@ public class CommonUtil {
         primary.retainAll(secondary);
         return new ArrayList<>(primary);
     }
+
+    public static String getDummyPath(String path) {
+        String[] photoUrlArgs = path.split("/");
+        String url = ACCESS_PATH + ACCESS_RESOURCE + photoUrlArgs[1] + File.separator + photoUrlArgs[2];
+        return url;
+    }
 }