mirror of
https://github.com/yanlongqi/jhinno-openapi-java-sdk.git
synced 2026-03-22 06:15:10 +08:00
feat(文件下载): 文件下载支持强制下载和文件流的获取
This commit is contained in:
@@ -15,6 +15,7 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -280,11 +281,24 @@ public class JHFileApiExecution implements JHApiExecution {
|
|||||||
* @return 文件地址信息
|
* @return 文件地址信息
|
||||||
*/
|
*/
|
||||||
public String getFileDownloadUrl(String username, String filePath) {
|
public String getFileDownloadUrl(String username, String filePath) {
|
||||||
|
return getFileDownloadUrl(username, filePath, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件下载地址
|
||||||
|
*
|
||||||
|
* @param username 用户名
|
||||||
|
* @param filePath 文件路径
|
||||||
|
* @param forceDownload 是否强制下载,打开密级之后未标密的文件无法下载,可以通过设置当前参数为true来强制下载,默认:false
|
||||||
|
* @return 文件地址信息
|
||||||
|
*/
|
||||||
|
public String getFileDownloadUrl(String username, String filePath, Boolean forceDownload) {
|
||||||
if (StringUtils.isBlank(filePath)) {
|
if (StringUtils.isBlank(filePath)) {
|
||||||
throw new ArgsException("filePath不能为空!");
|
throw new ArgsException("filePath不能为空!");
|
||||||
}
|
}
|
||||||
Map<String, Object> params = new HashMap<>(1);
|
Map<String, Object> params = new HashMap<>(1);
|
||||||
params.put("filePath", filePath);
|
params.put("filePath", filePath);
|
||||||
|
params.put("forceDownload", forceDownload);
|
||||||
String path = JHApiClient.getUrl(FilePathConstant.FILE_DOWNLOAD_PATH, params);
|
String path = JHApiClient.getUrl(FilePathConstant.FILE_DOWNLOAD_PATH, params);
|
||||||
Map<String, String> downloadInfo = execution.get(path, username,
|
Map<String, String> downloadInfo = execution.get(path, username,
|
||||||
new TypeReference<ResponseResult<Map<String, String>>>() {
|
new TypeReference<ResponseResult<Map<String, String>>>() {
|
||||||
@@ -295,6 +309,32 @@ public class JHFileApiExecution implements JHApiExecution {
|
|||||||
return downloadInfo.get("url");
|
return downloadInfo.get("url");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件输入流
|
||||||
|
*
|
||||||
|
* @param username 用户名
|
||||||
|
* @param filePath 文件路径
|
||||||
|
* @return 文件流
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public InputStream getFileInputStream(String username, String filePath) throws IOException {
|
||||||
|
return getFileInputStream(username, filePath, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件输入流
|
||||||
|
*
|
||||||
|
* @param username 用户名
|
||||||
|
* @param filePath 文件路径
|
||||||
|
* @param forceDownload 是否强制下载,打开密级之后未标密的文件无法下载,可以通过设置当前参数为true来强制下载,默认:false
|
||||||
|
* @return 文件流
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public InputStream getFileInputStream(String username, String filePath, Boolean forceDownload) throws IOException {
|
||||||
|
String fileUrl = getFileDownloadUrl(username, filePath, forceDownload);
|
||||||
|
return execution.getJhApiClient().getApiHttpClient().get(fileUrl, null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件压缩
|
* 文件压缩
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package com.jhinno.sdk.openapi.test.file;
|
package com.jhinno.sdk.openapi.test.file;
|
||||||
|
|
||||||
|
|
||||||
import com.jhinno.sdk.openapi.api.file.FileInfo;
|
import com.jhinno.sdk.openapi.api.file.FileInfo;
|
||||||
import com.jhinno.sdk.openapi.api.file.JHFileApiExecution;
|
import com.jhinno.sdk.openapi.api.file.JHFileApiExecution;
|
||||||
import com.jhinno.sdk.openapi.test.JHClientConfig;
|
import com.jhinno.sdk.openapi.test.JHClientConfig;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@@ -20,7 +20,6 @@ public class FileApiTest {
|
|||||||
private static final JHFileApiExecution execution = JHClientConfig.API_EXECUTRON_MANAGE
|
private static final JHFileApiExecution execution = JHClientConfig.API_EXECUTRON_MANAGE
|
||||||
.getApiExecution(JHFileApiExecution.class);
|
.getApiExecution(JHFileApiExecution.class);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试重命名文件或文件夹
|
* 测试重命名文件或文件夹
|
||||||
*/
|
*/
|
||||||
@@ -45,7 +44,6 @@ public class FileApiTest {
|
|||||||
execution.copyFile("lqyan", "/apps/JHDP/lqyan/PSUserService.class", "/apps/JHDP/lqyan/temp");
|
execution.copyFile("lqyan", "/apps/JHDP/lqyan/PSUserService.class", "/apps/JHDP/lqyan/temp");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试获取文件列表
|
* 测试获取文件列表
|
||||||
*/
|
*/
|
||||||
@@ -63,7 +61,6 @@ public class FileApiTest {
|
|||||||
System.out.println(execution.mkdir("jhadmin", "$HOMEtest1", true));
|
System.out.println(execution.mkdir("jhadmin", "$HOMEtest1", true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新建文件夹不强制新建
|
* 新建文件夹不强制新建
|
||||||
*/
|
*/
|
||||||
@@ -72,7 +69,6 @@ public class FileApiTest {
|
|||||||
System.out.println(execution.mkdir("lqyan", "/apps/JHDP/lqyan/temp/bbb/ddd"));
|
System.out.println(execution.mkdir("lqyan", "/apps/JHDP/lqyan/temp/bbb/ddd"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试新建文件(接口执行失败)
|
* 测试新建文件(接口执行失败)
|
||||||
*/
|
*/
|
||||||
@@ -81,7 +77,6 @@ public class FileApiTest {
|
|||||||
System.out.println(execution.mkFile("lqyan", "/apps/JHDP/lqyan/temp/ddd.txt"));
|
System.out.println(execution.mkFile("lqyan", "/apps/JHDP/lqyan/temp/ddd.txt"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试文件上传 (是否覆盖存在问题)
|
* 测试文件上传 (是否覆盖存在问题)
|
||||||
*/
|
*/
|
||||||
@@ -92,7 +87,6 @@ public class FileApiTest {
|
|||||||
execution.uploadFile("jhadmin", fileInputStream, file.getName(), "$HOME", true);
|
execution.uploadFile("jhadmin", fileInputStream, file.getName(), "$HOME", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试上传文件,(不覆盖源文件,如果isCover是true,上传后的文件有数字下标)
|
* 测试上传文件,(不覆盖源文件,如果isCover是true,上传后的文件有数字下标)
|
||||||
*/
|
*/
|
||||||
@@ -111,13 +105,22 @@ public class FileApiTest {
|
|||||||
System.out.println(execution.getFileDownloadUrl("jhadmin", "$HOME/aa2a.sh"));
|
System.out.println(execution.getFileDownloadUrl("jhadmin", "$HOME/aa2a.sh"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetFileStream() throws IOException {
|
||||||
|
InputStream in = execution.getFileInputStream("jhadmin", "$HOME/aaa.sh");
|
||||||
|
while (in.available() > 0) {
|
||||||
|
// 读取文件内容
|
||||||
|
int read = in.read();
|
||||||
|
System.out.println("读取文件内容:" + read);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCompress() {
|
public void testCompress() {
|
||||||
execution.compress("jhadmin", "$HOME/temp", "$HOME/temp.zip");
|
execution.compress("jhadmin", "$HOME/temp", "$HOME/temp.zip");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUncompress() {
|
public void testUncompress() {
|
||||||
execution.uncompress("jhadmin", "$HOME/temp.zip", "$HOME/test");
|
execution.uncompress("jhadmin", "$HOME/temp.zip", "$HOME/test");
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public class JobApiTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testGetHistoryJobs() {
|
public void testGetHistoryJobs() {
|
||||||
PageJobInfo pages = execution.getHistoryJobs("jhadmin", 1, 5, null, null, null);
|
PageJobInfo pages = execution.getHistoryJobs("jhadmin", 1, 5, null, JobStatusEnum.DONE, null);
|
||||||
System.out.println(pages);
|
System.out.println(pages);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user