开发者

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

        // st开发者_JS百科oring the file returns false
        result = ftpClient.storeFile(filename, byteArrayInputStream);

        byteArrayInputStream.close();
        ftpClient.logout();
    } catch (...) {
        ...
    } finally {
        // disconnect returns true
        disconnectFromFtpServer();
    }


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