无法使用FTP java在服务器上上载.class文件

时间:2014-04-16 12:14:41

标签: java ftp ftp-server

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

尝试: 1个GT;

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));

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

任何人都可以请求帮助。

1 个答案:

答案 0 :(得分: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帮助我:)