java jsch 设置编码格式,在Jsch中设置ASCII模式

在使用JSch进行SFTP传输时遇到Unix(LF)到Windows(CRLF)文件格式问题。由于JSch仅支持SFTP协议版本3,不包含文本模式标志,无法直接处理行结束符转换。作者尝试将文件读取为ASCII字符串并传输,但不确定是否有效。OpenSSH等常见SFTP服务器也不支持该转换。因此,需要寻找其他解决方案来处理行结束符差异。
摘要由CSDN通过智能技术生成

I need to overcome a Unix - Windows file format (LF to CRLF) issue I'm currently having.

The ftp client I am using is Jsch from Jcraft.

The documentation online is very bare, although I have come across a flag that can be set

SSH_FXF_TEXT_MODE

that enables ASCII mode, but I don't see where I should set this in the code itself, nor do I see it mentioned in these Javadocs

Below is my own attempt at a workaround.

The "Newly Added" line shows how I take the file and convert it to an ASCII encoded string, which I then transfer across using the channelSftp put method.

Originally I would have just put the file itself across.

final JSch jsch = new JSch();

final Session session = jsch.getSession(username, host);

session.setPassword(password);

session.connect();

final Channel channel = session.openChannel("sftp");

channel.connect();

final ChannelSftp channelSftp = (ChannelSftp) channel;

channelSftp.cd(destDir);

File file = new File(pathName);

String content = FileUtils.readFileToString(file, "US-ASCII"); // Newly Added

channelSftp.put(content, fileToTransfer.getName());

Please note that I omitted exception handling and other practices just for clarity of the code snippet.

Will this workaround succeed, or will Jsch's seemingly default binary mode override the ASCII encoded string and transfer it as usual?

I will test it, I was just wondering if any of you could tell straight off?

Or indeed knew how/where to set the Text_Mode flag! :)

Also, the version of Jsch I am using is jsch-0.1.49.jar.

解决方案

The text mode flag was added to SFTP protocol version 4. Jsch currently supports SFTP protocol version 3, which doesn't specify a text-mode flag.

You can see a list of SFTP specification revisions here. The RFC for protocol version 3 is here. Note that OpenSSH, the most widely used SFTP server, only supports protocol version 3 as well, and doesn't support line terminator conversion. So having the flag in Jsch wouldn't be very useful.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值