Skip to content

Commit

Permalink
🎨✨ httpByJack
Browse files Browse the repository at this point in the history
  • Loading branch information
lunasaw committed Nov 13, 2020
1 parent c3fdf95 commit 399a247
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 3 deletions.
5 changes: 5 additions & 0 deletions luna-commons-file/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>jersey-client</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-webdav</artifactId>
<version>2.2.5</version>
</dependency>
<!-- fastdfs 文件服务-->
<dependency>
<groupId>net.oschina.zcx7878</groupId>
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.luna.common.dto.constant.ResultCode;
import com.luna.common.exception.FileException;
import com.luna.file.img.ImageUtils;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -89,12 +91,11 @@ public static void downloadFileWithRetry(String url, String file, int maxRetry)
* @return
* @throws Exception
*/
public static void uploadFile(byte[] file, String filePath) {
public static void uploadFile(Client client, byte[] file, String filePath) {
File targetFile = new File(filePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
Client client = new Client();
WebResource resource = client.resource(filePath);
resource.put(String.class, file);
}
Expand All @@ -110,4 +111,11 @@ public static void delete(String filePath) {
WebResource resource = client.resource(filePath);
resource.delete();
}

public static void main(String[] args) {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("luna", "czy1024"));
uploadFile(client, ImageUtils.getBytes("C:\\Users\\improve\\Pictures\\Saved Pictures\\girl.png"),
"http://localhost:8087/luna/apache/a.png");
}
}
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());
}
}
}
6 changes: 5 additions & 1 deletion luna-commons-file/src/main/resources/application-pro.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ luna:
version: 1.0
copyrightYear: 2020
profile: luna
addressEnabled: true
addressEnabled: true

http:
username: luna
password: czy1024

0 comments on commit 399a247

Please sign in to comment.