add getFileStream

This commit is contained in:
Looly 2023-12-23 19:13:12 +08:00
parent be1aeab2c7
commit 44c50a9c49
5 changed files with 20 additions and 23 deletions

View File

@ -56,6 +56,7 @@ public class CommonsFtp extends AbstractFtp {
public static final int DEFAULT_PORT = 21;
// region ----- of
/**
* 构造CommonsFtp匿名登录
*
@ -728,17 +729,11 @@ public class CommonsFtp extends AbstractFtp {
}
}
/**
* 读取FTP服务器上的文件为输入流
*
* @param path 文件路径
* @return {@link InputStream}
*/
@Override
public InputStream read(String path) {
public InputStream getFileStream(final String path) {
final String fileName = FileNameUtil.getName(path);
final String dir = StrUtil.removeSuffix(path, fileName);
return read(dir, fileName);
return getFileStream(dir, fileName);
}
/**
@ -749,7 +744,7 @@ public class CommonsFtp extends AbstractFtp {
* @return {@link InputStream}
* @throws IORuntimeException IO异常
*/
public InputStream read(String dir, String fileName) throws IORuntimeException {
public InputStream getFileStream(final String dir, final String fileName) throws IORuntimeException {
String pwd = null;
if (isBackToPwd()) {
pwd = pwd();
@ -761,7 +756,7 @@ public class CommonsFtp extends AbstractFtp {
try {
client.setFileType(FTPClient.BINARY_FILE_TYPE);
return client.retrieveFileStream(fileName);
} catch (IOException e) {
} catch (final IOException e) {
throw new IORuntimeException(e);
} finally {
if (isBackToPwd()) {

View File

@ -2,7 +2,6 @@ package org.dromara.hutool.extra.ftp;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import org.rythmengine.utils.F;
import java.io.Closeable;
import java.io.File;
@ -161,5 +160,5 @@ public interface Ftp extends Closeable {
* @param path 文件路径
* @return {@link InputStream}
*/
InputStream read(String path);
InputStream getFileStream(String path);
}

View File

@ -633,14 +633,8 @@ public class JschSftp extends AbstractFtp {
return this;
}
/**
* 读取远程文件流
*
* @param path 远程文件路径
* @return 输入流
*/
@Override
public InputStream read(String path) {
public InputStream getFileStream(final String path) {
try {
return getClient().get(path);
} catch (final SftpException e) {

View File

@ -14,10 +14,12 @@ package org.dromara.hutool.extra.ssh.engine.sshj;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.sftp.RemoteFile;
import net.schmizz.sshj.sftp.RemoteResourceInfo;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.xfer.FileSystemFile;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.extra.ftp.AbstractFtp;
@ -233,8 +235,15 @@ public class SshjSftp extends AbstractFtp {
* @return {@link InputStream}
*/
@Override
public InputStream read(String path) {
throw new UnsupportedOperationException("sshjSftp不支持读取远程文件输入流!");
public InputStream getFileStream(final String path) {
final RemoteFile remoteFile;
try {
remoteFile = sftp.open(path);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
return remoteFile.new ReadAheadRemoteFileInputStream(16);
}
@Override

View File

@ -135,7 +135,7 @@ public class FtpTest {
@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")))) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(ftp.getFileStream("d://test/read/", "test.txt")))) {
String line;
while (StrUtil.isNotBlank(line = reader.readLine())) {
Console.log(line);