无法打开下载的文件

时间:2014-09-29 18:57:35

标签: android ftp download

我尝试从ftp下载apk文件,但下载后无法打开该文件 原始文件和下载的文件大小相同。 我正在使用apache commons。 我的代码:

FTPClient ftpClient = new FTPClient();
        try{
            ftpClient.connect(mServer);        
            ftpClient.login(mUser, mPassword);              ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            BufferedOutputStream FileStream = new BufferedOutputStream(new FileOutputStream(cestaLuHa),8*1024);
            boolean status=ftpClient.retrieveFile("LuHa.apk", desFileStream);
            if(status){
                result = "Downlaoded";
            }
            FileStream.flush();
            ftpClient.logout();
            ftpClient.disconnect();
        }
        catch (IOException e){
            result = "Error";
        }

1 个答案:

答案 0 :(得分:0)

首先下载文件;

FTPClient client = null;
File ExternalDirectory = Environment.getExternalStorageDirectory();
File DownloadFile = new File(ExternalDirectory.getAbsoluteFile()+"//"+"file.apk");
    try {
        client = new FTPClient();
        client.setControlEncoding("UTF-8");
        client.connect(mServer, 21);
        client.login(mUser,mPassword);
        client.enterLocalPassiveMode();
        client.setFileType(FTPClient.BINARY_FILE_TYPE);
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(DownloadFile));
        boolean success = client.retrieveFile("//httpdocs//file.apk", outputStream);
        if (!success) {
            outputStream.close();
            Log.v("Download Status","Not Download");
        } else {
            outputStream.close();
            Log.v("Download Status","Downloaded");
        }

这是打开apk的第二部分;

Uri uri = Uri.fromFile(DownloadFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/apk"); //Maybe it 's different check different website for this
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

最后一件事我必须说,你必须在后台执行此操作,在前景下载3.0的android被阻止后,你应该使用AsyncTask。 希望这对你有帮助