FTPClient(commons net)下载和注销不起作用

时间:2013-03-01 11:54:49

标签: java android apache-commons-net

我为我的英语道歉。

  1. 我使用以下代码将文件从ftp主机下载到Tablet。当我使用Wi-Fi时,一切运行良好。但是当我尝试使用移动互联网下载文件时,下载会停止,但并非总是如此,有时会正常完成。我发现stop始终是“retrieveFile”或“logout”,程序来到这些命令而没有进一步,只是站着,数据传输的图标不闪烁,停止随机发生。我尝试使用不同的移动运营商,但没有区别。可能是什么原因?

  2. 还有一个问题,但不是那么重要,我还没有找到如何获取文件大小,并使用了我的决定,也许还有另一种方法来获取文件大小?

    private void downloadFile(final String url,final String Message,final String Message2,final Uri uri){     final ProgressDialog progressDialog = new ProgressDialog(this);     新的AsyncTask(){            private Exception m_error = null;

           @Override
           protected void onPreExecute() {
            progressDialog.setMessage(Message);
            progressDialog.setCancelable(true);             
            progressDialog.setMax(100);
            progressDialog
              .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);             
            progressDialog.show();
           }
    
           @Override
           protected File doInBackground(String... params) {         
                     FileOutputStream fos = null;                        
                     File file = null;
                     FTPClient client = null;                       
                          try{  
                           client = new FTPClient();
                           client.connect(ftp_host,Integer.parseInt(ftp_port));
                           client.login(ftp_user, ftp_password);
                           client.enterLocalPassiveMode();
                           client.setFileType(FTP.BINARY_FILE_TYPE);
                           String stat = "";
                           if (url.equals("MobiTrade.apk")){
                               client.changeWorkingDirectory("/var/srv/home/user/mobitrade/update/");}
                        file = new File(Environment.getExternalStorageDirectory(),                     
                                   "/MobiTrade/update/MobiTrade.apk");}
                               stat = client.getStatus("/var/srv/home/user/mobitrade/update/MobiTrade.apk");                                                                   
                           else {   
                               client.changeWorkingDirectory("/var/srv/home/user/mobitrade/"+number+"/out/");
                               file = new File(Environment.getExternalStorageDirectory(),
                                   "/MobiTrade/in/"+url);
                        if (url.equals("message.csv")) file.delete();                               
                               stat = client.getStatus("/var/srv/home/user/mobitrade/"+number+"/out/"+url);                                                                
                         } 
    
                           final Integer FileSize;
                           if (stat.length() >= 64) {
                    stat = stat.substring(49,64);
                    stat = stat.trim();
                    FileSize = Integer.parseInt(stat);
                    }                                                   
                          else {
                    FileSize = 0;
                    }
    
                           fos = new FileOutputStream(file);
    
                           CountingOutputStream cos = new CountingOutputStream(fos){                                   
                               protected void beforeWrite(int n){
                                        super.beforeWrite(n);
                                        publishProgress(getCount(), FileSize);
                               }
                           };                              
    
                           if (url.equals("MobiTrade.apk")){
                               client.retrieveFile("/var/srv/home/user/mobitrade/update/MobiTrade.apk", cos);
                                }   
                           else {       
                               client.retrieveFile("/var/srv/home/user/mobitrade/"+number+"/out/"+url, cos);
                                }           
                       if (url.equals("message.csv")){
                           client.deleteFile("/var/srv/home/user/mobitrade/"+number+"/out/"+url);                                  
                       }            
    
                           client.logout();                             
    
                    }
                          catch (Exception e){
                           e.printStackTrace();
                          }
                             finally{
                                  try{
                                   if (fos != null) fos.close();
                                   if (client.isConnected()) {
                                        client.disconnect();
                                    }
                                  }
                                  catch (IOException e){
                                   e.printStackTrace();
                                  }
                             }
                          return file;
                    }
    
           protected void onProgressUpdate(Integer... values) {
            progressDialog
              .setProgress((int) ((values[0] / (float) values[1]) * 100));
           };
    
           @Override
           protected void onPostExecute(File result) {
            if (m_error != null) {
             m_error.printStackTrace();
             return;
            }
            progressDialog.hide();
            if (url.equals("settings.csv"))
                ProcessSettings(url);
            else if (url.equals("MobiTrade.apk"))
                ProcessUpdate();
            else
                ProcessData(url, Message2, uri);
           }
          }.execute(url);
         }
    
  3. 任何帮助都会很明显。

0 个答案:

没有答案