2

I am trying to store a byteArrayInputStream as File on a FTP Server. I could already connect to the Server and change the working path, but triggering the method to store the Stream as File on the Server returns always false.

I am using the apache FTPClient.

Can someone please give me a hint where my mistake can be!?

Here the Code:

    String filename = "xyz.xml"

    // connection returns true
    connectToFtpServer(ftpHost, ftpUser, ftpPassword, exportDirectory);

    // byteArray is not void
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);

    try {
        // change returns true
        result = ftpClient.changeWorkingDirectory(exportDirectory);

        // storing the file returns false
        result = ftpClient.storeFile(filename, byteArrayInputStream);

        byteArrayInputStream.close();
        ftpClient.logout();
    } catch (...) {
        ...
    } finally {
        // disconnect returns true
        disconnectFromFtpServer();
    }
0xPixelfrost
  • 10,244
  • 5
  • 39
  • 58
  • 6
    If you connect manually to that same server with that same username and password, can you store a file by that name in that directory? – Ernest Friedman-Hill Sep 20 '11 at 12:19
  • 1
    Ah! I can manually connect, but not store a file . Thanks! It is a permission problem. – 0xPixelfrost Sep 20 '11 at 12:22
  • I think you're using Apache FTPClient, but you should say so in your question. Do you have access to the FTP server logs? You've sketched out your exception handling. Does the storeFile() definitely not throw an exception? – slim Sep 20 '11 at 12:24
  • Yes i am using the Apache FTPClient. I will edit it – 0xPixelfrost Sep 20 '11 at 12:25

2 Answers2

0

It was actually a permission issue due to an invalid usergroup. After adding my user to the usergroup, i was able to store again files.

0xPixelfrost
  • 10,244
  • 5
  • 39
  • 58
0

I don't believe it's your code. Here is another example that looks very similar from kodejava: package org.kodejava.example.commons.net;

import org.apache.commons.net.ftp.FTPClient;
import java.io.FileInputStream;
import java.io.IOException;

public class FileUploadDemo {
public static void main(String[] args) {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;

    try {
        client.connect("ftp.domain.com");
        client.login("admin", "secret");

        //
        // Create an InputStream of the file to be uploaded
        //
        String filename = "Touch.dat";
        fis = new FileInputStream(filename);

        //
        // Store file to server
        //
        client.storeFile(filename, fis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 }
 }

I agree it's file permissions. There is not a way to change permissions in java itself yet, but there are other solutions. See this thread: How do i programmatically change file permissions?

HTH,

James

Community
  • 1
  • 1
James Drinkard
  • 15,342
  • 16
  • 114
  • 137