简体   繁体   English

Java-在FTP服务器上存储文件失败

[英]Java - Storing File on FTP Server fails

I am trying to store a byteArrayInputStream as File on a FTP Server . 我正在尝试将byteArrayInputStream作为文件存储在FTP服务器上 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 . 我已经可以连接到服务器并更改工作路径,但是触发将流作为文件存储在服务器上的方法总是返回false

I am using the apache FTPClient. 我正在使用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; 这是另一个看起来与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. 目前还没有办法在Java本身中更改权限,但是还有其他解决方案。 See this thread: How do i programmatically change file permissions? 请参见此线程: 如何以编程方式更改文件权限?

HTH, HTH,

James 詹姆士

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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