Setting ASCII mode in Jsch

醉酒当歌 提交于 2019-12-22 09:48:09

问题


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.


回答1:


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 may not be very useful.



来源:https://stackoverflow.com/questions/25791369/setting-ascii-mode-in-jsch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!