JSch中如何设置文件类型和文件传输方式? [英] How to set file type and file transfer mode in JSch?

查看:171
本文介绍了JSch中如何设置文件类型和文件传输方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Apache Common Net FTPClient 并在上传文件之前使用下面显示的方法设置了我的 ftpClient.

I used Apache Common Net FTPClient and had setup my ftpClient with methods shown below before upload file.

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);

现在我尝试使用 JSch.文件上传之前是否需要相同的设置以及它们的外观?谢谢.

Now I try to use JSch. Is there same setting needed before file uploading and how they looks like? Thanks.

推荐答案

首先从你的问题看不清楚,你是否知道 JSch 和 Apache Commons Net FtpClient 各自使用完全不同且不相关的协议.JSch 是一个 SSH/SFTP 客户端,而 FtpClient 是一个 FTP 客户端.

First of all, it is not clear from your question, whether you are aware that JSch and Apache Commons Net FtpClient each use completely different and unrelated protocols. JSch is an SSH/SFTP client, while FtpClient is an FTP client.

JSch 使用 SFTP 协议版本 3.

JSch uses an SFTP protocol version 3.

在 SFTP 协议版本 3 中,没有传输模式.或者换句话说,只有二进制传输模式.

In the SFTP protocol version 3, there are no transfer modes. Or in other words, there is only the binary transfer mode.

即使 JSch 使用了支持 ascii 模式的较新版本的 SFTP 协议,二进制模式仍然是 SFTP 中的默认模式.

And even if JSch used a newer version of SFTP protocol, which supports the ascii mode, the binary mode is the default anyway in SFTP.

因此您无需执行任何操作.

这与 FTP 协议形成对比,FTP 协议默认为 ascii 模式.因此,您已通过 FTP 显式切换到 binary 模式(使用 FTPClient.setFileType(如果是 Apache Commons Net).

This is in a contrast to the FTP protocol, which defaults to the ascii mode. So you have explicitly switch to the binary mode with FTP (using the FTPClient.setFileType in case of the Apache Commons Net).

至于 FTPClient.setFileTransferMode – 您的 FTP 代码错误.该方法接受 *_TRANSFER_MODE 常量之一.绝不是 *_FILE_TYPE.如果您使用 BINARY_FILE_TYPE,则会导致向服务器发送无效的 MODE I 命令.这肯定会失败,因此根本没有任何影响.检查方法结果代码,它肯定返回false.

As for the FTPClient.setFileTransferMode – Your FTP code is wrong. The method accepts one of the *_TRANSFER_MODE constants. Never the *_FILE_TYPE. If you use BINARY_FILE_TYPE, it results in sending an invalid MODE I command to the server. Which for sure fails, and consequently has no effect at all. Check the method result code, it for sure returns false.

从您的 FTP 代码中删除呼叫.无需调用FTPClient.setFileTransferMode.Apache Commons Net 仅支持默认的stream"模式(大多数 FTP 服务器也不支持任何其他模式).

Remove the call from your FTP code. There is no need to call FTPClient.setFileTransferMode. Apache Commons Net supports only the default "stream" mode anyway (and most FTP servers do not support any other mode either).

注意名称中的混淆.而ASCII/二进制通常称为传输模式".在 FTP 协议规范(RFC 959 的第 3.1.1 节)中,它们实际上被称为数据类型"并使用 TYPE 命令进行管理.

Note the confusion in the names. While the ascii/binary are commonly called "transfer modes". In FTP protocol specification (section 3.1.1 of RFC 959), they are actually called "data types" and are governed using TYPE command.

虽然 FTP 规范所称的传输模式"由 MODE 命令(第 3.4 节)管理,但它们是完全不同类型的模式:流/块/压缩.他们几乎从未被设定或什至被提及.

While what FTP specification calls "transmission modes", governed by MODE command (section 3.4), are completely different kind of modes: stream/block/compressed. They are hardly ever set or even mentioned.

由于 Apache Commons Net FtpClient 混淆地命名了设置传输模式"的方法;.setFileTransferMode,它经常会导致像你一样的错误——人们错误地使用 FTPClient.setFileTransferMode 在二进制/ascii 数据类型"之间切换em>.

As Apache Commons Net FtpClient confusingly names the method for setting "transmission mode" .setFileTransferMode, it frequently leads to the mistakes like you did – People incorrectly use FTPClient.setFileTransferMode to switch between the binary/ascii "data types".

这篇关于JSch中如何设置文件类型和文件传输方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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