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功能把文件上传到我们 ...
随机推荐
- Centralized Cache Management in HDFS
Overview(概述) Centralized cache management in HDFS is an explicit caching mechanism that allows users ...
- NYOJ 587 blockhouses 【DFS】
blockhouses 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 Suppose that we have a square city with straigh ...
- 定时任务,AlarmManager使用
CoderLt 定时任务,AlarmManager使用 项目需要:实现一个定时提醒的功能 查阅资料知道,需要使用AlarmManager AlarmManager介绍: AlarmManager是 ...
- Oracle Golden Gate原理简介
Oracle Golden Gate原理简介 http://www.askoracle.org/oracle/HighAvailability/20140109953.html#6545406-tsi ...
- CentOS工作内容(六)双网卡带宽绑定bind teaming
CentOS工作内容(六)双网卡带宽绑定bind teaming Teaming功能是什么功能http://zhidao.baidu.com/link?url=cpcwl9LH4FSHJBaTW-e ...
- abap关键字
1:abap将提升的关键字快捷输入 按tab键,提示的关键字将会自动输入. 2:shift tab 用于对其格式 3:ctrl+d 将改行复制到下一行.
- POJ1014:Dividing(多重背包)
http://poj.org/problem?id=1014 Description Marsha and Bill own a collection of marbles. They want to ...
- Look for the Air Jordan 32 in full family sizing
Following the release of the 'Rosso Corsa' colorway, Jordan Brand is now set to officially launch th ...
- cocos代码研究(2)Layer学习笔记
auto layer = Layer::create(); /*************华丽分割线*************/ auto layer = LayerColor::create(Colo ...
- Javascript-闰年javascript的判断
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...