0

我想使用 FTP 在服务器上上传一个 .class 文件(ABC.class)。

试过:1>

public boolean uploadFileOnFTPServer(File file, String uploadToPath) {
    boolean isUploaded = false;
    try {
        FileInputStream bis = new FileInputStream(file);
        if (connectToFTPServer()) {
            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(), bis)) {
                        bis.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;
}

也试过:

2>

InputStream bis = new FileInputStream(file);

3>

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

该文件正在上传到服务器上,但其已损坏或内容未正确存储在文件中。

任何人都可以请帮助。

4

1 回答 1

0

这有效:

在连接到服务器后但在传输文件之前添加 setFileType()。

                    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);                 
                    String remoteFileName = file.getName();
                    if (ftpClient.storeFile(remoteFileName, inputStream)) {
                        inputStream.close();
                        System.out.println("File " + file.getName() + " uploaded to server.");
                        isUploaded = true;
                    }

非常感谢 EJP 帮助我解决这个问题 :)

于 2014-04-21T06:53:51.463 回答