Question

I want to upload a .class file (ABC.class) on server using FTP.

Tried: 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;
}

Also tried:

2>

InputStream bis = new FileInputStream(file);

3>

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

The file is getting uploaded on server but its corrupted or content is not getting stored in file correctly.

Any one can please help.

Was it helpful?

Solution

This worked:

Adding setFileType() after getting connected to server but before transfer of file.

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

Thanks a lot to EJP for helping me with this :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top