1. 程式人生 > >JAVA讀取資料夾中CSV的URL並下載圖片

JAVA讀取資料夾中CSV的URL並下載圖片

package com.ross.httpdownload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.ross.httpdownload.util.SysValue;

public class ReadFile {
	public ReadFile() {
	}
	public static boolean readfile(String filepath)
			throws FileNotFoundException, IOException {
		try {
			File file = new File(filepath);
			if (!file.isDirectory()) {
				System.out.println(file.getPath());
				readcsv(file);
			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = filelist.length - 1; i >= 0; i--) {
					File readfile = new File(filepath + "\\" + filelist[i]);
					System.out.println(readfile.getPath());
					if (!readfile.isDirectory()) {
						readcsv(readfile);
					} else if (readfile.isDirectory()) {
						readfile(filepath + "\\" + filelist[i]);
					}
				}
			}

		} catch (FileNotFoundException e) {
			System.out.println("readfile()   Exception:" + e.getMessage());
		}
		return true;
	}

	public static void readcsv(File f) {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(f), "GB2312"));
			String line = null;
			List<String> ls1=new ArrayList<String>();
			List<String> ls2=new ArrayList<String>();
			while ((line = reader.readLine()) != null) {
				String item[] = line.split(",");
				String url=parseUrl(item[1]);
				String filename=parseSku(item[0]);
				ls1.add(url);
				ls2.add(filename);
			}
			int threadnum=10;
			ExecutorService fix=Executors.newFixedThreadPool(threadnum);
			ArrayList<Callable<Integer>> tasks=new ArrayList<Callable<Integer>>();
			for(int i=0;i<ls1.size();i++)
			{
				final int index=i;
				tasks.add(new Callable<Integer>() {
					public Integer call() throws Exception{
						makeImage(ls1.get(index), ls2.get(index));
						return null;
					}
				});
			}
			fix.invokeAll(tasks);
			fix.shutdown();
	        System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
	                .format(new Date()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static String parseUrl(String url)
	{
		String sFileURL = url;
        for(int i=0;i<sFileURL.length();i++)
        {
            if(sFileURL.charAt(i)=='/'&&sFileURL.charAt(i+1)=='n'&&sFileURL.charAt(i+2)=='5'&&sFileURL.charAt(i+3)=='/')
            {
                sFileURL=sFileURL.substring(0,i+1)+"popWaterMark"+sFileURL.substring(i+3,sFileURL.length());
                break;
            }
        }
        return sFileURL;
	}
	public static String parseSku(String SkuId){
		String sFileName = "";
        for(int i=1;;i++){
            sFileName=SkuId+"_"+String.valueOf(i)+".jpg";
            File te=new File(sFileName);
            if(!te.exists()){
                break;
            }
        }
        return sFileName;
	}
	public static void makeImage(String urls, String filePath) throws IOException { 
        // 網路請求所需變數 
		int flag=0;
		do{
	        try { 
	        	URL url = new URL(urls);
	            // 開啟連線
	            URLConnection con = url.openConnection();
	            // 設定請求超時為5s
	            con.setConnectTimeout(5 * 1000);
	            // 輸入流
	            InputStream is = con.getInputStream();
	     
	            // 1K的資料緩衝
	            byte[] bs = new byte[1024];
	            int len;
	     
	            OutputStream os = new FileOutputStream(SysValue.Save_As_Path+filePath);
	            // 開始讀取
	            while ((len = is.read(bs)) != -1) {
	                os.write(bs, 0, len);
	            }
	            // 完畢,關閉所有連結
	            os.close();
	            is.close();
	            System.out.println("正在執行下載任務:當前正在下載圖片" + filePath); 
	            flag=1;
	        } catch (MalformedURLException e) { 
	            flag=0;
	        } catch (IOException e) { 
	            flag=0;
	        } 
		}while(flag==0);
    }
	public static String formatString(String s) {
		if (s != null) {
			s = s.replaceAll("\ufeff", "");
		}
		return s;
	}
}