Преглед изворни кода

加入照片批量下载的功能

wuxw пре 6 година
родитељ
комит
fc0f02da75

+ 96 - 0
Api/src/main/java/com/java110/api/rest/GetPhotoByInst.java

@@ -0,0 +1,96 @@
+package com.java110.api.rest;
+
+import org.apache.commons.io.FileUtils;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.util.StringUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URL;
+
+/**
+ * @ClassName GetPhotoByInst
+ * @Description TODO
+ * @Author wuxw
+ * @Date 2019/8/28 22:49
+ * @Version 1.0
+ * add by wuxw 2019/8/28
+ **/
+public class GetPhotoByInst {
+
+    final static String PHOTO_INFO_PATH= "photoInfo.txt";
+    final static String DEFAULTE_PHOTO_DIR= "./Api/photos/";
+
+    public static void main(String[] args) {
+        //读取文件
+
+
+
+        Reader reader = null;
+        String sb = "";
+        try {
+            InputStream inputStream = new ClassPathResource(PHOTO_INFO_PATH).getInputStream();
+            //InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(File.separator + filePath);
+            reader = new InputStreamReader(inputStream, "UTF-8");
+            int tempChar;
+            StringBuffer b = new StringBuffer();
+            while ((tempChar = reader.read()) != -1) {
+                b.append((char) tempChar);
+            }
+            sb = b.toString();
+
+            String[] strs = sb.split("\n");
+
+            for(String str : strs){
+                dealPhoto(str);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+    private static void dealPhoto(String str) {
+
+        String[] tmpPhotoPaths = str.split("\\|");
+
+        String instId = tmpPhotoPaths[0];
+        String photoUrl = tmpPhotoPaths[1];
+
+        downloadFromUrl(photoUrl,DEFAULTE_PHOTO_DIR,"qhdx_"+instId+"_47.jpg");
+    }
+
+
+    /**
+     * 文件下载的方法
+     * @param  url 地址
+     * @param  dir 目录
+     * @return String fileName
+     */
+    public static String downloadFromUrl(String url, String dir,String fileName) {
+
+        try {
+            URL httpurl = new URL(url);
+            //	fileName = getFileNameFromUrl(url);
+            //String[] us=url.split("/");
+            //fileName=us[us.length-1];
+            System.out.println("fileName:"+fileName);
+            File f = new File(dir + fileName);
+            FileUtils.copyURLToFile(httpurl, f);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "Fault!";
+        }
+        return fileName;
+    }
+}

+ 10 - 0
Api/src/main/resources/photoInfo.txt

@@ -0,0 +1,10 @@
+1231231223|https://photo.zastatic.com/images/photo/11751/47002203/1343913361860.jpg?scrop=1&crop=1&cpos=north&w=200&h=200
+12321313213|https://photo.zastatic.com/images/photo/11751/47002203/1343913361860.jpg?scrop=1&crop=1&cpos=north&w=200&h=200
+213123123213|https://photo.zastatic.com/images/photo/11751/47002203/1343913361860.jpg?scrop=1&crop=1&cpos=north&w=200&h=200
+343454353545|https://photo.zastatic.com/images/photo/11751/47002203/1343913361860.jpg?scrop=1&crop=1&cpos=north&w=200&h=200
+345345353535|https://photo.zastatic.com/images/photo/11751/47002203/1343913361860.jpg?scrop=1&crop=1&cpos=north&w=200&h=200
+3452324666|https://photo.zastatic.com/images/photo/11751/47002203/1343913361860.jpg?scrop=1&crop=1&cpos=north&w=200&h=200
+7767866555|https://photo.zastatic.com/images/photo/11751/47002203/1343913361860.jpg?scrop=1&crop=1&cpos=north&w=200&h=200
+7890876543333|https://photo.zastatic.com/images/photo/11751/47002203/1343913361860.jpg?scrop=1&crop=1&cpos=north&w=200&h=200
+45645676876|https://photo.zastatic.com/images/photo/11751/47002203/1343913361860.jpg?scrop=1&crop=1&cpos=north&w=200&h=200
+243456576765|https://photo.zastatic.com/images/photo/11751/47002203/1343913361860.jpg?scrop=1&crop=1&cpos=north&w=200&h=200