Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
|
64a827df0b | |
|
63c8e87e2b | |
|
8255010524 |
|
@ -1,6 +1,4 @@
|
||||||
config.properties
|
config.properties
|
||||||
.mvn
|
.mvn
|
||||||
.idea
|
|
||||||
target
|
target
|
||||||
directory.db
|
directory.db
|
||||||
webdav.iml
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<module name="webdav" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
|
<bytecodeTargetLevel target="8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||||
|
<data-source source="LOCAL" name="directory.db" uuid="360b58ab-bdbc-4ea8-8af9-7b6a6907cff8">
|
||||||
|
<driver-ref>sqlite.xerial</driver-ref>
|
||||||
|
<synchronize>true</synchronize>
|
||||||
|
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
|
||||||
|
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/directory.db</jdbc-url>
|
||||||
|
<working-dir>$ProjectFileDir$</working-dir>
|
||||||
|
</data-source>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="RemoteRepositoriesConfiguration">
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Central Repository" />
|
||||||
|
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Maven Central repository" />
|
||||||
|
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="jboss.community" />
|
||||||
|
<option name="name" value="JBoss Community repository" />
|
||||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||||
|
</remote-repository>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -1,5 +1,6 @@
|
||||||
url=https://xxx.xxx/dav/
|
url=https://xxx.xxx/dav/
|
||||||
account=xxxxx@qq.com
|
account=xxxxx@qq.com
|
||||||
password=xxxxxxx
|
password=xxxxxxx
|
||||||
|
threads=8
|
||||||
homePath=E:\\data\\test
|
homePath=E:\\data\\test
|
||||||
root=test
|
root=test
|
|
@ -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,9 @@ 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.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -27,19 +31,52 @@ 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) {
|
|
||||||
String relativeName = cur.getAbsolutePath().substring(fromIndex).replaceAll("\\\\", "/");
|
|
||||||
String dbName = WebDavConfig.getRoot() + relativeName;
|
|
||||||
|
|
||||||
long count = allSuccessData.stream().filter(db -> db.contains(dbName)).count();
|
|
||||||
System.out.print(dbName + " : ");
|
ExecutorService executor = Executors.newFixedThreadPool(WebDavConfig.getThreads());
|
||||||
if (count == 0) {
|
List<String> finalAllSuccessData = allSuccessData;
|
||||||
WebDavUtil.uploadFile(cur, dbName);
|
int allFileSize = allFiles.size();
|
||||||
ProjectUtil.insertSuccessData(dbName);
|
|
||||||
} else {
|
AtomicInteger finishCount = new AtomicInteger();
|
||||||
System.out.println(" Ignored");
|
for (int i = 0; i < allFileSize; i++) {
|
||||||
}
|
int taskId = i;
|
||||||
|
|
||||||
|
executor.submit(() -> {
|
||||||
|
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
File cur = allFiles.get(taskId);
|
||||||
|
String relativeName = cur.getAbsolutePath().substring(fromIndex).replaceAll("\\\\", "/");
|
||||||
|
String dbName = WebDavConfig.getRoot() + relativeName;
|
||||||
|
|
||||||
|
SendResult result= null;
|
||||||
|
if (!finalAllSuccessData.contains(dbName)) {
|
||||||
|
result = WebDavUtil.uploadFile(cur, dbName);
|
||||||
|
if (result.getCode() == 201) {
|
||||||
|
ProjectUtil.insertSuccessData(dbName);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result = new SendResult(200, dbName + " : Ignored");
|
||||||
|
}
|
||||||
|
|
||||||
|
long endTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
|
||||||
|
synchronized (finishCount) {
|
||||||
|
int now = finishCount.getAndIncrement();
|
||||||
|
String str = String.format("[%d/%d] (%3.2f %%) <%d : %.2f s> %s",
|
||||||
|
now + 1, allFileSize, (now + 1) * 100.0f / allFiles.size(),
|
||||||
|
result.getCode(), (endTime - startTime)/1000.0, 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,6 +29,11 @@ public class WebDavConfig {
|
||||||
*/
|
*/
|
||||||
private final static String root;
|
private final static String root;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启用线程数量
|
||||||
|
*/
|
||||||
|
private final static Integer threads;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
try {
|
try {
|
||||||
|
@ -42,6 +47,7 @@ public class WebDavConfig {
|
||||||
password = properties.getProperty("password");
|
password = properties.getProperty("password");
|
||||||
homePath = properties.getProperty("homePath");
|
homePath = properties.getProperty("homePath");
|
||||||
root = properties.getProperty("root");
|
root = properties.getProperty("root");
|
||||||
|
threads = Integer.parseInt(properties.getProperty("threads"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getUrl() {
|
public static String getUrl() {
|
||||||
|
@ -63,4 +69,8 @@ public class WebDavConfig {
|
||||||
public static String getRoot() {
|
public static String getRoot() {
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Integer getThreads() {
|
||||||
|
return threads;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,14 @@ import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
import org.apache.http.client.CredentialsProvider;
|
import org.apache.http.client.CredentialsProvider;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpPut;
|
import org.apache.http.client.methods.HttpPut;
|
||||||
|
import org.apache.http.client.utils.URLEncodedUtils;
|
||||||
import org.apache.http.entity.ContentType;
|
import org.apache.http.entity.ContentType;
|
||||||
import org.apache.http.entity.FileEntity;
|
import org.apache.http.entity.FileEntity;
|
||||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
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 +28,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());
|
||||||
|
@ -36,22 +38,26 @@ public class WebDavUtil {
|
||||||
.build();
|
.build();
|
||||||
try {
|
try {
|
||||||
// 创建PUT请求
|
// 创建PUT请求
|
||||||
HttpPut httpPut = new HttpPut(WebDavConfig.getUrl() + "/" + davName);
|
HttpPut httpPut = new HttpPut(WebDavConfig.getUrl() + "/" + encodeURL(davName));
|
||||||
FileEntity fileEntity = new FileEntity(file, ContentType.DEFAULT_BINARY); // 或者根据需要选择正确的ContentType
|
FileEntity fileEntity = new FileEntity(file, ContentType.DEFAULT_BINARY); // 或者根据需要选择正确的ContentType
|
||||||
httpPut.setEntity(fileEntity);
|
httpPut.setEntity(fileEntity);
|
||||||
// 执行请求
|
// 执行请求
|
||||||
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();
|
||||||
|
@ -64,4 +70,9 @@ public class WebDavUtil {
|
||||||
private static String responseEntityToString(HttpEntity entity) throws Exception {
|
private static String responseEntityToString(HttpEntity entity) throws Exception {
|
||||||
return EntityUtils.toString(entity, "UTF-8"); // 需要Apache HttpCore的依赖来使用这个方法
|
return EntityUtils.toString(entity, "UTF-8"); // 需要Apache HttpCore的依赖来使用这个方法
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String encodeURL(String raw) {
|
||||||
|
String[] split = raw.split("/");
|
||||||
|
return URLEncodedUtils.formatSegments(split);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4" />
|
Loading…
Reference in New Issue