|
@@ -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;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|