当前位置: 代码迷 >> Android >> 使用commons-net-ftp-2.0.jar进行FTP远程下载时遇到的有关问题
  详细解决方案

使用commons-net-ftp-2.0.jar进行FTP远程下载时遇到的有关问题

热度:36   发布时间:2016-05-01 21:14:57.0
使用commons-net-ftp-2.0.jar进行FTP远程下载时遇到的问题
代码如下,
Java code
public class FTPTransferTask extends FTPTransferAstnTask {    /** */    private FTPClient mFTPClient = null;    private long lRemoteSize = 0L;    private long size= 0L;    private OutputStream out = null;    private InputStream in = null;    private byte[] bytes;    private int c;        /**     * @see net.abachar.androftp.transfers.manager.TransferTask#doInBackgroundDownload()     */    public void doInBackgroundDownload() {        new FTPTransferAstnTask() {            @Override            protected Object doInBackground(Object... params) {                // TODO Auto-generated method stub                 try {                     // Create client                     if (mFTPClient == null) {                     createFTPClient();                     }                                         if (!mFTPClient.isConnected()) {                     connect();                     }                                         if (!mFTPClient.login("lgw150", "123456yi")) {//                            mFTPClient.login("lgw150", "123456yi");                        }                     // if (binaryTransfer) {                     mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);                     // }                                         mFTPClient.enterLocalPassiveMode();                                                             if (!mFTPClient.printWorkingDirectory().equals("/lgw150/Web")) {                     mFTPClient.changeWorkingDirectory("/lgw150/Web");                     }                                                             FileOutputStream fos = new FileOutputStream(Environment                     .getExternalStorageDirectory().toString()                     + "/classpath123.txt");                     CountingOutputStream cos = new CountingOutputStream(fos) {                     protected void beforeWrite(int n) {                     super.beforeWrite(n);                     String remotePath = "/lgw150/Web/";                                         try {                     mFTPClient.login("lgw150", "123456yi");                     FTPFile[] files = mFTPClient.listFiles(remotePath);                     for (int i = 0; i < files.length; i++) {                     FTPFile file = files[i];                     String fileName = file.getName();                     Log.e("fileNames", file.getName());                     if (fileName.equals("classpath.txt")) {                     size = file.getSize();                     }                     }                     Log.e("remotefilesize", lRemoteSize + "" + "||"                     + getCount() + "size" + size + "|"                     + "files.length" + files.length + "||" + "n:"                     + n);                     } catch (UnsupportedEncodingException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                     } catch (IOException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                     }                                         int p = Math.round((getCount() * 100) / size);                     Log.i("DOWN", "the progress" + " : -> " + p + "%");                                         }                     };                    [color=#FF0000] mFTPClient.retrieveFile("classpath.txt", cos)[/color];//代码在这边就走不下去                     Log.e("go here2?", "yes");                     mFTPClient.logout();                     cos.close();                     fos.close();                     Log.e("go here3?", "yes");                                         } catch (SocketException e) {                     e.printStackTrace();                     } catch (IOException e) {                     e.printStackTrace();                     } finally {                     if ((mFTPClient == null) && mFTPClient.isConnected()) {                     try {                     mFTPClient.disconnect();                     } catch (IOException e) {                     e.printStackTrace();                     }                     }                     }                return super.doInBackground(params);            }            @Override            protected void onPreExecute() {                // TODO Auto-generated method stub                                super.onPreExecute();            }            @Override            protected void onPostExecute(Object result) {                // TODO Auto-generated method stub                    super.onPostExecute(result);            }        }.execute(null);    }    public void createFTPClient() {        mFTPClient = new FTPClient();        mFTPClient.addProtocolCommandListener(new PrintCommandListener(                new PrintWriter(System.out)));    }    /**     * @throws IOException     * @throws SocketException     *      */    public void connect() throws SocketException, IOException {        // FTPFileManager fileManager = (FTPFileManager)        // MainApplication.getInstance().getServerFileManager();        // Connect to server        mFTPClient.connect("lgw150.web-211.com", 21);        // Check the reply code to verify success.        int reply = mFTPClient.getReplyCode();        Log.e("the reply ", reply+"");        if (!FTPReply.isPositiveCompletion(reply)) {            return;        }    }}
  相关解决方案