用Wget进行下载

时间:2023-12-19 23:13:56

wget下载可以不用考虑资源的双向认证的问题,即

wget -N -S -O " + filePath + " " + urlStr + " --no-check-certificate

所以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;
}