commit
aa93d2d6d6
|
@ -0,0 +1,6 @@
|
|||
config.properties
|
||||
.mvn
|
||||
.idea
|
||||
target
|
||||
directory.db
|
||||
webdav.iml
|
|
@ -0,0 +1,5 @@
|
|||
url=https://xxx.xxx/dav/
|
||||
account=xxxxx@qq.com
|
||||
password=xxxxxxx
|
||||
homePath=E:\\data\\test
|
||||
root=test
|
|
@ -0,0 +1,88 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>top.dreamcenter</groupId>
|
||||
<artifactId>webdav</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<name>webdav</name>
|
||||
<url>https://www.dreamcenter.top</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit</groupId>
|
||||
<artifactId>junit-bom</artifactId>
|
||||
<version>5.11.0</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>3.36.0.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.1</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.6.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>top.dreamcenter.webdav.Main</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,45 @@
|
|||
package top.dreamcenter.webdav;
|
||||
|
||||
import top.dreamcenter.webdav.prop.WebDavConfig;
|
||||
import top.dreamcenter.webdav.util.ProjectUtil;
|
||||
import top.dreamcenter.webdav.util.WebDavUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
ProjectUtil.createDBIfNotExist();
|
||||
|
||||
List<File> allFiles = ProjectUtil.getAllFiles();
|
||||
List<String> allSuccessData = null;
|
||||
|
||||
try {
|
||||
allSuccessData = ProjectUtil.getAllSuccessData();
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
|
||||
if (allSuccessData == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// judge if exist in dir, if not add it, otherwise ignore.
|
||||
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 + " : ");
|
||||
if (count == 0) {
|
||||
WebDavUtil.uploadFile(cur, dbName);
|
||||
ProjectUtil.insertSuccessData(dbName);
|
||||
} else {
|
||||
System.out.println(" Ignored");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package top.dreamcenter.webdav.prop;
|
||||
|
||||
/**
|
||||
* 全局常量定义
|
||||
*/
|
||||
public class Constants {
|
||||
/**
|
||||
* 存表名
|
||||
*/
|
||||
public static final String TABLE_NAME = "file_monitor";
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package top.dreamcenter.webdav.prop;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Webdav配置
|
||||
*/
|
||||
public class WebDavConfig {
|
||||
/**
|
||||
* url地址
|
||||
*/
|
||||
private final static String url;
|
||||
/**
|
||||
* 账户
|
||||
*/
|
||||
private final static String account;
|
||||
/**
|
||||
* 密钥
|
||||
*/
|
||||
private final static String password;
|
||||
/**
|
||||
* 同步的根目录
|
||||
*/
|
||||
private final static String homePath;
|
||||
/**
|
||||
* dav根目录
|
||||
*/
|
||||
private final static String root;
|
||||
|
||||
static {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(new FileReader("config.properties"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
url = properties.getProperty("url");
|
||||
account = properties.getProperty("account");
|
||||
password = properties.getProperty("password");
|
||||
homePath = properties.getProperty("homePath");
|
||||
root = properties.getProperty("root");
|
||||
}
|
||||
|
||||
public static String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public static String getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public static String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public static String getHomePath() {
|
||||
return homePath;
|
||||
}
|
||||
|
||||
public static String getRoot() {
|
||||
return root;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
package top.dreamcenter.webdav.util;
|
||||
|
||||
import top.dreamcenter.webdav.prop.Constants;
|
||||
import top.dreamcenter.webdav.prop.WebDavConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* 项目工具
|
||||
*/
|
||||
public class ProjectUtil {
|
||||
|
||||
/**
|
||||
* 初始化建表
|
||||
*/
|
||||
public static void createDBIfNotExist(){
|
||||
SqlLiteUtil instance = SqlLiteUtil.getInstance();
|
||||
String sql = "CREATE TABLE IF NOT EXISTS " + Constants.TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, fileName TEXT, status INTEGER)";
|
||||
instance.execute(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据库中所有记录已存的文件
|
||||
* @return 所有已记录的文件
|
||||
* @throws SQLException 查询异常
|
||||
*/
|
||||
public static List<String> getAllSuccessData() throws SQLException {
|
||||
SqlLiteUtil sqlLiteUtil = SqlLiteUtil.getInstance();
|
||||
Connection connection = sqlLiteUtil.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT fileName FROM " + Constants.TABLE_NAME + " WHERE status = 1");
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
List<String> list = new LinkedList<>();
|
||||
while (resultSet.next()) {
|
||||
String fileName = resultSet.getString("fileName");
|
||||
list.add(fileName);
|
||||
}
|
||||
|
||||
connection.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入一条新的已存文件
|
||||
* @param dbName 文件记录名称
|
||||
*/
|
||||
public static void insertSuccessData(String dbName) {
|
||||
SqlLiteUtil sqlLiteUtil = SqlLiteUtil.getInstance();
|
||||
String sql = String.format("INSERT INTO %s (fileName, status) VALUES ('%s', 1)", Constants.TABLE_NAME, dbName);
|
||||
sqlLiteUtil.execute(sql);
|
||||
}
|
||||
|
||||
public static List<File> getAllFiles() {
|
||||
List<File> list = new LinkedList<>();
|
||||
|
||||
File file = new File(WebDavConfig.getHomePath());
|
||||
|
||||
Queue<File> dirQueue = new LinkedList<>();
|
||||
dirQueue.add(file);
|
||||
|
||||
while (!dirQueue.isEmpty()) {
|
||||
File tmp = dirQueue.remove();
|
||||
|
||||
File[] files = tmp.listFiles();
|
||||
if (files != null) {
|
||||
for (File curFile : files) {
|
||||
if (curFile.isDirectory()) dirQueue.add(curFile);
|
||||
else list.add(curFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package top.dreamcenter.webdav.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* sqlite工具
|
||||
*/
|
||||
public class SqlLiteUtil {
|
||||
|
||||
private static volatile SqlLiteUtil instance;
|
||||
|
||||
private SqlLiteUtil(){
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单例实例
|
||||
* @return SqlLiteUtil实例
|
||||
*/
|
||||
public static SqlLiteUtil getInstance() {
|
||||
if (instance == null){
|
||||
synchronized (SqlLiteUtil.class) {
|
||||
if (instance == null) {
|
||||
instance = new SqlLiteUtil();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个连接,主要用于查询
|
||||
* @return 连接connection
|
||||
* @throws SQLException 创建连接失败
|
||||
*/
|
||||
public Connection getConnection() throws SQLException {
|
||||
return DriverManager.getConnection("jdbc:sqlite:directory.db");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行sql语句,不需要返回值
|
||||
* @param sql sql语句
|
||||
*/
|
||||
public void execute(String sql) {
|
||||
try (Connection connection = getConnection()) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.execute();
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package top.dreamcenter.webdav.util;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.FileEntity;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import top.dreamcenter.webdav.prop.WebDavConfig;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* WebDav工具
|
||||
*/
|
||||
public class WebDavUtil {
|
||||
|
||||
/**
|
||||
* 上传更新文件
|
||||
* @param file 具体待上传的文件
|
||||
* @param davName dav拼接的相对文件路径和文件名
|
||||
*/
|
||||
public static void uploadFile(File file, String davName) {
|
||||
// 创建HttpClient实例
|
||||
CredentialsProvider provider = new BasicCredentialsProvider();
|
||||
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(WebDavConfig.getAccount(), WebDavConfig.getPassword());
|
||||
provider.setCredentials(AuthScope.ANY, credentials);
|
||||
CloseableHttpClient client = HttpClients.custom()
|
||||
.setDefaultCredentialsProvider(provider)
|
||||
.build();
|
||||
try {
|
||||
// 创建PUT请求
|
||||
HttpPut httpPut = new HttpPut(WebDavConfig.getUrl() + "/" + davName);
|
||||
FileEntity fileEntity = new FileEntity(file, ContentType.DEFAULT_BINARY); // 或者根据需要选择正确的ContentType
|
||||
httpPut.setEntity(fileEntity);
|
||||
// 执行请求
|
||||
try (CloseableHttpResponse response = client.execute(httpPut)) {
|
||||
// 检查响应状态码
|
||||
System.out.print(response.getStatusLine().getStatusCode());
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
if (responseEntity != null) {
|
||||
System.out.println(" " + responseEntityToString(responseEntity));
|
||||
} else {
|
||||
System.out.println(" Null");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
client.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String responseEntityToString(HttpEntity entity) throws Exception {
|
||||
return EntityUtils.toString(entity, "UTF-8"); // 需要Apache HttpCore的依赖来使用这个方法
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package top.dreamcenter;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import top.dreamcenter.webdav.prop.Constants;
|
||||
import top.dreamcenter.webdav.util.SqlLiteUtil;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
/**
|
||||
* Rigorous Test :-)
|
||||
*/
|
||||
@Test
|
||||
public void shouldAnswerWithTrue() {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue