关于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
- 关于apache FTP下载速度慢的解决方案
- 关于Apache james下载大附件速度慢问题
- 关于Mac上使用百度云下载速度慢的解决方案
- eclipse下载插件速度很慢解决方案
- myeclipse 运行速度慢的解决方案
- adsl 上传速度慢 的解决方案
- mysql远程连接速度慢的解决方案
- mysql 远程连接速度慢的解决方案
- 使用npm 安装速度慢的解决方案
- 数据库操作时,速度慢的解决方案
- mysql 远程连接速度慢的解决方案
- mysql 远程连接速度慢的解决方案
- pod install速度慢的终极解决方案
- Dedecms生成速度慢的解决方案
- github clone速度慢的解决方案
- pod install速度慢的终极解决方案
- Android sdk下载速度慢的问题
- macbook pro下载速度慢的解决方法
- 手机卫士学习06-自定义滚动的TextView实现走马灯效果
- ASCII编码与Unicode编码
- onActivityResult传值
- 新股集合竞价
- 制作dll和测试dll
- 关于apache FTP下载速度慢的解决方案
- 什么是编程语言
- 理财如何越理越有钱 朝气蓬勃的2015该如何实现理财计划?
- 抽奖算法总结
- 阿里云-Centos6.5-安装 JDK、Mysql、Tomcat、Nignx
- HttpServlets请求
- 解决embed总是float在最上面
- iOS开发之Xcode打包framework
- 输入法的选择