wget下載能夠不用考慮資源的雙向認證的問題,即java
wget -N -S -O " + filePath + " " + urlStr + " --no-check-certificateurl
因此java自帶的下載方式可能不是全部的資源都能下載,而wget能夠,而且wget支持斷點續傳,默認是20次重複的下載機會。而且下載異常穩定。資源
public long download(String urlStr, String filePath){
String wget = "wget -N -S -O " + filePath + " " + urlStr + " --no-check-certificate";
log.info("----------執行語句---------" + wget);
System.out.println(wget);
Runtime r = Runtime.getRuntime();
log.info("-------------------------------urlStr------cbj------1" + urlStr);
Process p = null;
long fileLength = -1;
boolean flag = false;
try {
p = r.exec(wget);
final InputStream is1 = p.getInputStream();
final InputStream is2 = p.getErrorStream();
BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
String line2 = null;
while ((line2 = br2.readLine()) != null) {
System.out.println("錯誤流" + line2);
if(line2.indexOf("200 OK") != -1){
flag = true;
}
if(line2.indexOf("Content-Length") != -1 && flag){
fileLength = NumberUtils.toLong(line2.split(":")[1].trim());
break;
}
}
Task task = new Task(is2);
executor.submit(task);
return fileLength;
}catch(Exception e){
e.printStackTrace();
}
return fileLength;
}get