android - FTP文件下载问题。获取只读文件异常

标签 android ftp download

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 连接。请在我错的地方帮助我。

最佳答案

我在创建类时提供了主机、用户名和密码,然后调用它来下载文件。那些人是对的,它可能正在尝试写入根目录或其他内容。我一直在为这个客户端使用 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

希望这对您有所帮助。

法文

关于android - FTP文件下载问题。获取只读文件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3987650/

相关文章:

android - 我如何启用通过 Webview 下载 PDF

java - 警报对话框不会显示

android - 是否可以询问用户是否要更新 google play 服务?

java - 如何在Java中将FTPFile列表转换为文件列表?

.net - 找不到类型[WinSCP.EnumerateOptions]

python - 通过 AWS Lambda 连接 FTP

android - Android 应用程序是否向后兼容?

android - 隐形列表查看项目 [发布 APK]

file - 强制下载 'data:text/plain' URL

java - 下载的文件有 0 字节