0

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.

Neha S
  • 283
  • 1
  • 3
  • 17
  • You're calling methods and referring to variables none of which are defined here. How are we supposed to help without knowing what's happening offstage? But the most likely problem is that you're sending the file in ASCII (text) mode; you need to use IMAGE (binary) mode. – Ernest Friedman-Hill Apr 16 '14 at 12:22
  • Sorry for inconvenience. `connectToFTPServer() - function to connect to server ftpClient - FTPClient ftpClient = null; disconnectFTPServer() - function to get disconnected from server I tried: ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);` didnt work – Neha S Apr 16 '14 at 12:31
  • Set it to NOT the ASCII file type! The ASCII type is the wrong type, but it's the default! – Ernest Friedman-Hill Apr 16 '14 at 12:45
  • `ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);` Not working :( – Neha S Apr 16 '14 at 12:52
  • Try to determine *how* it is corrupted. Is it the right size? Are certain byte values replaced consistently, or is it just garbage? Use the "od -t x2" command on Linux (for example) to examine the contents of good and corrupted class files. – Ernest Friedman-Hill Apr 18 '14 at 11:44

1 Answers1

0

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

Neha S
  • 283
  • 1
  • 3
  • 17