java按http地址列表下載檔案佇列
//http檔案佇列下載
public void httplistDownload() throws IOException{
String path=ServletActionContext.getRequest().getParameter("path");
//返回ts切片地址列表
URL stSpaceUrl = new URL(path);
HttpURLConnection storageconn = (HttpURLConnection) stSpaceUrl.openConnection();
storageconn.connect();
BufferedReader storageReader = new BufferedReader(new InputStreamReader(storageconn.getInputStream()));
String httpresponse=storageReader.readLine();
httpresponse=httpresponse.substring(1,httpresponse.lastIndexOf("]"));
String[] urlarray=httpresponse.split(", ");
int BUFFER_SIZE= 10240; //緩衝區大小10Kb
String strurl=null;
String filename=null;
String filepath=null;
Vector<String> vdownload=new Vector<String>(); //url列表
Vector<String> vfilename=new Vector<String>(); //檔名列表
for(int i=0;i<urlarray.length;i++){
filepath=java.net.URLDecoder.decode(urlarray[i], "utf-8");
filename=filepath.substring(filepath.lastIndexOf("/")+1);
vdownload.add(filepath); //解碼之後,新增到url列表
vfilename.add(filename); //檔名新增到列表
}
//按列表順序儲存資源
for(int i=0;i<vdownload.size();i++)
{
strurl=(String)vdownload.get(i); //從列表中取出url
filename=(String)vfilename.get(i); //從列表中取出檔名
FileOutputStream fos=null;
BufferedInputStream bis=null;
HttpURLConnection httpUrl=null;
URL url=null;
byte[] buffer=new byte[BUFFER_SIZE];
int size=0;
//建立連線
url=new URL(strurl);
httpUrl=(HttpURLConnection)url.openConnection();
//連線指定的資源
httpUrl.connect();
//獲取網路輸入流
bis=new BufferedInputStream(httpUrl.getInputStream());
//建立檔案
fos=new FileOutputStream("E:\\download\\"+filename);
//儲存檔案
while((size=bis.read(buffer))!=-1)
{
fos.write(buffer,0,size);
}
fos.close();
bis.close();
httpUrl.disconnect();
}
}