Browse Source

上传图片配置

xusonglin 5 years ago
parent
commit
477f3e8970

+ 32 - 1
src/main/java/com/jkcredit/sysnews/config/CorsConfig.java

@@ -6,8 +6,8 @@ import org.springframework.web.cors.CorsConfiguration;
 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
 import org.springframework.web.filter.CorsFilter;
 import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
 /**
  * @description:
@@ -43,4 +43,35 @@ public class CorsConfig implements WebMvcConfigurer {
                 .allowedMethods("GET", "POST")
                 .maxAge(3600);
     }
+
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry) {
+        registry.addResourceHandler("/app/image/**").addResourceLocations("/home/jknews/news/photo");
+    }
+
+
+    //http://127.0.0.1:8081/app/page/newsArticles
+    //http://127.0.0.1:8081/app/photo/upload
+    //http://127.0.0.1:8081/app/images/202003091435211231234.png
+    //http://127.0.0.1:8081/app/images/202003091435211231235.png
+    //http://127.0.0.1:8081/app/images/202003091435211231236.png
+
+    /*
+    application.properties
+    jkcredit.news.images.upload.path=/home/news/beans/photo
+    jkcredit.news.images.virthr.path=/home/news/beans/photo
+    jkcredit.news.access.url=https://127.0.0.1:8081
+    jkcredit.news.access.url=https://news.jkcredit.cn
+
+
+
+
+    File mkdir upload.path
+    IMAGE /201.png
+    ${jkcredit.news.access.url}${/app/images}/201.png
+
+
+
+     */
+
 }

+ 8 - 2
src/main/java/com/jkcredit/sysnews/service/newsArticle/NewsArticleServiceImpl.java

@@ -27,6 +27,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import java.io.File;
 import java.util.Date;
 
 /**
@@ -39,8 +40,12 @@ import java.util.Date;
 @Service
 @Transactional(readOnly = true)
 public class NewsArticleServiceImpl extends BaseService implements NewsArticleService {
-    @Value("${photo.relativePath}")
-    private String relativePath;
+    @Value("${photo.uploadPath}")
+    private String uploadPath;
+    @Value("${photo.accessPath}")
+    private String accessPath;
+    @Value("${photo.accessResource}")
+    private String accessResource;
 
     @Resource
     NewsArticleMapper mapper;
@@ -79,6 +84,7 @@ public class NewsArticleServiceImpl extends BaseService implements NewsArticleSe
         // 更新新闻content中的图片
         try {
             for (String url : newsArticleDto.getUrlList()) {
+                url = File.pathSeparator + url.replace(accessPath, "").replace(accessResource, "");
                 PhotoPo photoPo = photoService.getPhotoByUrl(url);
                 photoDto.setId(photoPo.getId());
                 photoService.editPhoto(photoDto);

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

@@ -37,8 +37,12 @@ import java.util.UUID;
 @Service
 @Transactional(readOnly = true)
 public class PhotoServiceImpl extends BaseService implements PhotoService {
-    @Value("${photo.relativePath}")
-    private String relativePath;
+    @Value("${photo.uploadPath}")
+    private String uploadPath;
+    @Value("${photo.accessPath}")
+    private String accessPath;
+    @Value("${photo.accessResource}")
+    private String accessResource;
 
     @Resource
     PhotoMapper photoMapper;
@@ -48,7 +52,7 @@ public class PhotoServiceImpl extends BaseService implements PhotoService {
     public String upload(MultipartFile photo) {
         String fileName = UUID.randomUUID().toString().replace("-","") + "-" +
                 photo.getOriginalFilename();
-        String filePath = relativePath + File.separator + fileName;
+        String filePath = uploadPath + File.separator + fileName;
         try {
             UploadUtil.upload(photo, filePath);
         } catch (IOException ioe) {
@@ -60,14 +64,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(filePath);
+            photoPo.setUrl(File.separator + fileName);
             photoMapper.insert(photoPo);
         } catch (Exception e) {
             log.error("图片上传-新增图片失败, 图片地址:{}", filePath);
             log.error("图片上传-新增图片失败,失败原因:{}", e.getMessage());
             throw new ServiceException("图片上传-新增图片失败");
         }
-        return filePath;
+        return accessPath + accessResource + fileName;
     }
 
     @Override
@@ -88,7 +92,7 @@ public class PhotoServiceImpl extends BaseService implements PhotoService {
         validate(photoDto);
         String fileName = UUID.randomUUID().toString().replace("-","") + "-" +
                 photoDto.getPhoto().getOriginalFilename();
-        String filePath = relativePath + File.separator + fileName;
+        String filePath = uploadPath + File.separator + fileName;
 
         try {
             UploadUtil.upload(photoDto.getPhoto(), filePath);
@@ -97,7 +101,7 @@ public class PhotoServiceImpl extends BaseService implements PhotoService {
             throw new ServiceException("图片上传失败");
         }
 
-        photoDto.setUrl(filePath);
+        photoDto.setUrl(File.separator + fileName);
         PhotoPo photoPo = new PhotoPo();
         BeanUtil.copyProperties(photoPo, photoDto);
         photoPo.setCreateTime(new Date());
@@ -149,7 +153,7 @@ public class PhotoServiceImpl extends BaseService implements PhotoService {
         PhotoPo photoPo = photoMapper.selectById(id);
         AssertUtils.assertNotNull(photoPo, CommonConstant.PHOTO_NOT_EXIST);
 
-        String url = photoPo.getUrl();
+        String url = uploadPath + photoPo.getUrl();
         File file = new File(url);
 
         boolean deleteResult = false;

+ 16 - 7
src/main/resources/application-dev.yml

@@ -1,13 +1,22 @@
 server:
   port: 18083
 spring:
-    datasource:
-      url: jdbc:mysql://39.105.129.147:3306/sys_news?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
-      username: root
-      password: 123456
-      driver-class-name: com.mysql.cj.jdbc.Driver
-      platform: mysql
+#    datasource:
+#      url: jdbc:mysql://39.105.12.14:3306/sys_news?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
+#      username: root
+#      password: 123456
+#      driver-class-name: com.mysql.cj.jdbc.Driver
+#      platform: mysql
+  datasource:
+    url: jdbc:mysql://192.168.1.199:3306/sys_news?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
+    username: sys_news
+    password: Sys_news_20200302
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    platform: mysql
 mybatis-plus:
   mapper-locations: classpath:mapper/*Mapper.xml
 photo:
-  relativePath: C:\Users\Administrator\Desktop\excel\photo
+  uploadPath: C:\Users\Administrator\Desktop\excel\photo
+  veritablePath: /home/jknews/news/photo
+  accessPath: http://192.168.1.199:18083
+  accessResource: /app/image/

+ 4 - 1
src/main/resources/application-prod.yml

@@ -10,4 +10,7 @@ spring:
 mybatis-plus:
   mapper-locations: classpath:mapper/*Mapper.xml
 photo:
-  relativePath: /home/jknews/news/photo
+  uploadPath: /home/jknews/news/photo
+  veritablePath: /home/jknews/news/photo
+  accessPath: http://192.168.1.199:18083
+  accessResource: /app/image/

+ 1 - 1
src/main/resources/application.yml

@@ -6,7 +6,7 @@ spring:
       enabled: true
       max-file-size: 200MB
   profiles:
-    active: prod
+    active: dev
   jpa:
     show-sql: true
   resources: