1 回答
TA贡献1895条经验 获得超7个赞
首先,你不能直接在FTP上写文件。您需要将其写入本地,然后将其存储在 FTP 上。试试这个,它对我有用。
public static void main(String[] args) {
String server = "ftp://ftp.dlptest.com/";
int port = 21;
String user = "dlpuser@dlptest.com";
String pass = "fLDScD4Ynth0p4OJ6bW6qCxjh";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
File firstLocalFile = new File("C:/Users/Administrator/Desktop/Book1.xlsx");
String firstRemoteFile = "Book1.xlsx";
InputStream inputStream = new FileInputStream(firstLocalFile);
System.out.println("Start uploading first file");
boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
inputStream.close();
if (done) {
System.out.println("The first file is uploaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
添加回答
举报