我想用 Apache Commons Net 实现一个 FTP 客户端,仅用于上传数据。FTP 服务器的连接和登录工作正常。但是上传不正常。这些文件与原件相比有点大。并且文件已损坏。我尝试了图像、视频和文本文件。只有文本文件没问题。
现在我在调试时看到
boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
给我false
。所以不能设置。为什么?(也许这不是问题?)
这是我的其余代码
client=new FTPClient();
try {
int reply;
client.connect(url, port);
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
client.login(user, pw);
boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
client.setControlKeepAliveTimeout(300);
client.enterLocalPassiveMode();
if (client.isConnected())
{
try {
File file=new File(<FILE>);
FileInputStream inputStream = new FileInputStream(file);
OutputStream outputStream = client.storeFileStream(file.getName());
byte[] buffer = new byte[4096];
int l;
while((l = inputStream.read(buffer))!=-1)
{
outputStream.write(buffer, 0, l);
}
inputStream.close();
outputStream.flush();
outputStream.close();}