Android中FTP上传、下载(含进度)

来源:互联网 发布:苏联对中国的援助知乎 编辑:程序博客网 时间:2024/05/17 01:59

声明:本应用为实现UI部分,即没有进度条,只包含功能实现部分

借鉴(http://royvandewater.com/2010/10/uploading-a-file-to-ftp-with-android/),PS:原来还可以这样搞

代码部分主要分为三个文件:MainActivity,FTP,ProgressInputStream

1. MainActivity

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.ftp;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.LinkedList;  
  6.   
  7. import com.ftp.FTP.DeleteFileProgressListener;  
  8. import com.ftp.FTP.DownLoadProgressListener;  
  9. import com.ftp.FTP.UploadProgressListener;  
  10.   
  11. import android.app.Activity;  
  12. import android.os.Bundle;  
  13. import android.os.Message;  
  14. import android.util.Log;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.Button;  
  18. import android.widget.EditText;  
  19. import android.widget.Toast;  
  20.   
  21. public class MainActivity extends Activity {  
  22.   
  23.     private static final String TAG = "MainActivity";  
  24.       
  25.     public static final String FTP_CONNECT_SUCCESSS = "ftp连接成功";  
  26.     public static final String FTP_CONNECT_FAIL = "ftp连接失败";  
  27.     public static final String FTP_DISCONNECT_SUCCESS = "ftp断开连接";  
  28.     public static final String FTP_FILE_NOTEXISTS = "ftp上文件不存在";  
  29.       
  30.     public static final String FTP_UPLOAD_SUCCESS = "ftp文件上传成功";  
  31.     public static final String FTP_UPLOAD_FAIL = "ftp文件上传失败";  
  32.     public static final String FTP_UPLOAD_LOADING = "ftp文件正在上传";  
  33.   
  34.     public static final String FTP_DOWN_LOADING = "ftp文件正在下载";  
  35.     public static final String FTP_DOWN_SUCCESS = "ftp文件下载成功";  
  36.     public static final String FTP_DOWN_FAIL = "ftp文件下载失败";  
  37.       
  38.     public static final String FTP_DELETEFILE_SUCCESS = "ftp文件删除成功";  
  39.     public static final String FTP_DELETEFILE_FAIL = "ftp文件删除失败";  
  40.   
  41.     @Override  
  42.     protected void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.main);  
  45.           
  46.         initView();  
  47.     }  
  48.   
  49.     private void initView() {  
  50.           
  51.         //上传功能  
  52.         //new FTP().uploadMultiFile为多文件上传  
  53.         //new FTP().uploadSingleFile为单文件上传  
  54.         Button buttonUpload = (Button) findViewById(R.id.button_upload);          
  55.         buttonUpload.setOnClickListener(new OnClickListener() {              
  56.             @Override  
  57.             public void onClick(View v) {  
  58.   
  59.                     new Thread(new Runnable() {           
  60.                         @Override  
  61.                         public void run() {  
  62.                               
  63.                             // 上传  
  64.                             File file = new File("/mnt/sdcard/ftpTest.docx");  
  65.                             try {  
  66.                                   
  67.                                 //单文件上传  
  68.                                 new FTP().uploadSingleFile(file, "/fff",new UploadProgressListener(){  
  69.   
  70.                                     @Override  
  71.                                     public void onUploadProgress(String currentStep,long uploadSize,File file) {  
  72.                                         // TODO Auto-generated method stub  
  73.                                         Log.d(TAG, currentStep);                                          
  74.                                         if(currentStep.equals(MainActivity.FTP_UPLOAD_SUCCESS)){  
  75.                                             Log.d(TAG, "-----shanchuan--successful");  
  76.                                         } else if(currentStep.equals(MainActivity.FTP_UPLOAD_LOADING)){  
  77.                                             long fize = file.length();  
  78.                                             float num = (float)uploadSize / (float)fize;  
  79.                                             int result = (int)(num * 100);  
  80.                                             Log.d(TAG, "-----shangchuan---"+result + "%");  
  81.                                         }  
  82.                                     }                             
  83.                                 });  
  84.                             } catch (IOException e) {  
  85.                                 // TODO Auto-generated catch block  
  86.                                 e.printStackTrace();  
  87.                             }  
  88.                               
  89.                         }  
  90.                     }).start();  
  91.                        
  92.             }  
  93.         });  
  94.       
  95.         //下载功能  
  96.         Button buttonDown = (Button)findViewById(R.id.button_down);  
  97.         buttonDown.setOnClickListener(new View.OnClickListener() {            
  98.             @Override  
  99.             public void onClick(View v) {  
  100.                   
  101.                 new Thread(new Runnable() {           
  102.                     @Override  
  103.                     public void run() {  
  104.                           
  105.                         // 下载  
  106.                         try {  
  107.                               
  108.                             //单文件下载  
  109.                             new FTP().downloadSingleFile("/fff/ftpTest.docx","/mnt/sdcard/download/","ftpTest.docx",new DownLoadProgressListener(){  
  110.   
  111.                                 @Override  
  112.                                 public void onDownLoadProgress(String currentStep, long downProcess, File file) {  
  113.                                     Log.d(TAG, currentStep);                                          
  114.                                     if(currentStep.equals(MainActivity.FTP_DOWN_SUCCESS)){  
  115.                                         Log.d(TAG, "-----xiazai--successful");  
  116.                                     } else if(currentStep.equals(MainActivity.FTP_DOWN_LOADING)){  
  117.                                         Log.d(TAG, "-----xiazai---"+downProcess + "%");  
  118.                                     }  
  119.                                 }  
  120.                                   
  121.                             });                           
  122.                           
  123.                         } catch (Exception e) {  
  124.                             // TODO Auto-generated catch block  
  125.                             e.printStackTrace();  
  126.                         }  
  127.                           
  128.                     }  
  129.                 }).start();  
  130.                   
  131.             }  
  132.         });  
  133.       
  134.         //删除功能  
  135.         Button buttonDelete = (Button)findViewById(R.id.button_delete);  
  136.         buttonDelete.setOnClickListener(new View.OnClickListener() {              
  137.             @Override  
  138.             public void onClick(View v) {  
  139.                   
  140.                 new Thread(new Runnable() {           
  141.                     @Override  
  142.                     public void run() {  
  143.                           
  144.                         // 删除  
  145.                         try {  
  146.   
  147.                             new FTP().deleteSingleFile("/fff/ftpTest.docx",new DeleteFileProgressListener(){  
  148.   
  149.                                 @Override  
  150.                                 public void onDeleteProgress(String currentStep) {  
  151.                                     Log.d(TAG, currentStep);                                          
  152.                                     if(currentStep.equals(MainActivity.FTP_DELETEFILE_SUCCESS)){  
  153.                                         Log.d(TAG, "-----shanchu--success");  
  154.                                     } else if(currentStep.equals(MainActivity.FTP_DELETEFILE_FAIL)){  
  155.                                         Log.d(TAG, "-----shanchu--fail");  
  156.                                     }  
  157.                                 }  
  158.                                   
  159.                             });                           
  160.                           
  161.                         } catch (Exception e) {  
  162.                             // TODO Auto-generated catch block  
  163.                             e.printStackTrace();  
  164.                         }  
  165.                           
  166.                     }  
  167.                 }).start();  
  168.                   
  169.             }  
  170.         });  
  171.       
  172.     }  
  173. }  

