在Jsch中设置ASCII模式 [英] Setting ASCII mode in Jsch

查看:57
本文介绍了在Jsch中设置ASCII模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解决我当前遇到的Unix-Windows文件格式(LF到CRLF)问题. 我正在使用的ftp客户端是Jcraft的Jsch.

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

启用ASCII模式,但是我看不到应该在代码本身中设置的位置,也看不到这些

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

下面是我自己的解决方法. 新添加的"行显示了如何获取文件并将其转换为ASCII编码的字符串,然后使用channelSftp put方法进行传输. 本来我只是将文件本身放在整个文件中.

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.

这种解决方法会成功吗,还是Jsch的看似默认的二进制模式会覆盖ASCII编码的字符串并像往常一样传输它?

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

我将对其进行测试,我只是想知道你们中的任何人是否可以直接说出来? 或者确实知道如何/在何处设置Text_Mode标志! :)

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! :)

此外,我正在使用的Jsch版本是jsch-0.1.49.jar.

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

推荐答案

文本模式标志已添加到SFTP协议版本4.

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.

您可以在此处看到SFTP规范修订列表.协议版本3的RFC是此处.请注意,最广泛使用的SFTP服务器OpenSSH也仅支持协议版本3,并且不支持行终止符转换.因此,在Jsch中放置标志并不是很有用.

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.

这篇关于在Jsch中设置ASCII模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