代码如下,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 + "%");
}
};
mFTPClient.retrieveFile("classpath.txt", cos);//代码在这边就走不下去
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;
}
}
}能下载远程的文件,但是调试的时候发现,代码总是走到mFTPClient.retrieveFile("classpath.txt", cos)这行时,接下去的几行就走不到,这是为什么?请指教。
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 + "%");
}
};
mFTPClient.retrieveFile("classpath.txt", cos);//代码在这边就走不下去
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;
}
}
}能下载远程的文件,但是调试的时候发现,代码总是走到mFTPClient.retrieveFile("classpath.txt", cos)这行时,接下去的几行就走不到,这是为什么?请指教。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货