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功能把文件上传到我们 ...
随机推荐
- 伸展树(Splay Tree)进阶 - 从原理到实现
目录 1 简介 2 基础操作 2.1 旋转 2.2 伸展操作 3 常规操作 3.1 插入操作 3.2 删除操作 3.3 查找操作 3.4 查找某数的排名.查找某排名的数 3.4.1 查找某数的排名 3 ...
- 洛谷P1084 疫情控制 [noip2012] 贪心+树论+二分答案 (还有个小bugQAQ
正解:贪心+倍增+二分答案 解题报告: 正好想做noip的题目然后又想落实学长之前讲的题?于是就找上了这题 其实之前做过,70,然后实在细节太多太复杂就不了了之,现在再看一遍感觉又一脸懵了... 从标 ...
- js屏蔽f12键
<script> $(document).keydown(function(e) { if (e.keyCode == 123) {/ ...
- python center() 函数
center Python center() 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串.默认填充字符为空格. 语法 center()方法语法: str.center(widt ...
- 启动InnoDB引擎的方法
启动InnoDB引擎的方法 http://down.chinaz.com/server/201207/2090_1.htm 启动InnoDB引擎的方法 Mysql中默认的是MyISAM数据引擎,可惜此 ...
- 自定义Metrics:让Prometheus监控你的应用程序
前言 Prometheus社区提供了大量的官方以及第三方Exporters,可以满足Prometheus的采纳者快速实现对关键业务,以及基础设施的监控需求. 如上所示,一个简单的应用以及环境架构.一般 ...
- [js]js中原型的继承
js继承01 思路: 单例/工厂/构造函数--演进到原型 搞清原型结构 原型继承 模拟系统原型继承 实现自己的继承 观察原型继承特点 演进到原型链这一步 //单例模式: 防止变量名冲突: // 思路: ...
- 从Maven仓库中导出jar包
从Maven仓库中导出jar包:进入工程pom.xml 所在的目录下,输入以下命令:mvn dependency:copy-dependencies -DoutputDirectory=lib更简单的 ...
- [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming
Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...
- pandas使用drop_duplicates去除DataFrame重复项
DataFrame中存在重复的行或者几行中某几列的值重复,这时候需要去掉重复行,示例如下: data.drop_duplicates(subset=['A','B'],keep='first',inp ...