Sfoglia il codice sorgente

修改封面图片为字符串路径

yusm 7 mesi fa
parent
commit
c6a0190a1d

+ 1 - 2
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/controller/ArticleController.java

@@ -8,7 +8,6 @@ import com.my.bigevent.pojo.Result;
 import com.my.bigevent.service.ArticleService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
-import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
 
@@ -73,7 +72,7 @@ public class ArticleController
                                          @RequestParam(value = "content",required = true) String content,
                                          @RequestParam(value = "profile",required = false) String profile,
                                          @RequestParam(value = "state",required = true) String state,
-                                         @RequestParam(value = "coverImage",required = false) MultipartFile coverImage,
+                                         @RequestParam(value = "coverImage",required = false) String coverImage,
                                          @RequestParam(value = "id",required = false) Integer id,
                                          @RequestParam(value = "productId",required = true) String productId
                                         )

+ 61 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/controller/CommonUploadController.java

@@ -0,0 +1,61 @@
+package com.my.bigevent.controller;
+
+import com.my.bigevent.pojo.Result;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.UUID;
+
+@RestController
+@RequestMapping("/common")
+public class CommonUploadController {
+
+
+    @Value(value = "${upload.path}")
+    private String path;
+
+
+    @RequestMapping(value="uploadFile")
+    public Object uploadFile(MultipartFile multipartFile) {
+
+        //然后处理文件
+        String fileName = multipartFile.getOriginalFilename();
+        String[] split = fileName.split("\\.");
+        String serverName = UUID.randomUUID().toString().replaceAll("-", "") + "."+split[split.length-1];
+
+        //检查目录
+        File dir = new File(path);
+        if (!dir.exists()) {
+            dir.mkdir();
+        }
+        File file = new File(dir, serverName);
+        InputStream inputStream = null;
+        OutputStream outputStream = null;
+        try {
+            inputStream = multipartFile.getInputStream();
+            outputStream = new FileOutputStream(file);
+            byte[] buffer = new byte[4096];
+            int temp = 0;
+            while ((temp = inputStream.read(buffer, 0, 4096)) != -1) {
+                outputStream.write(buffer, 0, temp);
+            }
+            inputStream.close();
+            outputStream.close();
+
+            String url = path + serverName;
+            return Result.success(url);
+
+        } catch (Exception exception) {
+            exception.printStackTrace();
+        }
+
+        return null;
+    }
+
+}

+ 2 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/pojo/Article.java

