Java ftp上传文件方法效率对比

一、功能简介:

txt文件采用ftp方式从windows传输到Linux系统;

二、ftp实现方法

(1)方法一:采用二进制流传输,设置缓冲区,速度快,50M的txt文件需要15秒;

//FTP传输到数据库服务器
public boolean uploadServerByFtp(String fileNmae){
  boolean flag = true;
//客户端数据文件路径
String client_path = "D://answer/data/";
//服务器上的存放数据文件路径
String server_path = "/home/download/file_tmp/";
String hostname = "192.25.125.112";
String ftpusername = "root";
String ftppwd = “123456”;
int port = 21;//查找路径下的指定txt文件,然后采用FTP上传
File file_name = new File(client_path+fileNmae);
if(!file_name.exists()){
  return false;
}
//创建ftp客户端
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("utf-8");
//主动模式
ftpClient.enterLocalActiveMode();
String getfileName = file_name.getName();
String getfileNamePath = file_name.getPath();
if((getfileName.substring(getfileName.lastIndexOf(".")).trim().equals(".txt"))){
  try {
  //链接ftp服务器
ftpClient.connect(hostname, port);
//登录ftp
ftpClient.login(ftpusername, ftppwd);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
   ftpClient.disconnect();
logger.info("Returns a 530 password username error or the current user does not have permission to close the FTP connection");
return false;
}
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
String server_file_name = server_path+ getfileName;
InputStream input = new FileInputStream(getfileNamePath);
OutputStream out = ftpClient.storeFileStream(server_file_name);
byte[] byteArray = new byte[4096];
int read = 0;
while ((read = input.read(byteArray)) != -1) {
  out.write(byteArray, 0, read);
}
out.close();
ftpClient.logout();
} catch (SocketException e) {
flag = false;
e.printStackTrace();
} catch (IOException e) {
flag = false;
e.printStackTrace();
} catch (Exception e) {
flag = false;
e.printStackTrace();
}finally {
  if (ftpClient.isConnected()) {
  try {
  ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
return flag;
}

(2)方法二:storeFile()方法,没有设置缓冲区,速度慢,50M的txt文件需要100秒;

//FTP传输到数据库服务器
public boolean uploadServerByFtp(String fileNmae){
  boolean flag = true;
//客户端数据文件路径
String client_path = "D://answer/data/";
//服务器上的存放数据文件路径
String server_path = "/home/download/file_tmp/";
String hostname = "192.25.125.112";
String ftpusername = "root";
String ftppwd = “123456”;
int port = 21;
//查找路径下的指定txt文件,然后采用FTP上传
File file_name = new File(client_path+fileNmae);
if(!file_name.exists()){
  return false;
}
//创建ftp客户端
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("utf-8");
String getfileName = file_name.getName();
String getfileNamePath = file_name.getPath();
if((getfileName.substring(getfileName.lastIndexOf(".")).trim().equals(".txt"))){
  try {
  //链接ftp服务器
ftpClient.connect(hostname, port);
//登录ftp
ftpClient.login(ftpusername, ftppwd);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
   ftpClient.disconnect();
logger.info("Returns a 530 password username error or the current user does not have permission to close the FTP connection");
return false;
}
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
String server_file_name = server_path+ getfileName;
//读取源文件流(客户端文件)
InputStream client_fileInput = new FileInputStream(getfileNamePath);
//传送到服务端
ftpClient.storeFile(server_file_name, client_fileInput);
client_fileInput.close();
ftpClient.logout();
} catch (SocketException e) {
flag = false;
e.printStackTrace();
} catch (IOException e) {
flag = false;
e.printStackTrace();
} catch (Exception e) {
flag = false;
e.printStackTrace();
}finally {
  if (ftpClient.isConnected()) {
  try {
  ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
return flag;
}

Java ftp上传文件方法效率对比的更多相关文章

  1. Java ftp 上传文件和下载文件

    今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...

  2. .net ftp上传文件方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  3. java ftp上传文件

    /** * 上传文件到ftp * @param server * @param user * @param pwd * @param filenames */ public static void u ...

  4. java ftp上传文件 工具类

    package com.learning.spboot.utils; import com.jcraft.jsch.*; import org.apache.commons.net.ftp.FTPCl ...

  5. 再看ftp上传文件

    前言 去年在项目中用到ftp上传文件,用FtpWebRequest和FtpWebResponse封装一个帮助类,这个在网上能找到很多,前台使用Uploadify控件,然后在服务器上搭建Ftp服务器,在 ...

  6. FTP 上传文件

    有时候需要通过FTP同步数据文件,除了比较稳定的IDE之外,我们程序员还可以根据实际的业务需求来开发具体的工具,具体的开发过程就不细说了,这里了解一下通过C#实现FTP上传文件到指定的地址. /// ...

  7. Ftp上传文件

    package net.util.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...

  8. C# FTP上传文件时出现"应 PASV 命令的请求,服务器返回了一个与 FTP 连接地址不同的地址。"的错误

    FTP上传文件时出现"应 PASV 命令的请求,服务器返回了一个与 FTP 连接地址不同的地址."的错误 解决方法是在原代码上增加这句话 reqFTP.UsePassive = f ...

  9. PHP使用FTP上传文件到服务器(实战篇)

    我们在做开发的过程中,上传文件肯定是避免不了的,平常我们的程序和上传的文件都在一个服务器上,我们也可以使用第三方sdk上传文件,但是文件在第三方服务器上.现在我们使用PHP的ftp功能把文件上传到我们 ...

随机推荐

  1. Java String, StringBuffer和StringBuilder实例

    1- 分层继承2- 可变和不可变的概念3- String3.1- 字符串是一个非常特殊的类3.2- String 字面值 vs. String对象3.3- String的方法3.3.1- length ...

  2. 超链接 a的小手

    cursor:hand   仅仅ie only,FIREFOX底下就不可以正常渲染 cursor:pointer; <span style="cursor:pointer;" ...

  3. PHP配置xcache缓存扩展

    安装步骤 wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz tar -xf xcache-3.2.0.tar ...

  4. ibatis sqlmap配置问题 “Check the IBatisNet.DataAccess.DaoSessionHandlers.SqlMapDaoSessionHandler.”

    - The error occurred while configure DaoSessionHandler.- The error occurred in <property name=&qu ...

  5. 不再以讹传讹,GET和POST的真正区别(转)

    add by zhj:按照restful的定义,GET是用于获取记录(幂等),POST用于创建记录(不幂等).GET也能带消息体?这个我没试过,文中说用浏览器发GET请求 是没法带的.另外,在< ...

  6. 003-redis-命令-key操作,字符串操作

    Redis 键(key) Redis 键命令用于管理 redis 的键. 序号 命令及描述 1 DEL key该命令用于在 key 存在时删除 key. 2 DUMP key 序列化给定 key ,并 ...

  7. python内置函数大全(分类)

    python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...

  8. 让你分分钟了解Web接口测试

    因为前后端架构分离技术的兴起,接口测试也越来越重要,最近一直想总结下,作为一个近三年的测试人员,接口这个词是耳濡目染的,而开发张口闭口也都是这个接口或那个接口怎么怎么样,自己遇到的bug也很多是接口问 ...

  9. CMFCPropertyGridProperty的使用

    设定初始值 CString str(_T("Button")); COleVariant cOlevariant(str); pTypeProperty->SetOrigin ...

  10. oracle修改内存使用和性能调节,SGA

    最近装了oracle,电脑实在太卡了,想要限制内存使用,结果碰到一系列问题: 要用SYS帐户登录,修改SGA使用,结果不知道SYS密码.用SYSTEM帐户权限不够. 试了几条语句后,有几个文件修改不了 ...