-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
307 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
luna-commons-file/src/main/java/com/luna/file/config/HttpUserAuthConfigValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.luna.file.config; | ||
|
||
import org.apache.commons.httpclient.Credentials; | ||
import org.apache.commons.httpclient.HttpClient; | ||
import org.apache.commons.httpclient.UsernamePasswordCredentials; | ||
import org.apache.commons.httpclient.auth.AuthScope; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.sun.jersey.api.client.Client; | ||
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; | ||
|
||
/** | ||
* @Package: com.luna.file.httpd | ||
* @ClassName: UserAuth | ||
* @Author: luna | ||
* @CreateTime: 2020/11/13 19:28 | ||
* @Description: | ||
*/ | ||
@ConfigurationProperties(prefix = "luna.http") | ||
@Component | ||
public class HttpUserAuthConfigValue { | ||
|
||
private String username; | ||
private String password; | ||
/** | ||
* 客户端 | ||
*/ | ||
private Client client; | ||
|
||
private HttpClient httpClient; | ||
|
||
public HttpClient getHttpClient() { | ||
if (httpClient == null) { | ||
this.httpClient = new HttpClient(); | ||
// 设置用户名密码认证形式 | ||
Credentials creds = new UsernamePasswordCredentials(username, password); | ||
httpClient.getState().setCredentials(AuthScope.ANY, creds); | ||
return httpClient; | ||
} | ||
return httpClient; | ||
} | ||
|
||
public HttpClient getHttpClient(String username, String password) { | ||
if (httpClient == null) { | ||
this.httpClient = new HttpClient(); | ||
// 设置用户名密码认证形式 | ||
Credentials creds = new UsernamePasswordCredentials(username, password); | ||
httpClient.getState().setCredentials(AuthScope.ANY, creds); | ||
return httpClient; | ||
} | ||
return httpClient; | ||
} | ||
|
||
public HttpUserAuthConfigValue setHttpClient(HttpClient httpClient) { | ||
this.httpClient = httpClient; | ||
return this; | ||
} | ||
|
||
public Client getClient() { | ||
if (client == null) { | ||
this.client = Client.create(); | ||
client.addFilter(new HTTPBasicAuthFilter(username, password)); | ||
return client; | ||
} | ||
return client; | ||
} | ||
|
||
public HttpUserAuthConfigValue setClient(Client client) { | ||
this.client = client; | ||
return this; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public HttpUserAuthConfigValue setUsername(String username) { | ||
this.username = username; | ||
return this; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public HttpUserAuthConfigValue setPassword(String password) { | ||
this.password = password; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
luna-commons-file/src/main/java/com/luna/file/httpd/HttpdFileUtilByJack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package com.luna.file.httpd; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.luna.common.dto.constant.ResultCode; | ||
import com.luna.common.exception.FileException; | ||
import org.apache.commons.httpclient.HttpClient; | ||
import org.apache.commons.httpclient.methods.InputStreamRequestEntity; | ||
import org.apache.commons.httpclient.methods.RequestEntity; | ||
import org.apache.jackrabbit.webdav.DavConstants; | ||
import org.apache.jackrabbit.webdav.DavException; | ||
import org.apache.jackrabbit.webdav.MultiStatus; | ||
import org.apache.jackrabbit.webdav.MultiStatusResponse; | ||
import org.apache.jackrabbit.webdav.client.methods.*; | ||
import org.apache.jackrabbit.webdav.lock.Scope; | ||
import org.apache.jackrabbit.webdav.lock.Type; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
||
public class HttpdFileUtilByJack { | ||
|
||
/** | ||
* 新建文件夹 | ||
* | ||
* @param httpClient | ||
* @param httpPath | ||
*/ | ||
public static Integer newDirectory(HttpClient httpClient, String httpPath) { | ||
try { | ||
DavMethod mkCol = new MkColMethod(httpPath); | ||
httpClient.executeMethod(mkCol); | ||
// 获取执行返回结果 | ||
return mkCol.getStatusCode(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, | ||
ResultCode.MSG_ERROR_SYSTEM_EXCEPTION + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* 上传文件 | ||
* | ||
* @param httpClient | ||
* @param filePath 文件路径 | ||
* @param httpPath 上传路径 | ||
*/ | ||
public static Integer upload(HttpClient httpClient, String filePath, String httpPath) { | ||
try { | ||
// Put Method - 上传本地文件 | ||
PutMethod put = new PutMethod(httpPath); | ||
RequestEntity requestEntity = new InputStreamRequestEntity(new FileInputStream(filePath)); | ||
put.setRequestEntity(requestEntity); | ||
httpClient.executeMethod(put); | ||
return put.getStatusCode(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, | ||
ResultCode.MSG_ERROR_SYSTEM_EXCEPTION + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* 远程复制文件 | ||
* | ||
* @param httpClient | ||
* @param uri 其实地址 | ||
* @param destinationUri 目的地址 | ||
* @param overwrite 是否覆盖 | ||
*/ | ||
public static Integer copyFileRemote(HttpClient httpClient, String uri, String destinationUri, boolean overwrite) { | ||
try { | ||
DavMethod copy = new CopyMethod(uri, destinationUri, overwrite); | ||
httpClient.executeMethod(copy); | ||
return copy.getStatusCode(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, | ||
ResultCode.MSG_ERROR_SYSTEM_EXCEPTION + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* 重命名文件,也可移动文件 | ||
* | ||
* @param httpClient | ||
* @param oldPath 旧地址 | ||
* @param newPath 新地址 | ||
* @return | ||
*/ | ||
public static Integer renameFile(HttpClient httpClient, String oldPath, String newPath) { | ||
try { | ||
DavMethod move = new MoveMethod(oldPath, newPath, true); | ||
httpClient.executeMethod(move); | ||
return move.getStatusCode(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, | ||
ResultCode.MSG_ERROR_SYSTEM_EXCEPTION + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* 文件枷锁 | ||
* | ||
* @param httpClient | ||
* @param filePath 文件路径 | ||
* @param owner 拥有者 | ||
* @param timeout 锁定时间 | ||
* @return 解锁token | ||
*/ | ||
public static String clockFile(HttpClient httpClient, String filePath, String owner, long timeout) { | ||
try { | ||
LockMethod lock = new LockMethod(filePath, Scope.SHARED, Type.WRITE, owner, | ||
timeout, false); | ||
httpClient.executeMethod(lock); | ||
String lockToken = lock.getLockToken(); | ||
return lockToken; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, | ||
ResultCode.MSG_ERROR_SYSTEM_EXCEPTION + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* 解锁文件 | ||
* | ||
* @param httpClient | ||
* @param filePath 文件路径 | ||
* @param lockToken 解锁token | ||
* @return | ||
*/ | ||
public static Integer unlockFile(HttpClient httpClient, String filePath, String lockToken) { | ||
try { | ||
DavMethod unlock = new UnLockMethod(filePath, lockToken); | ||
httpClient.executeMethod(unlock); | ||
return unlock.getStatusCode(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, | ||
ResultCode.MSG_ERROR_SYSTEM_EXCEPTION + e.getMessage()); | ||
} | ||
} | ||
|
||
public static Integer copyFileRemote(HttpClient httpClient, String uri, String destinationUri) { | ||
return copyFileRemote(httpClient, uri, destinationUri, true); | ||
} | ||
|
||
/** | ||
* 遍历文件夹 | ||
* | ||
* @param httpClient | ||
* @param folderPath 文件夹 | ||
*/ | ||
public static void findFile(HttpClient httpClient, String folderPath) { | ||
ArrayList<String> files = Lists.newArrayList(); | ||
try { | ||
DavMethod find = new PropFindMethod(folderPath, DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1); | ||
httpClient.executeMethod(find); | ||
MultiStatus multiStatus = find.getResponseBodyAsMultiStatus(); | ||
MultiStatusResponse[] responses = multiStatus.getResponses(); | ||
for (MultiStatusResponse respons : responses) { | ||
files.add(respons.getHref()); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, | ||
ResultCode.MSG_ERROR_SYSTEM_EXCEPTION + e.getMessage()); | ||
} catch (DavException e) { | ||
e.printStackTrace(); | ||
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, | ||
ResultCode.MSG_ERROR_SYSTEM_EXCEPTION + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* 删除文件夹 | ||
* | ||
* @param httpClient | ||
* @param path | ||
* @return | ||
*/ | ||
public static Integer deleteFile(HttpClient httpClient, String path) { | ||
try { | ||
DavMethod delete = new DeleteMethod(path); | ||
httpClient.executeMethod(delete); | ||
return delete.getStatusCode(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, | ||
ResultCode.MSG_ERROR_SYSTEM_EXCEPTION + e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters