我在 FTP 服务器上上传一个 .class 文件。上传的文件不是空的,但肯定已损坏,因为我无法反编译它。我怀疑在 FTP 服务器上上传 .class 文件有不同的方式。
这是片段:
public boolean uploadFileOnFTPServer(File file, String uploadToPath) {
//file - File stored on local system that is to be uploaded on server
//uploadToPath - Remote server path where the file is to be stored
boolean isUploaded = false;
try {
FileInputStream inputStream = new FileInputStream(file);
//InputStream inputStream = new FileInputStream(file);
if (connectToFTPServer()) { // connectToFTPServer() - method to connect to server
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // ftpClient is an object of FTPClient class
if (ftpClient.login(userName, password)) {
System.out.println("Logged in to server. Username: " + userName);
if (ftpClient.changeWorkingDirectory(uploadToPath)) {
System.out.println("Navigated to path " + uploadToPath);
if (ftpClient.storeFile(file.getName(), inputStream)) {
inputStream.close();
System.out.println("File " + file.getName() + " uploaded to server.");
isUploaded = true;
}
}
}
} catch (Exception e) {
strRemarks = "Exception reported: Unable to upload file. Error Details: " + e.toString();
System.out.println(strRemarks);
e.printStackTrace();
} finally {
disconnectFTPServer();
}
return isUploaded;
}