Java ftp上传文件方法效率对比
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上传文件方法效率对比的更多相关文章
- Java ftp 上传文件和下载文件
今天同事问我一个ftp 上传文件和下载文件功能应该怎么做,当时有点懵逼,毕竟我也是第一次,然后装了个逼,在网上找了一段代码发给同事,叫他调试一下.结果悲剧了,运行不通过.(装逼失败) 我找的文章链接: ...
- .net ftp上传文件方法
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- java ftp上传文件
/** * 上传文件到ftp * @param server * @param user * @param pwd * @param filenames */ public static void u ...
- java ftp上传文件 工具类
package com.learning.spboot.utils; import com.jcraft.jsch.*; import org.apache.commons.net.ftp.FTPCl ...
- 再看ftp上传文件
前言 去年在项目中用到ftp上传文件,用FtpWebRequest和FtpWebResponse封装一个帮助类,这个在网上能找到很多,前台使用Uploadify控件,然后在服务器上搭建Ftp服务器,在 ...
- FTP 上传文件
有时候需要通过FTP同步数据文件,除了比较稳定的IDE之外,我们程序员还可以根据实际的业务需求来开发具体的工具,具体的开发过程就不细说了,这里了解一下通过C#实现FTP上传文件到指定的地址. /// ...
- Ftp上传文件
package net.util.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOut ...
- C# FTP上传文件时出现"应 PASV 命令的请求,服务器返回了一个与 FTP 连接地址不同的地址。"的错误
FTP上传文件时出现"应 PASV 命令的请求,服务器返回了一个与 FTP 连接地址不同的地址."的错误 解决方法是在原代码上增加这句话 reqFTP.UsePassive = f ...
- PHP使用FTP上传文件到服务器(实战篇)
我们在做开发的过程中,上传文件肯定是避免不了的,平常我们的程序和上传的文件都在一个服务器上,我们也可以使用第三方sdk上传文件,但是文件在第三方服务器上.现在我们使用PHP的ftp功能把文件上传到我们 ...
随机推荐
- JAVA补充-接口
1.接口详解 package com.neusoft.interfaced; /** * 接口: * 语法:interface 接口名{ * public static final 变量1=值1: * ...
- HDFS Snapshots
Overview HDFS Snapshots are read-only point-in-time copies of the file system. Snapshots can be take ...
- Hive和Sqoop测试数据
测试数据以Oracle数据库自带scott用户emp和dept表为准: 一.MySQL数据库创建的emp和dept表语法及数据: drop table if exists dept;create ta ...
- svn冲突的解决
svn文件冲突的解决 冲突后,会产生三个多余的文件. ①文件名.扩展名.mine 这是你的文件,在你更新你的工作副本之前存在于你的工作副本中--也就是说,没有冲突标志.这个文件 除了你的最新修改外没有 ...
- 实例Python处理XML文件的方法
转自:http://www.jb51.net/article/71773.htm 以下是部分代码: <?xml version="1.0" encoding="UT ...
- Rikka with Parenthesis II---hdu5831(括号匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5831 给你一个只包含‘(’‘)’的字符串,然后让我们交换两个字符一次,问是否能得到一个合法的匹配:必须 ...
- 【python-opencv】17-形态学操作-腐蚀与膨胀
形态学操作其实就是改变物体的形状,比如腐蚀就是"变瘦",膨胀就是"变胖",看下图就明白了: 形态学操作一般作用于二值化图(也可直接作用于原图),来连接相邻的元素 ...
- C#静态类,静态构造函数,静态变量
静态变量位于栈上,它是一个全局变量,在编译期就已经生成. public class Cow public static int count; private int id; { id = ++coun ...
- Linux Packages Search
网站 : https://www.pkgs.org/ https://centos.pkgs.org/
- openstack 部署笔记--keystone
控制节点 安装keystone包 # yum install openstack-keystone httpd mod_wsgi keystone配置文件 # vim /etc/keystone/ke ...