简体   繁体   中英

Java - Storing File on FTP Server fails

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM