关于apache FTP下载速度慢的解决方案

来源:互联网 发布:特价专场首页淘宝优站 编辑:程序博客网 时间:2024/05/17 22:31

最近公司有一个项目,需要使用FTP进行文件的上传下载,以前写的FTP下载速度始终在2Mb/s,经过改进速度达到6Mb/s。

测试了很长时间才找到原因:

/** * 下载服务器上的文件或者文件夹 * @param ftpFileName 服务器上的文件夹名或者文件名 * @param localDir 下载到设备上的路径 * @param return 下载的文件数目 */private int downFileOrDir(String ftpFileName, String localDir, int downloadFileCount) {Message downloadFileMsg;downloadFileMsg = mHandler.obtainMessage();int downloadCount = 0;try {File file = new File(ftpFileName);File temp = new File(localDir);if (!temp.exists()) {temp.mkdirs();}// 判断是否是目录if (isDir(ftpFileName)) {String[] names = ftpClient.listNames();for (int i = 0; i < names.length; i++) {if (isDir(names[i])) {downFileOrDir(ftpFileName + '/' + names[i], localDir+ File.separator + names[i], downloadFileCount);ftpClient.changeToParentDirectory();} else {File localfile = new File(localDir + File.separator+ names[i]);if (!localfile.exists()) {fos = new FileOutputStream(localfile);ftpClient.retrieveFile(names[i], fos);} else {// 开始删除文件file.delete();fos = new FileOutputStream(localfile);ftpClient.retrieveFile(ftpFileName, fos);}}}} else {downloadCount = downloadFileCount + 1;File localfile = new File(localDir + File.separator+ file.getName());if (!localfile.exists()) {fos = new FileOutputStream(localfile);ftpClient.retrieveFile(ftpFileName, fos);} else {// 开始删除文件file.delete();fos = new FileOutputStream(localfile);ftpClient.retrieveFile(ftpFileName, fos);}ftpClient.changeToParentDirectory();}} catch (SocketException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} return downloadCount;}// 判断是否是目录public boolean isDir(String fileName) {try {// 切换目录,若当前是目录则返回true,否则返回true。boolean falg = ftpClient.changeWorkingDirectory(fileName);return falg;} catch (Exception e) {e.printStackTrace();}return false;}
以前是用上面的方法进行的下载,下载速度始终很慢。

提高速度的方法是:

ftpClient.retrieveFile(ftpFileName, fos);

改成

<span style="white-space:pre"></span>// 进度long step = serverSize / 100;long process = 0;long currentSize = 0;// 开始准备下载文件OutputStream out = new FileOutputStream(localFile, true);ftpClient.setRestartOffset(localSize);InputStream input = ftpClient.retrieveFileStream(names[j]);byte[] b = new byte[1024];int length = 0;while ((length = input.read(b)) != -1) {out.write(b, 0, length);currentSize = currentSize + length;if (currentSize / step != process) {process = currentSize / step;if (process % 1 == 0) { // 每隔%5的进度返回一次//进度}}}out.flush();out.close();input.close();// 此方法是来确保流处理完毕,如果没有此方法,可能会造成现程序死掉if (ftpClient.completePendingCommand()) {//成功} else {//失败}
修改后的下载速度大幅度提高

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 游戏突然掉帧怎么办 oppo应用商店打不开怎么办 ps4战地1校园网怎么办 战地4延迟高怎么办 红米手机打不开怎么办 台式电脑网页打不开怎么办 电脑网页很多都打不开怎么办 qq发送不了位置怎么办 qq无法找到入口怎么办 玩游戏总是掉怎么办 ipad所有按键失灵怎么办 手机启动器停止运行怎么办 安卓版泰拉瑞亚联机失去连接怎么办 吃鸡安装不了怎么办 obb文件丢失了怎么办 dnf登录闪退怎么办 泰拉瑞亚地图找不到备份怎么办 服务器没远程管理卡怎么办 tplink上不了网怎么办 键盘端口坏了怎么办 小米路由dns错误怎么办 电脑内存性能低怎么办 nas硬盘坏了怎么办 360安装环境异常怎么办 连接不上服务器怎么办 路由器被绑定mac怎么办 mac地址绑定失败怎么办 触摸屏忘了密码怎么办? 小区高层水压低怎么办 高层楼房水压不够怎么办 热水器温度太高怎么办 热水器出热水小怎么办 天然气热水器水压不够怎么办 热水器水变小了怎么办 液压齿轮泵没力怎么办 gps总是浮点解怎么办 电源要做到稳压怎么办 变压器输出电压低怎么办 变压器输出电压高怎么办 家里空调带不动怎么办 小天才定位不准怎么办