@@ -36,6 +36,8 @@ public class Article {
     private String productId;
     private String categoryNames;
 
+    private String coverImgUrl;
+
     @JsonFormat(pattern = "yyyy-MM-dd")
     @DateTimeFormat(pattern = "yyyy-MM-dd")
     private LocalDateTime createTime;//创建时间

+ 1 - 1
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/pojo/ArticleCoverImg.java

@@ -8,5 +8,5 @@ public class ArticleCoverImg {
 
     private Integer articleId;
 
-    private byte[] coverImgData;
+    private String coverImgData;
 }

+ 1 - 2
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/ArticleService.java

@@ -3,7 +3,6 @@ package com.my.bigevent.service;
 import com.my.bigevent.pojo.Article;
 import com.my.bigevent.pojo.PageBean;
 import com.my.bigevent.pojo.Result;
-import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
 
@@ -17,7 +16,7 @@ public interface ArticleService
 
     Article getArticleById(Integer id);
 
-    void insertOrUpdateArticle(String title, String categoryIds,String profile, String content, String state, MultipartFile coverImage,Integer id,String productId);
+    void insertOrUpdateArticle(String title, String categoryIds,String profile, String content, String state, String coverImage,Integer id,String productId);
 
     PageBean<Article> PageList(Integer pageIndex, Integer pageSize, String info);
 

+ 11 - 14
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/impl/ArticleServiceImpl.java

@@ -106,9 +106,9 @@ public class ArticleServiceImpl implements ArticleService
         if (!as.isEmpty()){
             for (Article a : as) {
                 ArticleCoverImg articleCoverImg= coverImgMapper.selectByArticleId(a.getId());
-                if(null!=articleCoverImg&&articleCoverImg.getCoverImgData()!=null){
-                    a.setCoverImg(articleCoverImg.getCoverImgData());
-                }
+//                if(null!=articleCoverImg&&articleCoverImg.getCoverImgData()!=null){
+//                    a.setCoverImg(articleCoverImg.getCoverImgData());
+//                }
 
                 if (a.getCategoryIds()!=null&&!a.getCategoryIds().isEmpty()) {
                     List<String> categoryIds = JSONObject.parseArray(a.getCategoryIds(), String.class);
@@ -138,7 +138,7 @@ public class ArticleServiceImpl implements ArticleService
 
     @Override
     @Transactional
-    public void insertOrUpdateArticle(String title, String categoryIds,String profile, String content, String state, MultipartFile coverImage, Integer id,String productId) {
+    public void insertOrUpdateArticle(String title, String categoryIds,String profile, String content, String state, String coverImage, Integer id,String productId) {
         Optional.ofNullable(title).orElseThrow(() -> new RuntimeException("请传标题"));
         Optional.ofNullable(content).orElseThrow(() -> new RuntimeException("请书写文章内容"));
         Optional.ofNullable(state).orElseThrow(() -> new RuntimeException("请传递发布类型"));
@@ -160,13 +160,15 @@ public class ArticleServiceImpl implements ArticleService
             article.setUpdateTime(LocalDateTime.now());
             article.setCreateUser(6);
             article.setProductId(productId);
+            article.setCoverImgUrl(coverImage);
 
             articleMapper.insert(article);
             log.info("文章的id{}", article.getId());
 
-            if (coverImage != null) {
-                handleCoverImage(coverImage, article.getId());
-            }
+//            if (coverImage != null) {
+////                handleCoverImage(coverImage, article.getId());
+//
+//            }
         }
         // 修改
         else {
@@ -182,14 +184,9 @@ public class ArticleServiceImpl implements ArticleService
             article.setUpdateTime(LocalDateTime.now());
             article.setId(id);
             article.setProductId(productId);
+            article.setCoverImgUrl(coverImage);
             articleMapper.update(article);
 
-            if (coverImage != null) {
-                coverImgMapper.deleteByArticleId(id);
-                handleCoverImage(coverImage, article.getId());
-            } else {
-                coverImgMapper.deleteByArticleId(id);
-            }
         }
     }
 
@@ -250,7 +247,7 @@ public class ArticleServiceImpl implements ArticleService
             byte[] bytes = coverImage.getBytes();
             ArticleCoverImg articleCoverImg = new ArticleCoverImg();
             articleCoverImg.setArticleId(articleId);
-            articleCoverImg.setCoverImgData(bytes);
+//            articleCoverImg.setCoverImgData(bytes);
             coverImgMapper.insert(articleCoverImg);
         } catch (IOException e) {
             throw new RuntimeException("保存封面图片失败", e);

+ 9 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/resources/com/my/bigevent/mapper/ArticleMapper.xml

@@ -34,6 +34,9 @@
             <if test=" productId!=null and productId!=''">
                 product_id,
             </if>
+            <if test=" coverImgUrl!=null and coverImgUrl!=''">
+                cover_img_url,
+            </if>
         </trim>
         <trim prefix=" values (" suffix=")" suffixOverrides=",">
             <if test="title !=null and title !=''">
@@ -63,6 +66,9 @@
             <if test=" productId!=null and productId!=''">
                 #{productId},
             </if>
+            <if test=" coverImgUrl!=null and coverImgUrl!=''">
+                #{coverImgUrl},
+            </if>
         </trim>
     </insert>
     <update id="update">
@@ -89,6 +95,9 @@
                 <if test=" productId!=null and productId!=''">
                     product_id=#{productId},
                 </if>
+                <if test=" coverImgUrl!=null and coverImgUrl!=''">
+                    cover_img_url=#{coverImgUrl},
+                </if>
             </set>
         where id=#{id}
     </update>

+ 4 - 0
fhKeeper/formulahousekeeper/webttkuaiban/src/main/java/com/firerock/webttkuaiban/demos/controller/ArticleTemplateController.java

@@ -3,6 +3,8 @@ package com.firerock.webttkuaiban.demos.controller;
 import com.firerock.webttkuaiban.demos.pojo.Article;
 import com.firerock.webttkuaiban.demos.service.ArticleService;
 import org.apache.tomcat.util.codec.binary.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
@@ -21,6 +23,7 @@ import java.util.List;
 @RequestMapping("articleTemplate")
 public class ArticleTemplateController {
 
+    private static final Logger log = LoggerFactory.getLogger(ArticleTemplateController.class);
     @Autowired
     ArticleService articleService;
 
@@ -65,6 +68,7 @@ public class ArticleTemplateController {
         // 定义格式化器
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
+        long l = System.currentTimeMillis();
         List<Article> latestList =articleService.latestList();
         if (!latestList.isEmpty()){
             for (Article article : latestList) {

+ 60 - 0
fhKeeper/formulahousekeeper/webttkuaiban/src/main/java/com/firerock/webttkuaiban/demos/controller/CommonUploadController.java

@@ -0,0 +1,60 @@
+package com.firerock.webttkuaiban.demos.controller;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.List;
+import java.util.UUID;
+
+@RestController
+@RequestMapping("/common")
+public class CommonUploadController {
+
+
+    @Value(value = "${upload.path}")
+    private String path;
+
+
+    @RequestMapping(value="uploadFile")
+    public Object uploadFile(MultipartFile multipartFile) {
+
+        //然后处理文件
+        String fileName = multipartFile.getOriginalFilename();
+        String[] split = fileName.split("\\.");
+        String serverName = UUID.randomUUID().toString().replaceAll("-", "") + "."+split[split.length-1];
+
+        //检查目录
+        File dir = new File(path);
+        if (!dir.exists()) {
+            dir.mkdir();
+        }
+        File file = new File(dir, serverName);
+        InputStream inputStream = null;
+        OutputStream outputStream = null;
+        try {
+            inputStream = multipartFile.getInputStream();
+            outputStream = new FileOutputStream(file);
+            byte[] buffer = new byte[4096];
+            int temp = 0;
+            while ((temp = inputStream.read(buffer, 0, 4096)) != -1) {
+                outputStream.write(buffer, 0, temp);
+            }
+            inputStream.close();
+            outputStream.close();
+
+            return serverName;
+
+        } catch (Exception exception) {
+            exception.printStackTrace();
+        }
+
+        return null;
+    }
+
+}

+ 3 - 1
fhKeeper/formulahousekeeper/webttkuaiban/src/main/java/com/firerock/webttkuaiban/demos/pojo/ArticleCoverImg.java

@@ -8,5 +8,7 @@ public class ArticleCoverImg {
 
     private Integer articleId;
 
-    private byte[] coverImgData;
+//    private byte[] coverImgData;
+
+    private String coverImgData;
 }