2. FTP

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.ftp;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10. import java.util.Date;  
  11. import java.util.LinkedList;  
  12. import org.apache.commons.net.ftp.FTPClient;  
  13. import org.apache.commons.net.ftp.FTPClientConfig;  
  14. import org.apache.commons.net.ftp.FTPFile;  
  15. import org.apache.commons.net.ftp.FTPReply;  
  16.   
  17. public class FTP {  
  18.     /** 
  19.      * 服务器名. 
  20.      */  
  21.     private String hostName;  
  22.   
  23.     /** 
  24.      * 端口号 
  25.      */  
  26.     private int serverPort;  
  27.   
  28.     /** 
  29.      * 用户名. 
  30.      */  
  31.     private String userName;  
  32.   
  33.     /** 
  34.      * 密码. 
  35.      */  
  36.     private String password;  
  37.   
  38.     /** 
  39.      * FTP连接. 
  40.      */  
  41.     private FTPClient ftpClient;  
  42.   
  43.     public FTP() {  
  44.         this.hostName = "192.168.1.101";  
  45.         this.serverPort = 21;  
  46.         this.userName = "admin";  
  47.         this.password = "1234";  
  48.         this.ftpClient = new FTPClient();  
  49.     }  
  50.   
  51.     // -------------------------------------------------------文件上传方法------------------------------------------------  
  52.   
  53.     /** 
  54.      * 上传单个文件. 
  55.      *  
  56.      * @param localFile 
  57.      *            本地文件 
  58.      * @param remotePath 
  59.      *            FTP目录 
  60.      * @param listener 
  61.      *            监听器 
  62.      * @throws IOException 
  63.      */  
  64.     public void uploadSingleFile(File singleFile, String remotePath,  
  65.             UploadProgressListener listener) throws IOException {  
  66.   
  67.         // 上传之前初始化  
  68.         this.uploadBeforeOperate(remotePath, listener);  
  69.   
  70.         boolean flag;  
  71.         flag = uploadingSingle(singleFile, listener);  
  72.         if (flag) {  
  73.             listener.onUploadProgress(MainActivity.FTP_UPLOAD_SUCCESS, 0,  
  74.                     singleFile);  
  75.         } else {  
  76.             listener.onUploadProgress(MainActivity.FTP_UPLOAD_FAIL, 0,  
  77.                     singleFile);  
  78.         }  
  79.   
  80.         // 上传完成之后关闭连接  
  81.         this.uploadAfterOperate(listener);  
  82.     }  
  83.   
  84.     /** 
  85.      * 上传多个文件. 
  86.      *  
  87.      * @param localFile 
  88.      *            本地文件 
  89.      * @param remotePath 
  90.      *            FTP目录 
  91.      * @param listener 
  92.      *            监听器 
  93.      * @throws IOException 
  94.      */  
  95.     public void uploadMultiFile(LinkedList<File> fileList, String remotePath,  
  96.             UploadProgressListener listener) throws IOException {  
  97.   
  98.         // 上传之前初始化  
  99.         this.uploadBeforeOperate(remotePath, listener);  
  100.   
  101.         boolean flag;  
  102.   
  103.         for (File singleFile : fileList) {  
  104.             flag = uploadingSingle(singleFile, listener);  
  105.             if (flag) {  
  106.                 listener.onUploadProgress(MainActivity.FTP_UPLOAD_SUCCESS, 0,  
  107.                         singleFile);  
  108.             } else {  
  109.                 listener.onUploadProgress(MainActivity.FTP_UPLOAD_FAIL, 0,  
  110.                         singleFile);  
  111.             }  
  112.         }  
  113.   
  114.         // 上传完成之后关闭连接  
  115.         this.uploadAfterOperate(listener);  
  116.     }  
  117.   
  118.     /** 
  119.      * 上传单个文件. 
  120.      *  
  121.      * @param localFile 
  122.      *            本地文件 
  123.      * @return true上传成功, false上传失败 
  124.      * @throws IOException 
  125.      */  
  126.     private boolean uploadingSingle(File localFile,  
  127.             UploadProgressListener listener) throws IOException {  
  128.         boolean flag = true;  
  129.         // 不带进度的方式  
  130.         // // 创建输入流  
  131.         // InputStream inputStream = new FileInputStream(localFile);  
  132.         // // 上传单个文件  
  133.         // flag = ftpClient.storeFile(localFile.getName(), inputStream);  
  134.         // // 关闭文件流  
  135.         // inputStream.close();  
  136.   
  137.         // 带有进度的方式  
  138.         BufferedInputStream buffIn = new BufferedInputStream(  
  139.                 new FileInputStream(localFile));  
  140.         ProgressInputStream progressInput = new ProgressInputStream(buffIn,  
  141.                 listener, localFile);  
  142.         flag = ftpClient.storeFile(localFile.getName(), progressInput);  
  143.         buffIn.close();  
  144.   
  145.         return flag;  
  146.     }  
  147.       
  148.     /** 
  149.      * 上传文件之前初始化相关参数 
  150.      *  
  151.      * @param remotePath 
  152.      *            FTP目录 
  153.      * @param listener 
  154.      *            监听器 
  155.      * @throws IOException 
  156.      */  
  157.     private void uploadBeforeOperate(String remotePath,  
  158.             UploadProgressListener listener) throws IOException {  
  159.   
  160.         // 打开FTP服务  
  161.         try {  
  162.             this.openConnect();  
  163.             listener.onUploadProgress(MainActivity.FTP_CONNECT_SUCCESSS, 0,  
  164.                     null);  
  165.         } catch (IOException e1) {  
  166.             e1.printStackTrace();  
  167.             listener.onUploadProgress(MainActivity.FTP_CONNECT_FAIL, 0null);  
  168.             return;  
  169.         }  
  170.   
  171.         // 设置模式  
  172.         ftpClient.setFileTransferMode(org.apache.commons.net.ftp.FTP.STREAM_TRANSFER_MODE);  
  173.         // FTP下创建文件夹  
  174.         ftpClient.makeDirectory(remotePath);  
  175.         // 改变FTP目录  
  176.         ftpClient.changeWorkingDirectory(remotePath);  
  177.         // 上传单个文件  
  178.   
  179.     }  
  180.   
  181.     /** 
  182.      * 上传完成之后关闭连接 
  183.      *  
  184.      * @param listener 
  185.      * @throws IOException 
  186.      */  
  187.     private void uploadAfterOperate(UploadProgressListener listener)  
  188.             throws IOException {  
  189.         this.closeConnect();  
  190.         listener.onUploadProgress(MainActivity.FTP_DISCONNECT_SUCCESS, 0null);  
  191.     }  
  192.   
  193.     // -------------------------------------------------------文件下载方法------------------------------------------------  
  194.   
  195.     /** 
  196.      * 下载单个文件,可实现断点下载. 
  197.      *  
  198.      * @param serverPath 
  199.      *            Ftp目录及文件路径 
  200.      * @param localPath 
  201.      *            本地目录 
  202.      * @param fileName        
  203.      *            下载之后的文件名称 
  204.      * @param listener 
  205.      *            监听器 
  206.      * @throws IOException 
  207.      */  
  208.     public void downloadSingleFile(String serverPath, String localPath, String fileName, DownLoadProgressListener listener)  
  209.             throws Exception {  
  210.   
  211.         // 打开FTP服务  
  212.         try {  
  213.             this.openConnect();  
  214.             listener.onDownLoadProgress(MainActivity.FTP_CONNECT_SUCCESSS, 0null);  
  215.         } catch (IOException e1) {  
  216.             e1.printStackTrace();  
  217.             listener.onDownLoadProgress(MainActivity.FTP_CONNECT_FAIL, 0null);  
  218.             return;  
  219.         }  
  220.   
  221.         // 先判断服务器文件是否存在  
  222.         FTPFile[] files = ftpClient.listFiles(serverPath);  
  223.         if (files.length == 0) {  
  224.             listener.onDownLoadProgress(MainActivity.FTP_FILE_NOTEXISTS, 0null);  
  225.             return;  
  226.         }  
  227.   
  228.         //创建本地文件夹  
  229.         File mkFile = new File(localPath);  
  230.         if (!mkFile.exists()) {  
  231.             mkFile.mkdirs();  
  232.         }  
  233.   
  234.         localPath = localPath + fileName;  
  235.         // 接着判断下载的文件是否能断点下载  
  236.         long serverSize = files[0].getSize(); // 获取远程文件的长度  
  237.         File localFile = new File(localPath);  
  238.         long localSize = 0;  
  239.         if (localFile.exists()) {  
  240.             localSize = localFile.length(); // 如果本地文件存在,获取本地文件的长度  
  241.             if (localSize >= serverSize) {  
  242.                 File file = new File(localPath);  
  243.                 file.delete();  
  244.             }  
  245.         }  
  246.           
  247.         // 进度  
  248.         long step = serverSize / 100;  
  249.         long process = 0;  
  250.         long currentSize = 0;  
  251.         // 开始准备下载文件  
  252.         OutputStream out = new FileOutputStream(localFile, true);  
  253.         ftpClient.setRestartOffset(localSize);  
  254.         InputStream input = ftpClient.retrieveFileStream(serverPath);  
  255.         byte[] b = new byte[1024];  
  256.         int length = 0;  
  257.         while ((length = input.read(b)) != -1) {  
  258.             out.write(b, 0, length);  
  259.             currentSize = currentSize + length;  
  260.             if (currentSize / step != process) {  
  261.                 process = currentSize / step;  
  262.                 if (process % 5 == 0) {  //每隔%5的进度返回一次  
  263.                     listener.onDownLoadProgress(MainActivity.FTP_DOWN_LOADING, process, null);  
  264.                 }  
  265.             }  
  266.         }  
  267.         out.flush();  
  268.         out.close();  
  269.         input.close();  
  270.           
  271.         // 此方法是来确保流处理完毕,如果没有此方法,可能会造成现程序死掉  
  272.         if (ftpClient.completePendingCommand()) {  
  273.             listener.onDownLoadProgress(MainActivity.FTP_DOWN_SUCCESS, 0new File(localPath));  
  274.         } else {  
  275.             listener.onDownLoadProgress(MainActivity.FTP_DOWN_FAIL, 0null);  
  276.         }  
  277.   
  278.         // 下载完成之后关闭连接  
  279.         this.closeConnect();  
  280.         listener.onDownLoadProgress(MainActivity.FTP_DISCONNECT_SUCCESS, 0null);  
  281.   
  282.         return;  
  283.     }  
  284.   
  285.     // -------------------------------------------------------文件删除方法------------------------------------------------  
  286.   
  287.     /** 
  288.      * 删除Ftp下的文件. 
  289.      *  
  290.      * @param serverPath 
  291.      *            Ftp目录及文件路径 
  292.      * @param listener 
  293.      *            监听器 
  294.      * @throws IOException 
  295.      */  
  296.     public void deleteSingleFile(String serverPath, DeleteFileProgressListener listener)  
  297.             throws Exception {  
  298.   
  299.         // 打开FTP服务  
  300.         try {  
  301.             this.openConnect();  
  302.             listener.onDeleteProgress(MainActivity.FTP_CONNECT_SUCCESSS);  
  303.         } catch (IOException e1) {  
  304.             e1.printStackTrace();  
  305.             listener.onDeleteProgress(MainActivity.FTP_CONNECT_FAIL);  
  306.             return;  
  307.         }  
  308.   
  309.         // 先判断服务器文件是否存在  
  310.         FTPFile[] files = ftpClient.listFiles(serverPath);  
  311.         if (files.length == 0) {  
  312.             listener.onDeleteProgress(MainActivity.FTP_FILE_NOTEXISTS);  
  313.             return;  
  314.         }  
  315.           
  316.         //进行删除操作  
  317.         boolean flag = true;  
  318.         flag = ftpClient.deleteFile(serverPath);  
  319.         if (flag) {  
  320.             listener.onDeleteProgress(MainActivity.FTP_DELETEFILE_SUCCESS);  
  321.         } else {  
  322.             listener.onDeleteProgress(MainActivity.FTP_DELETEFILE_FAIL);  
  323.         }  
  324.           
  325.         // 删除完成之后关闭连接  
  326.         this.closeConnect();  
  327.         listener.onDeleteProgress(MainActivity.FTP_DISCONNECT_SUCCESS);  
  328.           
  329.         return;  
  330.     }  
  331.   
  332.     // -------------------------------------------------------打开关闭连接------------------------------------------------  
  333.   
  334.     /** 
  335.      * 打开FTP服务. 
  336.      *  
  337.      * @throws IOException 
  338.      */  
  339.     public void openConnect() throws IOException {  
  340.         // 中文转码  
  341.         ftpClient.setControlEncoding("UTF-8");  
  342.         int reply; // 服务器响应值  
  343.         // 连接至服务器  
  344.         ftpClient.connect(hostName, serverPort);  
  345.         // 获取响应值  
  346.         reply = ftpClient.getReplyCode();  
  347.         if (!FTPReply.isPositiveCompletion(reply)) {  
  348.             // 断开连接  
  349.             ftpClient.disconnect();  
  350.             throw new IOException("connect fail: " + reply);  
  351.         }  
  352.         // 登录到服务器  
  353.         ftpClient.login(userName, password);  
  354.         // 获取响应值  
  355.         reply = ftpClient.getReplyCode();  
  356.         if (!FTPReply.isPositiveCompletion(reply)) {  
  357.             // 断开连接  
  358.             ftpClient.disconnect();  
  359.             throw new IOException("connect fail: " + reply);  
  360.         } else {  
  361.             // 获取登录信息  
  362.             FTPClientConfig config = new FTPClientConfig(ftpClient  
  363.                     .getSystemType().split(" ")[0]);  
  364.             config.setServerLanguageCode("zh");  
  365.             ftpClient.configure(config);  
  366.             // 使用被动模式设为默认  
  367.             ftpClient.enterLocalPassiveMode();  
  368.             // 二进制文件支持  
  369.             ftpClient  
  370.                     .setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);  
  371.         }  
  372.     }  
  373.   
  374.     /** 
  375.      * 关闭FTP服务. 
  376.      *  
  377.      * @throws IOException 
  378.      */  
  379.     public void closeConnect() throws IOException {  
  380.         if (ftpClient != null) {  
  381.             // 退出FTP  
  382.             ftpClient.logout();  
  383.             // 断开连接  
  384.             ftpClient.disconnect();  
  385.         }  
  386.     }  
  387.   
  388.     // ---------------------------------------------------上传、下载、删除监听---------------------------------------------  
  389.       
  390.     /* 
  391.      * 上传进度监听 
  392.      */  
  393.     public interface UploadProgressListener {  
  394.         public void onUploadProgress(String currentStep, long uploadSize, File file);  
  395.     }  
  396.   
  397.     /* 
  398.      * 下载进度监听 
  399.      */  
  400.     public interface DownLoadProgressListener {  
  401.         public void onDownLoadProgress(String currentStep, long downProcess, File file);  
  402.     }  
  403.   
  404.     /* 
  405.      * 文件删除监听 
  406.      */  
  407.     public interface DeleteFileProgressListener {  
  408.         public void onDeleteProgress(String currentStep);  
  409.     }  
  410.   
  411. }  

