Android FTP 下载 pause/resume
Android FTP download pause/resume
我正在开发一个 Android 项目,需要 FTP 下载 paused/resumed。
这是我用于 FTP 连接的代码:
ftpClient.setConnectTimeout(25000);
ftpClient.login("login", "password");
ftpClient.changeWorkingDirectory("/audio");
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
那我开始下载:
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] bytesArray = new byte[4096];
int bytesRead = -1;
totalRead = 0;
if (localFileSize > 0) {
ftpClient.setRestartOffset(localFileSize);
ftpClient.restart(localFileSize);
}
InputStream inputStream = ftpClient.retrieveFileStream(fileName);
while ((bytesRead = inputStream.read(bytesArray)) != -1) {
totalRead += bytesRead;
outputStream.write(bytesArray, (int) localFileSize, bytesRead);
}
success = ftpClient.completePendingCommand();
我尝试像这样使用 abort
暂停:
if (ftpClient.abort()) {
//connection aborted!;
}
但似乎 abort
在有活动下载时不起作用,如下所述:https://issues.apache.org/jira/browse/NET-419
有什么方法可以在 Android 中执行 pause/resume 下载 FTP 吗?
如果您真的只想暂停下载(与 abort/reconnect/resume 相反),那么您需要做的就是暂时暂停写入数据连接流的 while
循环。
见How to pause/resume thread in Android?
我使用相同的 AsyncTask
进行连接、下载和中止操作:
protected Void doInBackground(Integer... params) {
int command = params.length > 0 ? params[0] : 0;
switch (command) {
case CONNECT:
connect();
break;
case DOWNLOAD:
download();
break;
case PAUSE:
abortConnection();
break;
}
return null;
}
每次使用单例设计模式
确保只有一个任务运行
public static FtpConnectTask getInstance(FTPClientWrapper ftpClient) {
if (instance != null) {
if (instance.getStatus().name().toLowerCase().equals("running"){
instance.cancel(true);
}
if (instance.getStatus().name().toLowerCase().equals("finished")) {
// task finished!
}
}
instance = new FtpConnect(uiListener, ftpClientW);
return instance;
}
这是download()
方法的代码:
we look for local file size each time the download starts, then we use this size as starting point
//create local file
File localFile = new File(outDir, FILE_NAME));
// get file size if the file exists
long localFileSize = localFile.length();
//retrieve file from server
try {
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile, true));
byte[] bytesArray = new byte[4096];
int bytesRead;
int totalRead = (int) localFileSize;
ftpClient.restart(localFileSize);
InputStream inputStream = ftpClient.retrieveFileStream(REMOTE_FILE);
while ((bytesRead = inputStream.read(bytesArray)) != -1) {
totalRead += bytesRead;
outputStream.write(bytesArray, 0, bytesRead);
// break the loop when AsyncTask is cancelled
if(isCancelled()){
break;
}
}
if (ftpClient.completePendingCommand()) {
// Task completed!
}
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
这是abortConnection()
的代码
if (ftpClient.isConnected()) {
try {
if (ftpClient.abort()) {
// Connection aborted!
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
// Not connected!
}
To resume your download just call download()
again.
我正在开发一个 Android 项目,需要 FTP 下载 paused/resumed。
这是我用于 FTP 连接的代码:
ftpClient.setConnectTimeout(25000);
ftpClient.login("login", "password");
ftpClient.changeWorkingDirectory("/audio");
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
那我开始下载:
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] bytesArray = new byte[4096];
int bytesRead = -1;
totalRead = 0;
if (localFileSize > 0) {
ftpClient.setRestartOffset(localFileSize);
ftpClient.restart(localFileSize);
}
InputStream inputStream = ftpClient.retrieveFileStream(fileName);
while ((bytesRead = inputStream.read(bytesArray)) != -1) {
totalRead += bytesRead;
outputStream.write(bytesArray, (int) localFileSize, bytesRead);
}
success = ftpClient.completePendingCommand();
我尝试像这样使用 abort
暂停:
if (ftpClient.abort()) {
//connection aborted!;
}
但似乎 abort
在有活动下载时不起作用,如下所述:https://issues.apache.org/jira/browse/NET-419
有什么方法可以在 Android 中执行 pause/resume 下载 FTP 吗?
如果您真的只想暂停下载(与 abort/reconnect/resume 相反),那么您需要做的就是暂时暂停写入数据连接流的 while
循环。
见How to pause/resume thread in Android?
我使用相同的 AsyncTask
进行连接、下载和中止操作:
protected Void doInBackground(Integer... params) {
int command = params.length > 0 ? params[0] : 0;
switch (command) {
case CONNECT:
connect();
break;
case DOWNLOAD:
download();
break;
case PAUSE:
abortConnection();
break;
}
return null;
}
每次使用单例设计模式
确保只有一个任务运行public static FtpConnectTask getInstance(FTPClientWrapper ftpClient) {
if (instance != null) {
if (instance.getStatus().name().toLowerCase().equals("running"){
instance.cancel(true);
}
if (instance.getStatus().name().toLowerCase().equals("finished")) {
// task finished!
}
}
instance = new FtpConnect(uiListener, ftpClientW);
return instance;
}
这是download()
方法的代码:
we look for local file size each time the download starts, then we use this size as starting point
//create local file
File localFile = new File(outDir, FILE_NAME));
// get file size if the file exists
long localFileSize = localFile.length();
//retrieve file from server
try {
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile, true));
byte[] bytesArray = new byte[4096];
int bytesRead;
int totalRead = (int) localFileSize;
ftpClient.restart(localFileSize);
InputStream inputStream = ftpClient.retrieveFileStream(REMOTE_FILE);
while ((bytesRead = inputStream.read(bytesArray)) != -1) {
totalRead += bytesRead;
outputStream.write(bytesArray, 0, bytesRead);
// break the loop when AsyncTask is cancelled
if(isCancelled()){
break;
}
}
if (ftpClient.completePendingCommand()) {
// Task completed!
}
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
这是abortConnection()
if (ftpClient.isConnected()) {
try {
if (ftpClient.abort()) {
// Connection aborted!
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
// Not connected!
}
To resume your download just call
download()
again.