ソースを参照

自动保存图片

詹子聪 5 年 前
コミット
72162ae556

+ 5 - 1
pom.xml

@@ -130,7 +130,11 @@
             <artifactId>jackson-databind</artifactId>
             <version>2.1.4</version>
         </dependency>
-
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot</artifactId>
+            <version>2.1.0.RELEASE</version>
+        </dependency>
 
 
     </dependencies>

+ 1 - 0
src/main/java/com/miekir/shibei/controller/api/JsonController.java

@@ -9,6 +9,7 @@ import com.miekir.shibei.repository.JsonRepository;
 import com.miekir.shibei.repository.SystemRepository;
 import com.miekir.shibei.repository.UserRepository;
 import com.miekir.shibei.tool.*;
+import com.miekir.shibei.tool.web.WebTool;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpHeaders;
 import org.springframework.stereotype.Controller;

+ 43 - 0
src/main/java/com/miekir/shibei/controller/task/OnStartServer.java

@@ -0,0 +1,43 @@
+package com.miekir.shibei.controller.task;
+
+import com.miekir.shibei.tool.web.Sex8Tool;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.boot.context.event.ApplicationReadyEvent;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.EventListener;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.Arrays;
+import java.util.concurrent.Executors;
+
+/**
+ * spring mvc web应用启动时就执行特定处理
+ *
+ */
+@Component
+public class OnStartServer {
+
+    private static boolean mStarted = false;
+    @PostConstruct
+    public void init() {
+        // 执行一次
+        // 一启动完成就开始去爬虫
+        if (OnStartServer.mStarted) {
+            return;
+        }
+        OnStartServer.mStarted = true;
+        Executors.newSingleThreadExecutor().submit(new Runnable() {
+            @Override
+            public void run() {
+                // 递归会导致StackOverflowError,所以用循环代替
+                boolean shouldContinue = true;
+                while (shouldContinue) {
+                    shouldContinue = Sex8Tool.startGettingData();
+                }
+            }
+        });
+    }
+}

ファイルの差分が大きいため隠しています
+ 159 - 60
src/main/java/com/miekir/shibei/tool/web/Sex8Tool.java


+ 134 - 0
src/main/java/com/miekir/shibei/tool/web/WebImageTool.java

@@ -0,0 +1,134 @@
+package com.miekir.shibei.tool.web;
+
+import com.miekir.shibei.tool.TextUtils;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+/*#手动保存的图片(Nginx)
+location /manual/ {
+alias /file/images/eden/manual/;
+}*/
+public class WebImageTool {
+    private static final String PATH_AUTO_FOLDER = "/file/images/eden/auto/";
+    private static final String CMD_GET_AUTO_FILE_COUNT = "ls -l " + PATH_AUTO_FOLDER + " | grep \"^-\"|wc -l";
+    private static final String IMAGE_FORMAT = ".jpg";
+    private static final String IMAGE_URL_FORMAT = "http://jianjie.life/auto/%s.%s";
+
+    private WebImageTool() {
+    }
+
+    /**
+     * 爬虫自动保存图片
+     *
+     * @param imageUrl
+     * @return
+     */
+    public static String autoSaveImage(String imageUrl) {
+        if (TextUtils.isEmpty(imageUrl)) {
+            return null;
+        }
+
+        String format = IMAGE_FORMAT;
+        int formatIndex = imageUrl.lastIndexOf(".");
+        if (formatIndex != -1 && formatIndex < imageUrl.length() - 1) {
+            format = imageUrl.substring(formatIndex);
+        }
+
+        File folder = new File(PATH_AUTO_FOLDER);
+        //File folder = new File("D:\\");
+        if (!folder.exists()) {
+            folder.mkdirs();
+        }
+
+        // 获取下一文件名
+        int futureFileCount = getFilesCount(CMD_GET_AUTO_FILE_COUNT) + 1;
+        File file = new File(folder, String.valueOf(futureFileCount) + format);
+        if (file.exists()) {
+            file.delete();
+        }
+
+        /*try (InputStream in = new URL(imageUrl).openStream();) {
+            Files.copy(in, Paths.get(file.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
+        } catch (Exception e) {
+            // handle exception
+        }*/
+
+
+        BufferedInputStream in = null;
+        FileOutputStream fileOutputStream = null;
+        try {
+            // 为了避免403
+            HttpURLConnection connection = ((HttpURLConnection)new URL(imageUrl).openConnection());
+            connection.addRequestProperty("User-Agent", "Mozilla/4.0");
+            InputStream input;
+            if (connection.getResponseCode() == 200) {
+                // this must be called before 'getErrorStream()' works
+                input = connection.getInputStream();
+            } else {
+                input = connection.getErrorStream();
+            }
+
+            in = new BufferedInputStream(input);
+            fileOutputStream = new FileOutputStream(file);
+            byte[] dataBuffer = new byte[1024];
+            int bytesRead;
+            while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
+                fileOutputStream.write(dataBuffer, 0, bytesRead);
+            }
+            return String.format(IMAGE_URL_FORMAT, String.valueOf(futureFileCount), format);
+        } catch (IOException e) {
+            // handle exception
+            e.printStackTrace();
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (fileOutputStream != null) {
+                try {
+                    fileOutputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return null;
+    }
+
+
+    /**
+     * 获取文件目录的文件数
+     * @param commandStr
+     */
+    public static int getFilesCount(String commandStr) {
+        BufferedReader br = null;
+        try {
+            Process p = Runtime.getRuntime().exec(commandStr);
+            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
+            String line = null;
+            StringBuilder sb = new StringBuilder();
+            while ((line = br.readLine()) != null) {
+                sb.append(line + "\n");
+            }
+            return Integer.parseInt(br.readLine());
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (br != null) {
+                try {
+                    br.close();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return 0;
+    }
+}

+ 2 - 1
src/main/java/com/miekir/shibei/tool/WebTool.java

@@ -1,4 +1,4 @@
-package com.miekir.shibei.tool;
+package com.miekir.shibei.tool.web;
 
 import java.net.HttpURLConnection;
 import java.net.ProtocolException;
@@ -6,6 +6,7 @@ import java.net.URL;
 
 import com.miekir.shibei.bean.WeatherBean;
 import com.miekir.shibei.bean.YijiBean;
+import com.miekir.shibei.tool.TextUtils;
 import org.jsoup.Connection;
 import org.jsoup.Jsoup;
 import org.jsoup.helper.HttpConnection;

+ 3 - 1
src/main/webapp/html/index.jsp

@@ -12,7 +12,9 @@
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">
-  <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
+  <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后!
+  记得修改Edit Configurations... -> Tomcat Server -> eden -> Deployment -> Application context为/eden
+  否则找不到页面-->
   <title>SpringMVC Demo 首页</title>
 
   <!-- 新 Bootstrap 核心 CSS 文件 -->