|
|
@@ -0,0 +1,93 @@
|
|
|
+package com.miekir.shibei.tool.web;
|
|
|
+
|
|
|
+import com.miekir.shibei.tool.TextUtils;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.File;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.URL;
|
|
|
+
|
|
|
+/*#手动保存的图片(Nginx)
|
|
|
+location /manual/ {
|
|
|
+alias /file/images/eden/manual/;
|
|
|
+}*/
|
|
|
+public class Sex8Tool {
|
|
|
+ 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 static final String HOST_FORMAT = "http://sohumayun.space/forum-96-%s.html";
|
|
|
+
|
|
|
+ private Sex8Tool() {}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 爬虫自动保存图片
|
|
|
+ * @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 + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ URL url = new URL(imageUrl);
|
|
|
+ BufferedImage img = ImageIO.read(url);
|
|
|
+ File folder = new File(PATH_AUTO_FOLDER);
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ ImageIO.write(img, format, file);
|
|
|
+ return String.format(IMAGE_URL_FORMAT, String.valueOf(futureFileCount), format);
|
|
|
+ } catch (Exception e) {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|