【发布时间】:2014-04-18 07:37:01
【问题描述】:
我正在 FTP 服务器上上传一个 .class 文件。上传的文件不是空的,但肯定已损坏,因为我无法反编译它。我怀疑在 FTP 服务器上上传 .class 文件有不同的方式。
这里是sn-p:
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;
}
【问题讨论】:
-
看起来不错。文件大小一样吗?
-
没有。在本地系统上,.class 的大小是 7013,上传的大小是 7095
-
这表明它是以文本模式上传的,并且发生了 CR/LF 翻译。尝试将文件类型设置移至登录后。
-
非常感谢。它奏效了。
标签: java ftp ftp-client ftp-server