Ver Fonte

在win上抓图片之前的准备

詹子聪 há 5 anos atrás
pai
commit
b1aed4fb6e

+ 5 - 4
src/main/java/com/miekir/shibei/tool/web/Sex8Tool.java

@@ -1,5 +1,6 @@
 package com.miekir.shibei.tool.web;
 
+import com.sun.org.slf4j.internal.Logger;
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
@@ -12,6 +13,7 @@ import java.net.HttpURLConnection;
 import java.net.ProtocolException;
 import java.net.URL;
 import java.util.Random;
+import java.util.logging.Level;
 import java.util.zip.GZIPInputStream;
 
 public class Sex8Tool {
@@ -181,11 +183,10 @@ public class Sex8Tool {
     }
 
     private static void justWait() {
-        // todo
-        int waitMinute = new Random().nextInt(40) + 20;
+        int waitMinute = new Random().nextInt(4) + 1;
         try {
-            //Thread.sleep(waitMinute * 1000);
-            Thread.sleep(1000);
+            Thread.sleep(waitMinute * 1000);
+            //Thread.sleep(5000);
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 20 - 6
src/main/java/com/miekir/shibei/tool/web/WebImageTool.java

@@ -1,6 +1,7 @@
 package com.miekir.shibei.tool.web;
 
 import com.miekir.shibei.tool.TextUtils;
+import sun.net.NetworkClient;
 
 import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
@@ -18,6 +19,8 @@ sudo chmod 777 -R /file/images/
 恢复默认权限
 sudo chmod 755 /file/images/*/
 public class WebImageTool {
+    // 不能小于100KB
+    private static final int FILE_SIZE_LIMIT = 100 * 1024;
     // 只要授权之后,是可以直接写这个路径的,但是由于jar包等冲突(或缺失),会导致war包发布的时候,没有真正运行,也就没有文件写进去了。
     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";
@@ -44,8 +47,8 @@ public class WebImageTool {
             format = imageUrl.substring(formatIndex);
         }
 
-        File folder = new File(PATH_AUTO_FOLDER);
-        //File folder = new File("D:\\");
+        //File folder = new File(PATH_AUTO_FOLDER);
+        File folder = new File("F:\\auto");
         if (!folder.exists()) {
             folder.mkdirs();
         }
@@ -76,7 +79,8 @@ public class WebImageTool {
                 // this must be called before 'getErrorStream()' works
                 input = connection.getInputStream();
             } else {
-                input = connection.getErrorStream();
+                return null;
+                //input = connection.getErrorStream();
             }
 
             in = new BufferedInputStream(input);
@@ -86,6 +90,9 @@ public class WebImageTool {
             while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                 fileOutputStream.write(dataBuffer, 0, bytesRead);
             }
+            if (file.length()  < FILE_SIZE_LIMIT) {
+                return null;
+            }
             return String.format(IMAGE_URL_FORMAT, String.valueOf(futureFileCount), format);
         } catch (IOException e) {
             // handle exception
@@ -99,16 +106,23 @@ public class WebImageTool {
                 }
             }
             if (fileOutputStream != null) {
-                try {
-                    fileOutputStream.close();
+                try {fileOutputStream.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
+
+            // 去除太小的文件
+            if (file.length()  < FILE_SIZE_LIMIT) {
+                file.delete();
+            }
         }
         return null;
     }
 
+    /**
+     * JAVA获取文件目录的文件数
+     */
     public static int getFilesCount(File folderFile) {
         if (folderFile == null) {
             return 0;
@@ -123,7 +137,7 @@ public class WebImageTool {
     }
 
     /**
-     * 获取文件目录的文件数
+     * 命令行获取文件目录的文件数
      * @param commandStr
      */
     public static int getFilesCount(String commandStr) {