shiyj
2019-09-02 b784175e978b0fcfca8ca8bed7953ae4eacf49e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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;
    }
}