更加好看的日志输出
parent
aa93d2d6d6
commit
8255010524
|
@ -1,5 +1,6 @@
|
||||||
package top.dreamcenter.webdav;
|
package top.dreamcenter.webdav;
|
||||||
|
|
||||||
|
import top.dreamcenter.webdav.bean.SendResult;
|
||||||
import top.dreamcenter.webdav.prop.WebDavConfig;
|
import top.dreamcenter.webdav.prop.WebDavConfig;
|
||||||
import top.dreamcenter.webdav.util.ProjectUtil;
|
import top.dreamcenter.webdav.util.ProjectUtil;
|
||||||
import top.dreamcenter.webdav.util.WebDavUtil;
|
import top.dreamcenter.webdav.util.WebDavUtil;
|
||||||
|
@ -7,6 +8,10 @@ import top.dreamcenter.webdav.util.WebDavUtil;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -27,19 +32,46 @@ public class Main {
|
||||||
|
|
||||||
// judge if exist in dir, if not add it, otherwise ignore.
|
// judge if exist in dir, if not add it, otherwise ignore.
|
||||||
int fromIndex = WebDavConfig.getHomePath().length();
|
int fromIndex = WebDavConfig.getHomePath().length();
|
||||||
for (File cur : allFiles) {
|
|
||||||
|
|
||||||
|
ExecutorService executor = Executors.newFixedThreadPool(8);
|
||||||
|
List<String> finalAllSuccessData = allSuccessData;
|
||||||
|
int allFileSize = allFiles.size();
|
||||||
|
|
||||||
|
AtomicInteger finishCount = new AtomicInteger();
|
||||||
|
for (int i = 0; i < allFileSize; i++) {
|
||||||
|
int taskId = i;
|
||||||
|
|
||||||
|
executor.submit(() -> {
|
||||||
|
File cur = allFiles.get(taskId);
|
||||||
String relativeName = cur.getAbsolutePath().substring(fromIndex).replaceAll("\\\\", "/");
|
String relativeName = cur.getAbsolutePath().substring(fromIndex).replaceAll("\\\\", "/");
|
||||||
String dbName = WebDavConfig.getRoot() + relativeName;
|
String dbName = WebDavConfig.getRoot() + relativeName;
|
||||||
|
|
||||||
long count = allSuccessData.stream().filter(db -> db.contains(dbName)).count();
|
SendResult result= null;
|
||||||
System.out.print(dbName + " : ");
|
if (!finalAllSuccessData.contains(dbName)) {
|
||||||
if (count == 0) {
|
result = WebDavUtil.uploadFile(cur, dbName);
|
||||||
WebDavUtil.uploadFile(cur, dbName);
|
if (result.getCode() == 201) {
|
||||||
ProjectUtil.insertSuccessData(dbName);
|
ProjectUtil.insertSuccessData(dbName);
|
||||||
} else {
|
|
||||||
System.out.println(" Ignored");
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
result = new SendResult(200, dbName + " : Ignored");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized (finishCount) {
|
||||||
|
int now = finishCount.getAndIncrement();
|
||||||
|
String str = String.format("[%d/%d] (%3.2f %%) <%d> %s",
|
||||||
|
now + 1, allFileSize, (now + 1) * 100.0f / allFiles.size(),
|
||||||
|
result.getCode(), result.getResult());
|
||||||
|
int code = result.getCode();
|
||||||
|
if (code == 200 || code == 201) {
|
||||||
|
System.out.println(str);
|
||||||
|
} else {
|
||||||
|
System.err.println(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
executor.shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package top.dreamcenter.webdav.bean;
|
||||||
|
|
||||||
|
public class SendResult {
|
||||||
|
private Integer code;
|
||||||
|
private String result;
|
||||||
|
|
||||||
|
public SendResult(Integer code, String result) {
|
||||||
|
this.code = code;
|
||||||
|
this.result = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCode(Integer code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getResult() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResult(String result) {
|
||||||
|
this.result = result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
|
import top.dreamcenter.webdav.bean.SendResult;
|
||||||
import top.dreamcenter.webdav.prop.WebDavConfig;
|
import top.dreamcenter.webdav.prop.WebDavConfig;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -26,7 +27,7 @@ public class WebDavUtil {
|
||||||
* @param file 具体待上传的文件
|
* @param file 具体待上传的文件
|
||||||
* @param davName dav拼接的相对文件路径和文件名
|
* @param davName dav拼接的相对文件路径和文件名
|
||||||
*/
|
*/
|
||||||
public static void uploadFile(File file, String davName) {
|
public static SendResult uploadFile(File file, String davName) {
|
||||||
// 创建HttpClient实例
|
// 创建HttpClient实例
|
||||||
CredentialsProvider provider = new BasicCredentialsProvider();
|
CredentialsProvider provider = new BasicCredentialsProvider();
|
||||||
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(WebDavConfig.getAccount(), WebDavConfig.getPassword());
|
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(WebDavConfig.getAccount(), WebDavConfig.getPassword());
|
||||||
|
@ -42,16 +43,20 @@ public class WebDavUtil {
|
||||||
// 执行请求
|
// 执行请求
|
||||||
try (CloseableHttpResponse response = client.execute(httpPut)) {
|
try (CloseableHttpResponse response = client.execute(httpPut)) {
|
||||||
// 检查响应状态码
|
// 检查响应状态码
|
||||||
System.out.print(response.getStatusLine().getStatusCode());
|
int code = response.getStatusLine().getStatusCode();
|
||||||
HttpEntity responseEntity = response.getEntity();
|
HttpEntity responseEntity = response.getEntity();
|
||||||
|
|
||||||
if (responseEntity != null) {
|
if (responseEntity != null) {
|
||||||
System.out.println(" " + responseEntityToString(responseEntity));
|
String result = davName + " : " + responseEntityToString(responseEntity);
|
||||||
|
return new SendResult(code, result);
|
||||||
} else {
|
} else {
|
||||||
System.out.println(" Null");
|
String result = davName + " : Null";
|
||||||
|
return new SendResult(code, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
String result = davName + " : " + e.getMessage();
|
||||||
|
return new SendResult(-1, result);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
client.close();
|
client.close();
|
||||||
|
|
Loading…
Reference in New Issue