我已经编写了一个java代码来上传ftp服务器上的文件...但是它不适用于端口2121 ......我已经在FileZilla和WINScp中测试了它...它在那里工作得很好..如果我使用端口21的服务器,我的代码工作正常... 我在Java文件(FtpFileUpload.java)中的代码如下:
//FtpFileUpload.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
public class FtpFileUpload {
private static final int BUFFER_SIZE = 4096;
public static void main(String[] args) {
//Following Ftp String working... Active Port 21
//String ftpString = "ftp://username:password@host.com:21/FtpFileTest.txt;type=i";
//Following Ftp String not working... Passive Port 2121
String ftpString = "ftp://username:password@host.com:2121/FtpFileTest.txt;type=i";
String filePath = "C:/Java/Test.txt";
System.out.println("Upload URL: " + ftpString);
try {
URL url = new URL(ftpString);
URLConnection conn = url.openConnection();
OutputStream outputStream = conn.getOutputStream();
FileInputStream inputStream = new FileInputStream(filePath);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
System.out.println("File uploaded");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}