android用ftp下载多个文件?

时间:2014-12-31 06:39:14

标签: android ftp downloading

我正在尝试从ftp服务器下载多个文件,但我检索到的所有文件都已损坏/这是我正在使用的代码:

我在我的fileslist数组中循环,只从ftp服务器下载我需要的文件

private class get_files extends AsyncTask< Void, Void, Void>{

        protected Void doInBackground(Void... arg0) {
            //Download the files                                                
            try {

                Log.e("FTP", "Starting ftp session");
                FTPClient ftp = new FTPClient();

                Log.e("FTP", "Connecting to FTP");
                ftp.connect("myftpserver");

                if(ftp.login("myuser", "mypassword")){
                    Log.e("FTP", "Was able to connect to FTP");

                    ftp.changeWorkingDirectory("/somedir");
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                    ftp.enterLocalPassiveMode();        
                    ftp.setBufferSize(1024*1024);

                     String dirPath = getFilesDir().getAbsolutePath() + File.separator + "mp3";
                     File path = new File(getFilesDir(),"mp3");

                    for (String file : files){                      

                         File mypath = new File(path,file);             

                         BufferedOutputStream buffout = null;

                        try {
                             buffout = new BufferedOutputStream(new FileOutputStream(mypath));
                             ftp.retrieveFile(file, buffout);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();             
                        } finally {
                                try {
                                    buffout.close();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }                       
                        }
                    }
                }
                } catch (SocketException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } finally {
                     try {
                        ftp.logout();
                        ftp.disconnect();                           
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            return null;
        }                   

0 个答案:

没有答案