android service上传,开源库android-upload-service上传一个bug

最近有个项目用到ftp上传,然后我在github上面找了一个叫android-upload-service的库,一开始连接的是家里NAS上的ftp没有问题,后来部署了服务器,无论是pureftp还是vsftp死活连接不上,有点坑,于是把源码拉下来看了一下,它是封装了Apache的Commons Net库,debug进去发现FTPUploadTask里面的ftpClient在调用connect之后就报超时异常,看样子是配置问题,经过逐一排查,发现是ftpClient.setAutodetectUTF8(true)导致,直接注销掉就行了,FTPUploadTask的upload函数如下

@Override

protected void upload() throws Exception {

try {

if (ftpParams.useSSL) {

String secureProtocol = ftpParams.secureSocketProtocol;

if (secureProtocol == null || secureProtocol.isEmpty())

secureProtocol = FTPUploadTaskParameters.DEFAULT_SECURE_SOCKET_PROTOCOL;

ftpClient = new FTPSClient(secureProtocol, ftpParams.implicitSecurity);

Logger.debug(LOG_TAG, "Created FTP over SSL (FTPS) client with "

+ secureProtocol + " protocol and "

+ (ftpParams.implicitSecurity ? "implicit security" : "explicit security"));

} else {

ftpClient = new FTPClient();

}

ftpClient.setBufferSize(UploadService.BUFFER_SIZE);

ftpClient.setCopyStreamListener(this);

ftpClient.setDefaultTimeout(ftpParams.connectTimeout);

ftpClient.setConnectTimeout(ftpParams.connectTimeout);

ftpClient.setAutodetectUTF8(true);//注销掉这一行

Logger.debug(LOG_TAG, "Connect timeout set to " + ftpParams.connectTimeout + "ms");

Logger.debug(LOG_TAG, "Connecting to " + params.serverUrl

+ ":" + ftpParams.port + " as " + ftpParams.username);

ftpClient.connect(params.serverUrl, ftpParams.port);

if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {

throw new Exception("Can't connect to " + params.serverUrl

+ ":" + ftpParams.port

+ ". The server response is: " + ftpClient.getReplyString());

}

if (!ftpClient.login(ftpParams.username, ftpParams.password)) {

throw new Exception("Error while performing login on " + params.serverUrl

+ ":" + ftpParams.port

+ " with username: " + ftpParams.username

+ ". Check your credentials and try again.");

}

// to prevent the socket timeout on the control socket during file transfer,

// set the control keep alive timeout to a half of the socket timeout

int controlKeepAliveTimeout = ftpParams.socketTimeout / 2 / 1000;

ftpClient.setSoTimeout(ftpParams.socketTimeout);

ftpClient.setControlKeepAliveTimeout(controlKeepAliveTimeout);

ftpClient.setControlKeepAliveReplyTimeout(controlKeepAliveTimeout * 1000);

Logger.debug(LOG_TAG, "Socket timeout set to " + ftpParams.socketTimeout

+ "ms. Enabled control keep alive every " + controlKeepAliveTimeout + "s");

ftpClient.enterLocalPassiveMode();

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

ftpClient.setFileTransferMode(ftpParams.compressedFileTransfer ?

FTP.COMPRESSED_TRANSFER_MODE : FTP.STREAM_TRANSFER_MODE);

// this is needed to calculate the total bytes and the uploaded bytes, because if the

// request fails, the upload method will be called again

// (until max retries is reached) to retry the upload, so it's necessary to

// know at which status we left, to be able to properly notify firther progress.

calculateUploadedAndTotalBytes();

String baseWorkingDir = ftpClient.printWorkingDirectory();

Logger.debug(LOG_TAG, "FTP default working directory is: " + baseWorkingDir);

Iterator iterator = new ArrayList<>(params.files).iterator();

while (iterator.hasNext()) {

UploadFile file = iterator.next();

if (!shouldContinue)

break;

uploadFile(baseWorkingDir, file);

addSuccessfullyUploadedFile(file);

iterator.remove();

}

// Broadcast completion only if the user has not cancelled the operation.

if (shouldContinue) {

broadcastCompleted(new ServerResponse(UploadTask.TASK_COMPLETED_SUCCESSFULLY,

UploadTask.EMPTY_RESPONSE, null));

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (ftpClient.isConnected()) {

try {

Logger.debug(LOG_TAG, "Logout and disconnect from FTP server: "

+ params.serverUrl + ":" + ftpParams.port);

ftpClient.logout();

ftpClient.disconnect();

} catch (Exception exc) {

Logger.error(LOG_TAG, "Error while closing FTP connection to: "

+ params.serverUrl + ":" + ftpParams.port, exc);

}

}

ftpClient = null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值