在SFTP中写入文件而不传输文件。

huangapple 未分类评论43阅读模式
英文:

Write to file in sftp without transfer the file

问题

我正在寻找一种在SFTP中写入文件而无需传输文件的方法。我使用jsch进行操作,我发现使用chilkat也有一种方法可以实现:

CkSFtp sftp = new CkSFtp();
sftp.WriteFileText(handle, "ansi", "Hey !");
sftp.CloseHandle(handle);

但是使用jsch,我仍然没有找到一种实现这个的方法。而且我无法让chilkat库正常工作。

那么,你有解决方案吗?

英文:

I'm looking for a way to write in a file in sftp without transfer the file. I work with jsch and i saw that with chilkat there was a way to do this :

CkSFtp sftp = new CkSFtp();
sftp.WriteFileText(handle, "ansi", "Hey !");
sftp.CloseHandle(handle);

but with jsch i still haven't figured out a way to do it. And I couldn't get the chilkat library to work.

So, do you have solutions ?

答案1

得分: 0

我假设您想要上传内存中的数据/文本。JSch有很多方法可以上传内存中的数据。

其中基本的一个是:

public void put(InputStream src, String dst) throws SftpException

要使用上述方法上传文本字符串,您可以使用以下代码:

InputStream src = new ByteArrayInputStream("Hey !".getBytes(StandardCharsets.UTF_8));
sftp.put(src, "/remote/path/file.txt");

另一个选项是:

public OutputStream put(String dst) throws SftpException

关于您的措辞:正如@RealSkeptic已经评论的那样,无论源是文件与否,您总是传输相同的数据。因此,您不能在没有“传输”的情况下进行上传。SFTP协议和服务器不关心您从哪里获取数据。

英文:

I assume you want to upload in-memory data/text. JSch has loads of methods can upload in-memory data.

The basic one is:

public void put(InputStream src, String dst) throws SftpException

To upload text string using the above method, you can use:

InputStream src = new ByteArrayInputStream("Hey !".getBytes(StandardCharsets.UTF_8));
sftp.put(src, "/remote/path/file.txt");

The other options is:

public OutputStream put(String dst) throws SftpException

Regarding your wording: As @RealSkeptic already commented, you always transfer the same data, no matter if the source is a file or not. So you cannot upload without "transfer". The SFTP protocol nor the server do not care where you take the data from.

huangapple
  • 本文由 发表于 2020年6月29日 17:22:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/62635034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定