mirror of
https://github.com/yanlongqi/jhinno-openapi-java-sdk.git
synced 2026-03-22 06:15:10 +08:00
文件相关接集成完成
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -48,6 +48,12 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
|
||||
@@ -132,15 +132,29 @@ public class JHApiExecution {
|
||||
* @param username 用户名
|
||||
* @return 请求头
|
||||
*/
|
||||
protected Map<String, String> getHeaders(String username) {
|
||||
if (StringUtils.isBlank(username)) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
protected Map<String, String> getHeaders(String username, boolean isContentType) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
// 默认请求json数据
|
||||
if (isContentType) {
|
||||
headers.put("Content-type", "application/json");
|
||||
}
|
||||
if (StringUtils.isBlank(username)) {
|
||||
return headers;
|
||||
}
|
||||
headers.put("token", getToken(username));
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建一个带token的请求头
|
||||
*
|
||||
* @param username 用户名
|
||||
* @return 请求头
|
||||
*/
|
||||
protected Map<String, String> getHeaders(String username) {
|
||||
return getHeaders(username, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起一个匿名的GET请求
|
||||
*
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.jhinno.sdk.openapi.api.file;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 文件下载地址信息
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/5 17:53
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class DownloadInfo {
|
||||
|
||||
/**
|
||||
* 文件地址
|
||||
*/
|
||||
private String url;
|
||||
|
||||
|
||||
}
|
||||
53
src/main/java/com/jhinno/sdk/openapi/api/file/FileInfo.java
Normal file
53
src/main/java/com/jhinno/sdk/openapi/api/file/FileInfo.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package com.jhinno.sdk.openapi.api.file;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 文件信息
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/5 10:47
|
||||
*/
|
||||
@Data
|
||||
public class FileInfo {
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String fileName;
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* 文件类型
|
||||
* <ul>
|
||||
* <li>directory:文件夹</li>
|
||||
* <li>file:文件</li>
|
||||
* </ul>
|
||||
*/
|
||||
private String fileType;
|
||||
|
||||
/**
|
||||
* 文件属主
|
||||
*/
|
||||
private String owner;
|
||||
|
||||
/**
|
||||
* 是否可读
|
||||
*/
|
||||
private Boolean read;
|
||||
|
||||
/**
|
||||
* 是否可写
|
||||
*/
|
||||
private Boolean write;
|
||||
|
||||
/**
|
||||
* 是否可执行
|
||||
*/
|
||||
private Boolean execute;
|
||||
|
||||
|
||||
}
|
||||
@@ -64,8 +64,4 @@ public class FilePathConstant {
|
||||
*/
|
||||
public static final String FILE_UNCOMPRESS_PATH = "/ws/api/files/uncompress";
|
||||
|
||||
/**
|
||||
* 浏览文件
|
||||
*/
|
||||
public static final String FILE_BROWSE_PATH = "/ws/api/browseFile";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.jhinno.sdk.openapi.api.file;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 文件创建消息
|
||||
*
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/5 14:17
|
||||
*/
|
||||
@Data
|
||||
public class FilePathInfo {
|
||||
|
||||
/**
|
||||
* 新的文件夹的位置
|
||||
*/
|
||||
private String dirPath;
|
||||
}
|
||||
@@ -1,11 +1,22 @@
|
||||
package com.jhinno.sdk.openapi.api.file;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.jhinno.sdk.openapi.ArgsException;
|
||||
import com.jhinno.sdk.openapi.CommonConstant;
|
||||
import com.jhinno.sdk.openapi.ServiceException;
|
||||
import com.jhinno.sdk.openapi.api.JHApiExecution;
|
||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||
import com.jhinno.sdk.openapi.client.JHApiClient;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -22,20 +33,20 @@ public class JHFileApiExecution extends JHApiExecution {
|
||||
/**
|
||||
* 重命名文件
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param sourceFileName 源文件
|
||||
* @param targetName 目标文件
|
||||
* @param username 用户名
|
||||
* @param sourceFileNamePath 源文件路径
|
||||
* @param targetFileName 目标文件路径
|
||||
*/
|
||||
public void renameFile(String username, String sourceFileName, String targetName) {
|
||||
if (StringUtils.isBlank(sourceFileName)) {
|
||||
throw new ArgsException("sourceFileName不能为空!");
|
||||
public void renameFile(String username, String sourceFileNamePath, String targetFileName) {
|
||||
if (StringUtils.isBlank(sourceFileNamePath)) {
|
||||
throw new ArgsException("sourceFileNamePath不能为空!");
|
||||
}
|
||||
if (StringUtils.isBlank(targetName)) {
|
||||
throw new ArgsException("targetName不能为空!");
|
||||
if (StringUtils.isBlank(targetFileName)) {
|
||||
throw new ArgsException("targetFileName不能为空!");
|
||||
}
|
||||
Map<String, Object> body = new HashMap<>(2);
|
||||
body.put("oldFileName", sourceFileName);
|
||||
body.put("newFileName", targetName);
|
||||
body.put("oldFileName", sourceFileNamePath);
|
||||
body.put("newFileName", targetFileName);
|
||||
put(FilePathConstant.FILE_RENAME_PATH, username, body);
|
||||
}
|
||||
|
||||
@@ -43,17 +54,286 @@ public class JHFileApiExecution extends JHApiExecution {
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param fileName 文件路径
|
||||
* @param username 用户名
|
||||
* @param sourceFilePath 源文件路径
|
||||
*/
|
||||
public void deleteFile(String username, String fileName) {
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
throw new ArgsException("fileName不能为空!");
|
||||
public void deleteFile(String username, String sourceFilePath) {
|
||||
if (StringUtils.isBlank(sourceFilePath)) {
|
||||
throw new ArgsException("sourceFilePath不能为空!");
|
||||
}
|
||||
Map<String, Object> params = new HashMap<>(1);
|
||||
params.put("fileName", fileName);
|
||||
params.put("fileName", sourceFilePath);
|
||||
String path = JHApiClient.getUrl(FilePathConstant.FILE_DELETE_PATH, params);
|
||||
delete(path, username);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 拷贝文件到目标文件夹
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param sourceFilePath 源文件路径
|
||||
* @param targetDirectoryPath 目标文件路径
|
||||
*/
|
||||
public void copyFile(String username, String sourceFilePath, String targetDirectoryPath) {
|
||||
if (StringUtils.isBlank(sourceFilePath)) {
|
||||
throw new ArgsException("sourceFilePath不能为空!");
|
||||
}
|
||||
if (StringUtils.isBlank(targetDirectoryPath)) {
|
||||
throw new ArgsException("targetDirectoryPath不能为空!");
|
||||
}
|
||||
Map<String, Object> body = new HashMap<>(2);
|
||||
body.put("sourceFileName", sourceFilePath);
|
||||
body.put("targetDirectory", targetDirectoryPath);
|
||||
put(FilePathConstant.FILE_COPY_PATH, username, body);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取文件列表
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param dirPath 文件路径
|
||||
* @return 文件列表
|
||||
*/
|
||||
public List<FileInfo> getFileList(String username, String dirPath) {
|
||||
if (StringUtils.isBlank(dirPath)) {
|
||||
throw new ArgsException("path不能为空!");
|
||||
}
|
||||
Map<String, Object> params = new HashMap<>(1);
|
||||
params.put("dir", dirPath);
|
||||
String path = JHApiClient.getUrl(FilePathConstant.FILE_LIST_PATH, params);
|
||||
return get(path, username, new TypeReference<ResponseResult<List<FileInfo>>>() {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建文件夹
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param dirPath 文件夹路径
|
||||
* @param isForce 是否强制创建(非必传,默认:false)
|
||||
* @return 新建后的文件路径
|
||||
*/
|
||||
public FilePathInfo mkdir(String username, String dirPath, Boolean isForce) {
|
||||
if (StringUtils.isBlank(dirPath)) {
|
||||
throw new ArgsException("dirPath不能为空!");
|
||||
}
|
||||
Map<String, Object> body = new HashMap<>(2);
|
||||
body.put("dirPath", dirPath);
|
||||
if (isForce != null) {
|
||||
body.put("isForce", isForce.toString());
|
||||
}
|
||||
return post(FilePathConstant.FILE_MKDIR_PATH, username, body, new TypeReference<ResponseResult<FilePathInfo>>() {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 新建文件夹,默认不强制新建
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param dirPath 文件路径
|
||||
* @return 新建后的文件路径
|
||||
*/
|
||||
public FilePathInfo mkdir(String username, String dirPath) {
|
||||
return mkdir(username, dirPath, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建文件
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param filePath 文件路径
|
||||
* @return 新的文件路径
|
||||
*/
|
||||
public FilePathInfo mkFile(String username, String filePath) {
|
||||
if (StringUtils.isBlank(filePath)) {
|
||||
throw new ArgsException("filePath不能为空!");
|
||||
}
|
||||
Map<String, Object> body = new HashMap<>(1);
|
||||
body.put("filePath", filePath);
|
||||
return post(FilePathConstant.FILE_MKFILE_PATH, username, body, new TypeReference<ResponseResult<FilePathInfo>>() {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* <p>
|
||||
* 如果isCover为空或者为false,源文件目录下存在相同文件则会报错
|
||||
* </p>
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param is 文件流
|
||||
* @param fileName 文件名称
|
||||
* @param uploadPath 上传路径
|
||||
* @param isCover 是否覆盖(非必填,默认:false)
|
||||
*/
|
||||
public void uploadFile(String username, InputStream is, String fileName, String uploadPath, Boolean isCover) {
|
||||
if (is == null) {
|
||||
throw new ArgsException("is是必填参数");
|
||||
}
|
||||
if (StringUtils.isBlank(uploadPath)) {
|
||||
throw new ArgsException("uploadPath是必填参数");
|
||||
}
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.setCharset(StandardCharsets.UTF_8);
|
||||
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
||||
builder.addBinaryBody("file", is, ContentType.MULTIPART_FORM_DATA, fileName);
|
||||
if (isCover != null) {
|
||||
builder.addTextBody("isCover", isCover.toString());
|
||||
}
|
||||
builder.addTextBody("uploadPath", uploadPath);
|
||||
HttpPost httpPost = new HttpPost(jhApiClient.getUrl(FilePathConstant.FILE_UPLOAD_PATH));
|
||||
httpPost.setEntity(builder.build());
|
||||
ResponseResult<Object> result = jhApiClient.request(httpPost, getHeaders(username, false), new TypeReference<ResponseResult<Object>>() {
|
||||
});
|
||||
if (StringUtils.equals(result.getResult(), CommonConstant.FAILED)) {
|
||||
throw new ServiceException(FilePathConstant.FILE_UPLOAD_PATH, result.getCode(), result.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 上传文件(不覆盖源文件)
|
||||
* <p>源文件目录下存在相同文件则会报错</p>
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param is 文件流
|
||||
* @param fileName 文件名
|
||||
* @param uploadPath 上传路径
|
||||
*/
|
||||
public void uploadFile(String username, InputStream is, String fileName, String uploadPath) {
|
||||
uploadFile(username, is, fileName, uploadPath, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取文件下载地址
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param filePath 文件路径
|
||||
* @return 文件地址信息
|
||||
*/
|
||||
public DownloadInfo getFileDownloadUrl(String username, String filePath) {
|
||||
if (StringUtils.isBlank(filePath)) {
|
||||
throw new ArgsException("filePath不能为空!");
|
||||
}
|
||||
Map<String, Object> params = new HashMap<>(1);
|
||||
params.put("filePath", filePath);
|
||||
String path = JHApiClient.getUrl(FilePathConstant.FILE_DOWNLOAD_PATH, params);
|
||||
return get(path, username, new TypeReference<ResponseResult<DownloadInfo>>() {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件压缩
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param sourceDirName 源文件目录
|
||||
* @param targetFilePath 目标文件路径
|
||||
* @param compressType 压缩类型 (未使用以后扩展)
|
||||
*/
|
||||
public void compress(String username, String sourceDirName, String targetFilePath, String compressType) {
|
||||
if (StringUtils.isBlank(sourceDirName)) {
|
||||
throw new ArgsException("sourceDirName不能为空!");
|
||||
}
|
||||
if (StringUtils.isBlank(targetFilePath)) {
|
||||
throw new ArgsException("targetFilePath不能为空!");
|
||||
}
|
||||
Map<String, Object> params = new HashMap<>(3);
|
||||
params.put("sourceDirName", sourceDirName);
|
||||
params.put("targetFilePath", targetFilePath);
|
||||
if (StringUtils.isNotBlank(compressType)) {
|
||||
params.put("compressType", compressType);
|
||||
}
|
||||
String path = JHApiClient.getUrl(FilePathConstant.FILE_COMPRESS_PATH, params);
|
||||
post(path, username);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 文件压缩
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param sourceDirName 源文件目录(多个文件适用英文逗号隔开)
|
||||
* @param targetFilePath 目标文件路径
|
||||
*/
|
||||
public void compress(String username, String sourceDirName, String targetFilePath) {
|
||||
compress(username, sourceDirName, targetFilePath, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解压文件
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param sourceFilePath 源文件路径
|
||||
* @param targetDirPath 目标文件路径
|
||||
* @param isCover 是否覆盖
|
||||
* @param password 密码
|
||||
* @param compressType 压缩类型 (未使用以后扩展)
|
||||
*/
|
||||
public void uncompress(String username, String sourceFilePath, String targetDirPath, Boolean isCover, String password, String compressType) {
|
||||
if (StringUtils.isBlank(sourceFilePath)) {
|
||||
throw new ArgsException("sourceFilePath不能为空!");
|
||||
}
|
||||
if (StringUtils.isBlank(targetDirPath)) {
|
||||
throw new ArgsException("targetDirPath不能为空!");
|
||||
}
|
||||
Map<String, Object> params = new HashMap<>(5);
|
||||
params.put("zipFile", sourceFilePath);
|
||||
params.put("targetDir", targetDirPath);
|
||||
if (isCover != null) {
|
||||
params.put("isCoverZipDir", isCover);
|
||||
}
|
||||
if (StringUtils.isNotBlank(password)) {
|
||||
params.put("password", password);
|
||||
}
|
||||
if (StringUtils.isNotBlank(compressType)) {
|
||||
params.put("compressType", compressType);
|
||||
}
|
||||
String path = JHApiClient.getUrl(FilePathConstant.FILE_UNCOMPRESS_PATH, params);
|
||||
post(path, username);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解压文件
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param sourceFilePath 源文件路径
|
||||
* @param targetDirPath 目标文件路径
|
||||
* @param isCover 是否覆盖
|
||||
* @param password 密码
|
||||
*/
|
||||
public void uncompress(String username, String sourceFilePath, String targetDirPath, Boolean isCover, String password) {
|
||||
uncompress(username, sourceFilePath, targetDirPath, isCover, password, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解压文件
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param sourceFilePath 源文件路径
|
||||
* @param targetDirPath 目标文件路径
|
||||
* @param isCover 是否覆盖
|
||||
*/
|
||||
public void uncompress(String username, String sourceFilePath, String targetDirPath, Boolean isCover) {
|
||||
uncompress(username, sourceFilePath, targetDirPath, isCover, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解压文件
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param sourceFilePath 源文件路径
|
||||
* @param targetDirPath 目标文件路径
|
||||
*/
|
||||
public void uncompress(String username, String sourceFilePath, String targetDirPath) {
|
||||
uncompress(username, sourceFilePath, targetDirPath, null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.jhinno.sdk.openapi.client;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -9,7 +10,6 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.*;
|
||||
import org.apache.http.config.Registry;
|
||||
@@ -28,12 +28,12 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
@@ -204,18 +204,14 @@ public class JHApiClient {
|
||||
throw new ClientException("httpRequest不能为空");
|
||||
}
|
||||
httpRequest.setConfig(requestConfig);
|
||||
if (headers == null) {
|
||||
headers = new HashMap<>();
|
||||
}
|
||||
// 默认请求json数据
|
||||
if (StringUtils.isBlank(headers.get("Content-type"))) {
|
||||
headers.put("Content-type", "application/json");
|
||||
}
|
||||
|
||||
// 添加请求头
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
httpRequest.setHeader(entry.getKey(), entry.getValue());
|
||||
if (CollectionUtil.isNotEmpty(headers)) {
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
httpRequest.setHeader(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
HttpResponse response = closeableHttpClient.execute(httpRequest);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
@@ -263,7 +259,7 @@ public class JHApiClient {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new RuntimeException("url不能为空");
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(baseUrl + path);
|
||||
HttpGet httpGet = new HttpGet(getUrl(path));
|
||||
return request(httpGet, headers, type);
|
||||
}
|
||||
|
||||
@@ -303,9 +299,8 @@ public class JHApiClient {
|
||||
}
|
||||
urlBuilder.append(entry.getKey()).append("=");
|
||||
if (value instanceof String) {
|
||||
urlBuilder.append(URLEncoder.encode((String) value, "utf-8"));
|
||||
}
|
||||
if (value instanceof Date) {
|
||||
urlBuilder.append(URLEncoder.encode((String) value, StandardCharsets.UTF_8.name()));
|
||||
} else if (value instanceof Date) {
|
||||
SimpleDateFormat format = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
|
||||
urlBuilder.append(URLEncoder.encode(format.format(value), "utf-8"));
|
||||
} else {
|
||||
@@ -320,6 +315,17 @@ public class JHApiClient {
|
||||
return urlBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取完整的请求路径
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @return 请求URL
|
||||
*/
|
||||
public String getUrl(String path) {
|
||||
return baseUrl + path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送post请求
|
||||
*
|
||||
@@ -335,7 +341,7 @@ public class JHApiClient {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new RuntimeException("path不能为空");
|
||||
}
|
||||
HttpPost httpPost = new HttpPost(baseUrl + path);
|
||||
HttpPost httpPost = new HttpPost(getUrl(path));
|
||||
try {
|
||||
if (body != null) {
|
||||
String bodyStr = mapper.writeValueAsString(body);
|
||||
@@ -363,7 +369,7 @@ public class JHApiClient {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new RuntimeException("url不能为空");
|
||||
}
|
||||
HttpPut httpPost = new HttpPut(baseUrl + path);
|
||||
HttpPut httpPost = new HttpPut(getUrl(path));
|
||||
try {
|
||||
if (body != null) {
|
||||
String bodyStr = mapper.writeValueAsString(body);
|
||||
@@ -416,7 +422,7 @@ public class JHApiClient {
|
||||
if (StringUtils.isBlank(path)) {
|
||||
throw new RuntimeException("url不能为空");
|
||||
}
|
||||
HttpDelete httpDelete = new HttpDelete(baseUrl + path);
|
||||
HttpDelete httpDelete = new HttpDelete(getUrl(path));
|
||||
return request(httpDelete, headers, type);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,6 @@ public class JHClientConfig {
|
||||
/**
|
||||
* 初始化JHApi客户端
|
||||
*/
|
||||
public static final JHApiClient client = JHApiClient.build("https://192.168.0.22/appform");
|
||||
public static final JHApiClient client = JHApiClient.build("https://192.168.87.25/appform");
|
||||
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class AuthApiTest {
|
||||
*/
|
||||
@Test
|
||||
public void testGetToken() {
|
||||
String token = jhAppApiExecution.getToken("lqyan");
|
||||
String token = jhAppApiExecution.getToken("jhadmin");
|
||||
System.out.println("token:" + token);
|
||||
}
|
||||
|
||||
|
||||
133
src/test/java/com/jhinno/sdk/openapi/test/file/FileApiTest.java
Normal file
133
src/test/java/com/jhinno/sdk/openapi/test/file/FileApiTest.java
Normal file
@@ -0,0 +1,133 @@
|
||||
package com.jhinno.sdk.openapi.test.file;
|
||||
|
||||
import cn.hutool.core.lang.ConsoleTable;
|
||||
import com.jhinno.sdk.openapi.api.file.FileInfo;
|
||||
import com.jhinno.sdk.openapi.api.file.JHFileApiExecution;
|
||||
import com.jhinno.sdk.openapi.test.JHClientConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yanlongqi
|
||||
* @date 2024/2/5 10:20
|
||||
*/
|
||||
public class FileApiTest {
|
||||
|
||||
private static final JHFileApiExecution execution = new JHFileApiExecution(JHClientConfig.client);
|
||||
|
||||
|
||||
/**
|
||||
* 测试重命名文件或文件夹
|
||||
*/
|
||||
@Test
|
||||
public void testRenameFile() {
|
||||
execution.renameFile("lqyan", "/apps/JHDP/lqyan/temp/PSUserService.class", "aaa.class");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试删除文件
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteFile() {
|
||||
execution.deleteFile("lqyan", "/apps/JHDP/lqyan/temp/aaa.class");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试文件拷贝
|
||||
*/
|
||||
@Test
|
||||
public void testCopyFile() {
|
||||
execution.copyFile("lqyan", "/apps/JHDP/lqyan/PSUserService.class", "/apps/JHDP/lqyan/temp");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试获取文件列表
|
||||
*/
|
||||
@Test
|
||||
public void testGetFileList() {
|
||||
List<FileInfo> fileList = execution.getFileList("lqyan", "$HOME");
|
||||
ConsoleTable consoleTable = ConsoleTable.create();
|
||||
consoleTable.setSBCMode(false);
|
||||
consoleTable.addHeader("fileName", "fileType", "owner", "read", "write", "execute", "path");
|
||||
for (FileInfo fileInfo : fileList) {
|
||||
consoleTable.addBody(fileInfo.getFileName(), fileInfo.getFileType(), fileInfo.getOwner(), fileInfo.getRead().toString(), fileInfo.getWrite().toString(), fileInfo.getExecute().toString(), fileInfo.getPath());
|
||||
}
|
||||
consoleTable.print();
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试创建文夹
|
||||
*/
|
||||
@Test
|
||||
public void testMkdir() {
|
||||
System.out.println(execution.mkdir("jhadmin", "$HOME/test", true));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 新建文件夹不强制新建
|
||||
*/
|
||||
@Test
|
||||
public void testMkdirNoForce() {
|
||||
System.out.println(execution.mkdir("lqyan", "/apps/JHDP/lqyan/temp/bbb/ddd"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试新建文件
|
||||
* <p>
|
||||
* 当前接口请求执行失败
|
||||
* </p>
|
||||
*/
|
||||
@Test
|
||||
public void testMkFile() {
|
||||
System.out.println(execution.mkFile("lqyan", "/apps/JHDP/lqyan/temp/ddd.txt"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试文件上传 (是否覆盖存在问题)
|
||||
*/
|
||||
@Test
|
||||
public void testUploadFile() throws IOException {
|
||||
File file = new File("C:\\Users\\yanlongqi\\Desktop\\双色球.xls");
|
||||
FileInputStream fileInputStream = new FileInputStream(file);
|
||||
execution.uploadFile("jhadmin", fileInputStream, file.getName(), "$HOME/temp", true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 测试伤上传文件,不覆盖源文件
|
||||
*/
|
||||
@Test
|
||||
public void testUploadFileNoCover() throws IOException {
|
||||
File file = new File("C:\\Users\\yanlongqi\\Desktop\\双色球.xls");
|
||||
FileInputStream fileInputStream = new FileInputStream(file);
|
||||
execution.uploadFile("jhadmin", fileInputStream, file.getName(), "$HOME/temp");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试获取文件下载的链接
|
||||
*/
|
||||
@Test
|
||||
public void testGetFileDownloadUrl() {
|
||||
System.out.println(execution.getFileDownloadUrl("jhadmin", "$HOME/temp/双色球.xls"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCompress() {
|
||||
execution.compress("jhadmin", "$HOME/temp", "$HOME/temp.zip");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUncompress() {
|
||||
execution.uncompress("jhadmin", "$HOME/temp.zip", "$HOME/test");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user