mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
commit
be1aeab2c7
@ -728,6 +728,48 @@ public class CommonsFtp extends AbstractFtp {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取FTP服务器上的文件为输入流
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @return {@link InputStream}
|
||||
*/
|
||||
@Override
|
||||
public InputStream read(String path) {
|
||||
final String fileName = FileNameUtil.getName(path);
|
||||
final String dir = StrUtil.removeSuffix(path, fileName);
|
||||
return read(dir, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件为输入流
|
||||
*
|
||||
* @param dir 服务端的文件目录
|
||||
* @param fileName 服务端的文件名
|
||||
* @return {@link InputStream}
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public InputStream read(String dir, String fileName) throws IORuntimeException {
|
||||
String pwd = null;
|
||||
if (isBackToPwd()) {
|
||||
pwd = pwd();
|
||||
}
|
||||
|
||||
if (!cd(dir)) {
|
||||
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", dir);
|
||||
}
|
||||
try {
|
||||
client.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||
return client.retrieveFileStream(fileName);
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
} finally {
|
||||
if (isBackToPwd()) {
|
||||
cd(pwd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取FTPClient客户端对象
|
||||
*
|
||||
|
@ -6,6 +6,7 @@ import org.rythmengine.utils.F;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
@ -153,4 +154,12 @@ public interface Ftp extends Closeable {
|
||||
* @since 5.3.5
|
||||
*/
|
||||
void recursiveDownloadFolder(String sourcePath, File destDir);
|
||||
|
||||
/**
|
||||
* 读取FTP服务器上的文件为输入流
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @return {@link InputStream}
|
||||
*/
|
||||
InputStream read(String path);
|
||||
}
|
||||
|
@ -633,6 +633,21 @@ public class JschSftp extends AbstractFtp {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取远程文件流
|
||||
*
|
||||
* @param path 远程文件路径
|
||||
* @return 输入流
|
||||
*/
|
||||
@Override
|
||||
public InputStream read(String path) {
|
||||
try {
|
||||
return getClient().get(path);
|
||||
} catch (final SftpException e) {
|
||||
throw new SshException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
JschUtil.close(this.channel);
|
||||
|
@ -226,6 +226,17 @@ public class SshjSftp extends AbstractFtp {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取远程文件输入流
|
||||
*
|
||||
* @param path 远程文件路径
|
||||
* @return {@link InputStream}
|
||||
*/
|
||||
@Override
|
||||
public InputStream read(String path) {
|
||||
throw new UnsupportedOperationException("sshjSftp不支持读取远程文件输入流!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
|
@ -16,10 +16,13 @@ import org.apache.commons.net.ftp.FTPSClient;
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.extra.ssh.engine.jsch.JschSftp;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.List;
|
||||
|
||||
public class FtpTest {
|
||||
@ -128,6 +131,18 @@ public class FtpTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void readTest() throws Exception {
|
||||
try (final CommonsFtp ftp = CommonsFtp.of("localhost");
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(ftp.read("d://test/read/", "test.txt")))) {
|
||||
String line;
|
||||
while (StrUtil.isNotBlank(line = reader.readLine())) {
|
||||
Console.log(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void existSftpTest() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user