package phantancy.doujin.io.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import phantancy.doujin.common.Log;

public class DownloadPic {

    private static Log log = new Log(DownloadPic.class);
    
    private static final boolean overrideFile = false;

    // TODO 下载失败删除当前文件
    public void downloadPicByUrlList(List<String> picUrlList, String folderName) {
        log.writeLog("Get " + picUrlList.size() + " image urls.\n Download Start...");
        // TODO manage file path in conf file
        File folder = new File(folderName);
        if (!folder.exists() || !folder.isDirectory()) {
            folder.mkdir();
        }
        log.writeLog("Folder Path: " + folder.getAbsolutePath());

        int number = 0;
        for (String imageUrl : picUrlList) {
            number++;
            // Get image name
            String[] urlSplit = imageUrl.split("/");
            String imageName = urlSplit[urlSplit.length - 1];
            log.writeLog("downloading picture [" + number + "/" + picUrlList.size() + "] ：" + imageName);
            // Duplicate image ignore
            File imageFile = new File(folder.getAbsolutePath() + "\\" + imageName);
            if (imageFile.exists()) {
                log.writeLog(imageName + " exist.");
                if (overrideFile) {
                    imageFile.delete();
                }
                continue;
            } else {
                try {
                    imageFile.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            // Download
            int times = 0;
            while (true) {
                times++;
                InputStream is = null;
                OutputStream os = null;
                try {
                    // 构造URL
                    URL url = new URL(imageUrl);
                    // 打开连接
                    URLConnection con = url.openConnection();
                    // 设置请求超时为5s
                    con.setConnectTimeout(500);
                    // 输入流
                    is = con.getInputStream();

                    // 1K的数据缓冲
                    byte[] bs = new byte[1024];
                    // 读取到的数据长度
                    int len;
                    // 输出的文件流
                    os = new FileOutputStream(imageFile);
                    // 开始读取
                    while ((len = is.read(bs)) != -1) {
                        os.write(bs, 0, len);
                    }
                    times += 3;
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    // 完毕，关闭所有链接
                    try {
                        os.close();
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (times > 3) {
                    break;
                }
            }
        }
    }
}

@@ -677,29 +677,101 @@
     
-File folder = new Fil
+String rootPath = %22D:%5C%5C%22;%0A        File folder = new File(rootPath + %22%5C%5C%22 + getEnableFolderNam
 e(fo
@@ -775,24 +775,25 @@
 (folderName)
+)
 ;%0A        if
@@ -2329,16 +2329,17 @@
 eout(500
+0
 );%0A     
@@ -2780,32 +2780,96 @@
               %7D%0A
+                    os.close();%0A                    is.close();%0A
                 
@@ -2986,18 +2986,8 @@
    %7D
- finally %7B
 %0A   
@@ -3003,182 +3003,92 @@
     
-    // %E5%AE%8C%E6%AF%95%EF%BC%8C%E5%85%B3%E9%97%AD%E6%89%80%E6%9C%89%E9%93%BE%E6%8E%A5%0A                    try %7B%0A                        os.close();%0A                        is.close();%0A                    %7D catch (IOException e) %7B%0A     
+if (times %3E 3) %7B%0A                    break;%0A                %7D%0A            %7D%0A
         
     
@@ -3087,181 +3087,134 @@
     
- 
+%7D%0A
     
+%7D%0A%0A
     
-  e.printStackTrace();%0A                    %7D%0A                %7D%0A                if (times %3E 3) %7B%0A                    break;%0A                %7D%0A            %7D%0A        %7D
+private String getEnableFolderName(String folderName) %7B%0A        return folderName.replaceAll(%22%5B/%5C%5C%5C%5C:*?%3C%3E%7C%5D%22,%22%22);
 %0A   

fix invalide folder name