您的位置:首页 > 编程语言 > Java开发

JAVA读取文件夹中CSV的URL并下载图片

2015-12-11 15:56 781 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: