【问题标题】:FTP file download problem. Getting readonly file exceptionFTP文件下载问题。获取只读文件异常
【发布时间】:2010-10-21 12:53:14
【问题描述】:
public class FtpDownloadDemo {
public static void Connection(String filename) {
    FTPClient client = new FTPClient();
    FileOutputStream fos = null;

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

        //
        // The remote filename to be downloaded.
        //
       ftpClient.setFileType(FTP.IMAGE_FILE_TYPE);

        fos = new FileOutputStream(filename);

        //
        // Download file from FTP server
        //
        client.retrieveFile("/" + filename, fos);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

}

我正在使用此代码下载一些图像文件。但在 fos = new FileOutputStream(filename); 获取 file.jpeg 是只读文件异常。我正在使用 commons.net jar 文件进行 ftp 连接。请帮助我哪里错了。

【问题讨论】:

  • 这与 Android 有什么关系?文件名在哪里定义?它可能指向您没有写入访问权限的位置。
  • 其实这是一个演示。在我的 android 应用程序中,我为此创建了一个函数。并将文件名作为参数传递。
  • 你需要在manifest中声明适当的权限后,传递一个你可以写的地方的有效文件名,比如外部存储目录。尝试在没有 FTP 的情况下将一些虚拟数据写入文件输出流,您可能会得到相同的错误。

标签: android ftp download


【解决方案1】:

我在创建类时提供了主机、用户名和密码,然后调用它来下载文件。那些家伙是对的,它可能是试图写入根目录或其他东西。我一直在为这个客户端使用 commons-net-2.2.jar。

public void GetFileFTP(String srcFileSpec, String destpath, String destname) {
    File pathSpec = new File(destpath);
    FTPClient client = new FTPClient();
    BufferedOutputStream fos = null;
    try {
        client.connect(mhost);
        client.login(muser, mpass);

        client.enterLocalPassiveMode(); // important!
        client.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
        fos = new BufferedOutputStream(
                              new FileOutputStream(pathSpec.toString()+"/"+destname));
        client.retrieveFile(srcFileSpec, fos);
        }//try 
        catch (IOException e) {
            Log.e("FTP", "Error Getting File");
            e.printStackTrace();
            }//catch
        finally {
            try {
                if (fos != null) fos.close();
                client.disconnect();
                }//try
                catch (IOException e) {
                    Log.e("FTP", "Disconnect Error");
                    e.printStackTrace();
                    }//catch
                }//finally
    Log.v("FTP", "Done");    
    }//getfileFTP

希望这会有所帮助。

帕文斯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多