Apache Commons FTP problems
Asked Answered
A

3

5

I want to implement a FTP Client with Apache Commons Net only for uploading data. The Connection and Login to FTP-Server works fine. But the upload does not work right. The files are a little to big as the originals. And the files are damaged. I tried an image, a video and a textfile. Only the textfile is alright.

Now I see while debugging

boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);

gives me false. So it can not be set. Why? (Maybe this is not the problem?)

Here a the rest of my code

client=new FTPClient();

    try {           
        int reply;
        client.connect(url, port);
        reply = client.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }


        client.login(user, pw);
        boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        client.setControlKeepAliveTimeout(300);
        client.enterLocalPassiveMode();

if (client.isConnected())
    {
    try {
        File file=new File(<FILE>);
        FileInputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = client.storeFileStream(file.getName());

          byte[] buffer = new byte[4096];
          int l;
       while((l = inputStream.read(buffer))!=-1)
               {
                outputStream.write(buffer, 0, l);
            }

          inputStream.close();
          outputStream.flush();
          outputStream.close();}
Alegar answered 11/7, 2011 at 13:53 Comment(0)
E
12

Change the following:

boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);

Should be:

boolean xxx=client.setFileType(FTP.BINARY_FILE_TYPE);

You have confused FileTransferModes with FileTypes.

The available FileTypes are:

The available FileTransferModes are:

I suppose if apache introduced enums for these constant types, then this kind of problem could be avoided, but then the library would not be available to pre-java-5 runtimes.
I wonder how much of an issue java 1.4 compatibility really is.

Eada answered 11/7, 2011 at 21:21 Comment(0)
A
2

If only the text file was transferred successfully, I suspect you need to set the binary transfer file type.

See the setFileType method to see how to do this.

The commons-net wiki mentions this is the cause of most file corruption issues.

Age answered 11/7, 2011 at 14:1 Comment(4)
Hi, I tried it, but the method gives me false, so the Mode can not be set.Alegar
You tried this some where after the connect method was called?Age
And also after the login method.Age
He tried the setFileTransferMode, not the setFileType method you suggested. This is a correct answer.Cope
W
0

This work for me, Uploading Image and download after It´s Ok: Using

    FTP.LOCAL_FILE_TYPE

this code using logger, replace for you logger or use System.out.println("");

    private void cargarData(File filelocal) {
    FTPClient client = new FTPClient();

    try {

        client.connect("URLHOSTFTP", "PORT: DEFAULT 21");
        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            logger.error("FTP server refused connection.");
            System.exit(1);
        }
        client.login("USER FTP", "PASS FTP");
        boolean type = client.setFileType(FTP.LOCAL_FILE_TYPE);

        logger.info("Tipo Aceptado:" + type);
        client.setControlKeepAliveTimeout(300);
        client.enterLocalPassiveMode();
        if (client.isConnected()) {
            FileInputStream fis = null;
            fis = new FileInputStream(filelocal);
            client.storeFile(filelocal.getName(), fis);
            client.logout();
            if (fis != null) {
                fis.close();
            }
        }
        logger.info(client.getReplyString());
    } catch (IOException e) {
        logger.error("error" + e.getMessage());
        e.printStackTrace();

    } catch (Exception e) {
        logger.error("error" + e.getMessage());
        e.printStackTrace();

    }
}
Woaded answered 23/6, 2019 at 3:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.