Browse Source

图片修复

chao 4 năm trước cách đây
mục cha
commit
53a2e4e1c0

+ 6 - 0
pom.xml

@@ -133,6 +133,12 @@
             <version>2.8.0</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.jsoup</groupId>
+            <artifactId>jsoup</artifactId>
+            <version>1.11.2</version>
+        </dependency>
+
     </dependencies>
 
 

+ 15 - 1
src/main/java/com/caimei/www/controller/GenerateApi.java

@@ -1,11 +1,16 @@
 package com.caimei.www.controller;
 
+import com.caimei.www.mapper.ProductDao;
+import com.caimei.www.pojo.JsonModel;
 import com.caimei.www.service.generate.GenerateHtml;
+import com.caimei.www.utils.GetProductImgUtil;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.server.ServerWebExchange;
 
 import javax.annotation.Resource;
+import java.util.List;
 
 /**
  * Description
@@ -17,7 +22,8 @@ import javax.annotation.Resource;
 public class GenerateApi {
     @Resource
     private GenerateHtml generateHtml;
-
+    @Resource
+    private ProductDao productDao;
     /**
      * 生成静态首页
      */
@@ -34,4 +40,12 @@ public class GenerateApi {
         return generateHtml.generateStaticFiles();
     }
 
+
+//    @PostMapping("/product/img/repair")
+//    public String repairProductImg() {
+//        List<String> infoList = productDao.getProductInfo();
+//        infoList.forEach(GetProductImgUtil::saveProductImg);
+//        return "商品数量:" + infoList.size() + ", " + infoList.toString();
+//    }
+
 }

+ 4 - 0
src/main/java/com/caimei/www/mapper/ProductDao.java

@@ -67,4 +67,8 @@ public interface ProductDao {
     List<Bigtype> findBigtype();
 
     List<SmallType> findSmalltype();
+    /**
+     * 修复商品图片(临时)
+     */
+    List<String> getProductInfo();
 }

+ 43 - 4
src/main/java/com/caimei/www/service/generate/impl/GenerateHtmlImpl.java

@@ -19,10 +19,9 @@ import org.thymeleaf.spring5.context.webflux.SpringWebFluxContext;
 import org.thymeleaf.util.StringUtils;
 
 import javax.annotation.Resource;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintWriter;
+import java.io.*;
+import java.net.URL;
+import java.net.URLConnection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -224,4 +223,44 @@ public class GenerateHtmlImpl implements GenerateHtml {
         }
     }
 
+
+
+    public static void download(String urlString, String filename,String savePath) throws Exception {
+        // 构造URL
+        URL url = new URL(urlString);
+        // 打开连接
+        URLConnection con = url.openConnection();
+        //设置请求超时为5s
+        con.setConnectTimeout(5*1000);
+        // 输入流
+        InputStream is = con.getInputStream();
+        // 1K的数据缓冲
+        byte[] bs = new byte[1024];
+        // 读取到的数据长度
+        int len;
+        // 输出的文件流
+       File sf=new File(savePath);
+       if(!sf.exists()){
+           sf.mkdirs();
+       }
+       OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);
+        // 开始读取
+        while ((len = is.read(bs)) != -1) {
+          os.write(bs, 0, len);
+        }
+        // 完毕,关闭所有链接
+        os.close();
+        is.close();
+    }
+
+
+
+
+
+
+
+
+
+
+
 }

+ 86 - 0
src/main/java/com/caimei/www/utils/GetProductImgUtil.java

@@ -0,0 +1,86 @@
+package com.caimei.www.utils;
+
+import lombok.extern.slf4j.Slf4j;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2021/5/28
+ */
+@Slf4j
+public class GetProductImgUtil {
+
+    /**
+     * 保存图片到本地服务器
+     * @param textBody 富文本
+     */
+    public static void saveProductImg(String textBody) {
+        // 解析富文本
+        Element doc = Jsoup.parseBodyFragment(textBody).body();
+        Elements images = doc.select("img[src]");
+        List<String> srcList = new ArrayList<>();
+        for (Element element : images) {
+            String imgUrl = element.attr("src");
+            // 筛选测试服务器的图片路径
+            if (imgUrl.indexOf("img-b.caimei365.com") > 0) {
+                srcList.add(imgUrl);
+            }
+        }
+        // 本地存放路径
+        String basePath = "D:/mnt/newdatadrive/fdfs/storage/data/";
+        srcList.forEach(img -> {
+            try {
+                // 下载图片,并根据路径规律保持原有路径
+                GetProductImgUtil.downloadImage(img, img.substring(45), basePath+img.substring(39, 45));
+            } catch (Exception e) {
+                log.error("try-catch:",e);
+            }
+        });
+    }
+
+    /**
+     * 下载图片
+     *
+     * @param urlString 图片链接
+     * @param filename  图片名称
+     * @param savePath  保存路径
+     */
+    private static void downloadImage(String urlString, String filename, String savePath) throws Exception {
+        // 构造URL打开连接,并设置输入流与缓冲
+        URL url = new URL(urlString);
+        URLConnection con = url.openConnection();
+        con.setConnectTimeout(5*1000);
+        InputStream is = con.getInputStream();
+        byte[] bs = new byte[1024];
+        // 读取到的数据长度
+        int len;
+        // 输出的文件流
+        File sf=new File(savePath);
+        if(!sf.exists()){
+           sf.mkdirs();
+        }
+        OutputStream os = new FileOutputStream(sf.getPath()+"/"+filename);
+        // 开始读取
+        while ((len = is.read(bs)) != -1) {
+            os.write(bs, 0, len);
+        }
+        os.close();
+        is.close();
+        // 打印图片链接
+        log.info(urlString);
+    }
+
+}

+ 3 - 0
src/main/resources/mapper/ProductMapper.xml

@@ -117,6 +117,9 @@
 	<select id="findSmalltype" resultType="com.caimei.www.pojo.classify.SmallType">
 		SELECT smallTypeID,name FROM smalltype ORDER BY sortIndex
 	</select>
+    <select id="getProductInfo" resultType="java.lang.String">
+        select detailinfo from productdetailinfo where detailinfo like '%img-b.caimei365.com%'
+	</select>
 
 
 </mapper>