mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add getFileStream
This commit is contained in:
parent
be1aeab2c7
commit
44c50a9c49
@ -56,6 +56,7 @@ public class CommonsFtp extends AbstractFtp {
|
|||||||
public static final int DEFAULT_PORT = 21;
|
public static final int DEFAULT_PORT = 21;
|
||||||
|
|
||||||
// region ----- of
|
// region ----- of
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造CommonsFtp,匿名登录
|
* 构造CommonsFtp,匿名登录
|
||||||
*
|
*
|
||||||
@ -728,28 +729,22 @@ public class CommonsFtp extends AbstractFtp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取FTP服务器上的文件为输入流
|
|
||||||
*
|
|
||||||
* @param path 文件路径
|
|
||||||
* @return {@link InputStream}
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream read(String path) {
|
public InputStream getFileStream(final String path) {
|
||||||
final String fileName = FileNameUtil.getName(path);
|
final String fileName = FileNameUtil.getName(path);
|
||||||
final String dir = StrUtil.removeSuffix(path, fileName);
|
final String dir = StrUtil.removeSuffix(path, fileName);
|
||||||
return read(dir, fileName);
|
return getFileStream(dir, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取文件为输入流
|
* 读取文件为输入流
|
||||||
*
|
*
|
||||||
* @param dir 服务端的文件目录
|
* @param dir 服务端的文件目录
|
||||||
* @param fileName 服务端的文件名
|
* @param fileName 服务端的文件名
|
||||||
* @return {@link InputStream}
|
* @return {@link InputStream}
|
||||||
* @throws IORuntimeException IO异常
|
* @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;
|
String pwd = null;
|
||||||
if (isBackToPwd()) {
|
if (isBackToPwd()) {
|
||||||
pwd = pwd();
|
pwd = pwd();
|
||||||
@ -761,7 +756,7 @@ public class CommonsFtp extends AbstractFtp {
|
|||||||
try {
|
try {
|
||||||
client.setFileType(FTPClient.BINARY_FILE_TYPE);
|
client.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||||||
return client.retrieveFileStream(fileName);
|
return client.retrieveFileStream(fileName);
|
||||||
} catch (IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
} finally {
|
} finally {
|
||||||
if (isBackToPwd()) {
|
if (isBackToPwd()) {
|
||||||
|
@ -2,7 +2,6 @@ package org.dromara.hutool.extra.ftp;
|
|||||||
|
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.core.util.CharsetUtil;
|
import org.dromara.hutool.core.util.CharsetUtil;
|
||||||
import org.rythmengine.utils.F;
|
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -161,5 +160,5 @@ public interface Ftp extends Closeable {
|
|||||||
* @param path 文件路径
|
* @param path 文件路径
|
||||||
* @return {@link InputStream}
|
* @return {@link InputStream}
|
||||||
*/
|
*/
|
||||||
InputStream read(String path);
|
InputStream getFileStream(String path);
|
||||||
}
|
}
|
||||||
|
@ -633,14 +633,8 @@ public class JschSftp extends AbstractFtp {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取远程文件流
|
|
||||||
*
|
|
||||||
* @param path 远程文件路径
|
|
||||||
* @return 输入流
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream read(String path) {
|
public InputStream getFileStream(final String path) {
|
||||||
try {
|
try {
|
||||||
return getClient().get(path);
|
return getClient().get(path);
|
||||||
} catch (final SftpException e) {
|
} catch (final SftpException e) {
|
||||||
|
@ -14,10 +14,12 @@ package org.dromara.hutool.extra.ssh.engine.sshj;
|
|||||||
|
|
||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
import net.schmizz.sshj.connection.channel.direct.Session;
|
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.RemoteResourceInfo;
|
||||||
import net.schmizz.sshj.sftp.SFTPClient;
|
import net.schmizz.sshj.sftp.SFTPClient;
|
||||||
import net.schmizz.sshj.xfer.FileSystemFile;
|
import net.schmizz.sshj.xfer.FileSystemFile;
|
||||||
import org.dromara.hutool.core.collection.CollUtil;
|
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.io.IoUtil;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.dromara.hutool.extra.ftp.AbstractFtp;
|
import org.dromara.hutool.extra.ftp.AbstractFtp;
|
||||||
@ -233,8 +235,15 @@ public class SshjSftp extends AbstractFtp {
|
|||||||
* @return {@link InputStream}
|
* @return {@link InputStream}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public InputStream read(String path) {
|
public InputStream getFileStream(final String path) {
|
||||||
throw new UnsupportedOperationException("sshjSftp不支持读取远程文件输入流!");
|
final RemoteFile remoteFile;
|
||||||
|
try {
|
||||||
|
remoteFile = sftp.open(path);
|
||||||
|
} catch (final IOException e) {
|
||||||
|
throw new IORuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return remoteFile.new ReadAheadRemoteFileInputStream(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -135,7 +135,7 @@ public class FtpTest {
|
|||||||
@Disabled
|
@Disabled
|
||||||
public void readTest() throws Exception {
|
public void readTest() throws Exception {
|
||||||
try (final CommonsFtp ftp = CommonsFtp.of("localhost");
|
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;
|
String line;
|
||||||
while (StrUtil.isNotBlank(line = reader.readLine())) {
|
while (StrUtil.isNotBlank(line = reader.readLine())) {
|
||||||
Console.log(line);
|
Console.log(line);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user