3. ProgressInputStream

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.ftp;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6.   
  7. import com.ftp.FTP.UploadProgressListener;  
  8.   
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.os.Message;  
  12. import android.util.Log;  
  13.   
  14. public class ProgressInputStream extends InputStream {  
  15.   
  16.     private static final int TEN_KILOBYTES = 1024 * 10;  //每上传10K返回一次  
  17.   
  18.     private InputStream inputStream;  
  19.   
  20.     private long progress;  
  21.     private long lastUpdate;  
  22.   
  23.     private boolean closed;  
  24.       
  25.     private UploadProgressListener listener;  
  26.     private File localFile;  
  27.   
  28.     public ProgressInputStream(InputStream inputStream,UploadProgressListener listener,File localFile) {  
  29.         this.inputStream = inputStream;  
  30.         this.progress = 0;  
  31.         this.lastUpdate = 0;  
  32.         this.listener = listener;  
  33.         this.localFile = localFile;  
  34.           
  35.         this.closed = false;  
  36.     }  
  37.   
  38.     @Override  
  39.     public int read() throws IOException {  
  40.         int count = inputStream.read();  
  41.         return incrementCounterAndUpdateDisplay(count);  
  42.     }  
  43.   
  44.     @Override  
  45.     public int read(byte[] b, int off, int len) throws IOException {  
  46.         int count = inputStream.read(b, off, len);  
  47.         return incrementCounterAndUpdateDisplay(count);  
  48.     }  
  49.   
  50.     @Override  
  51.     public void close() throws IOException {  
  52.         super.close();  
  53.         if (closed)  
  54.             throw new IOException("already closed");  
  55.         closed = true;  
  56.     }  
  57.   
  58.     private int incrementCounterAndUpdateDisplay(int count) {  
  59.         if (count > 0)  
  60.             progress += count;  
  61.         lastUpdate = maybeUpdateDisplay(progress, lastUpdate);  
  62.         return count;  
  63.     }  
  64.   
  65.     private long maybeUpdateDisplay(long progress, long lastUpdate) {  
  66.         if (progress - lastUpdate > TEN_KILOBYTES) {  
  67.             lastUpdate = progress;  
  68.             this.listener.onUploadProgress(MainActivity.FTP_UPLOAD_LOADING, progress, this.localFile);  
  69.         }  
  70.         return lastUpdate;  
  71.     }  
  72.       
  73.     
  74.       
  75. }  


源代码下载:http://download.csdn.net/detail/tianyitianyi1/7770415

0 0