package brips.com.yxjx.pub;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FTPHandle {
private String username;
private String password;
private String ip;
private int port;
private Properties property = null;// 配置
private String configFile;// 配置文件的路径名
private FTPClient ftpClient = null;
private String filedir = "";// FTP文件路径
private final String[] FILE_TYPES = { "文件", "目录", "符号链接", "未知类型" };
private static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm");
/**
* 设置参数
*
* @param configFile
* --参数的配置文件
*/
private void setArg(String configFile) {
property = new Properties();
BufferedInputStream inBuff = null;
try {
inBuff = new BufferedInputStream(this.getClass().getResourceAsStream(configFile));
property.load(inBuff);
username = property.getProperty("username");
password = property.getProperty("password");
ip = property.getProperty("ip");
port = Integer.parseInt(property.getProperty("port"));
filedir = property.getProperty("filedir");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inBuff != null)
inBuff.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 设置FTP客服端的配置--一般可以不设置
*
* @return
*/
private FTPClientConfig getFtpConfig() {
FTPClientConfig ftpConfig = new FTPClientConfig(
FTPClientConfig.SYST_UNIX);
ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);
return ftpConfig;
}
/**
* 连接到服务器
*/
public void connectServer() {
if (ftpClient == null) {
int reply;
try {
setArg(configFile);
ftpClient = new FTPClient();
// ftpClient.configure(getFtpConfig());
ftpClient.connect(ip);
ftpClient.login(username, password);
ftpClient.setDefaultPort(port);
System.out.print(ftpClient.getReplyString());
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
}
} catch (Exception e) {
System.err.println("登录ftp服务器【" + ip + "】失败");
e.printStackTrace();
}
}
}
/**
* 进入到服务器的某个目录下
*
* @param directory
*/
public void changeWorkingDirectory() {
try {
ftpClient.changeWorkingDirectory(filedir);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 上传文件
*
* @param inputStream--文件输入流
* @param newFileName--新的文件名
*/
public void uploadFile(InputStream inputStream, String newFileName) {
changeWorkingDirectory();// 进入文件夹
// 上传文件
BufferedInputStream buffIn = null;
try {
buffIn = new BufferedInputStream(inputStream);
ftpClient.storeFile(newFileName, buffIn);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (buffIn != null)
buffIn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 列出服务器上文件和目录
*
* @param regStr
* --匹配的正则表达式
*/
@SuppressWarnings("unchecked")
public List listRemoteFiles(String regStr) {
List list = new ArrayList();
try {
FTPFile[] files = ftpClient.listFiles(regStr);
if (files == null || files.length == 0) {
System.out.println("There has not any file!");
return null;
} else {
for (FTPFile file : files) {
if (file != null) {
Map map = new HashMap();
String filename = file.getName();
int filenamelen = filename.length();
if(filenamelen>4){
String filetype = filename.substring(filenamelen-3);
if("txt".equals(filetype)){
String name = file.getName();
name = name.substring(0,name.length()-4);
map.put("filename", name);
map.put("filesize", FileUtils.byteCountToDisplaySize(file.getSize()));
map.put("scsj", dateFormat.format(file.getTimestamp().getTime()));
list.add(map);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 下载文件
* @param remoteFileName --服务器上的文件名
* @param localFileName--本地文件名
*/
public void loadFile(String remoteFileName,String localFileName){
//下载文件
BufferedOutputStream buffOut=null;
try{
buffOut=new BufferedOutputStream(new FileOutputStream(localFileName));
ftpClient.retrieveFile(remoteFileName, buffOut);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(buffOut!=null)
buffOut.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
/**
* 删除文件
*/
public void deleteFile(String filename){
try{
ftpClient.deleteFile(filename);
}catch(IOException ioe){
ioe.printStackTrace();
}
}
/**
* 关闭连接
*/
public void closeConnect(){
try{
if(ftpClient!=null){
ftpClient.logout();
ftpClient.disconnect();
System.out.println("Ftp have closed");
}
}catch(Exception e){
e.printStackTrace();
}
}
public String getConfigFile() {
return configFile;
}
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
public String[] getFILE_TYPES() {
return FILE_TYPES;
}
public FTPClient getFtpClient() {
return ftpClient;
}
public void setFtpClient(FTPClient ftpClient) {
this.ftpClient = ftpClient;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public Properties getProperty() {
return property;
}
public void setProperty(Properties property) {
this.property = property;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFiledir() {
return filedir;
}
public void setFiledir(String filedir) {
this.filedir = filedir;
}
}
分享到:
相关推荐
java上传文件到ftp服务器,从ftp服务器下载东西demo,日管理
java实现ftp断点续传,上传文件到FTP服务器,支持断点续传,同时支持LINUX主机,代码里面包括了比较多的java网络操作类,比如连接FTP的常规类、递归创建远程服务器目录 上传文件到服务器,新上传和断点续传操作,远程...
Apache commons-net 上传文件至FTP服务器
通过java程序来实现将本地文件上传到ftp服务器的功能
JAVA中生成xml文件到指定路径和上传到ftp服务器到指定路径的方法。
java大文件上传至ftp服务器带进度条显示的,进度条的显示和ftp的上传速度同步,解决了进度条显示和ftp不同步的问题
用java语言编写的ftp小工具,可以按指定时间监控ftp服务器,把服务器指定目录内新产生的文件或者文件夹下载到本地指定文件夹,下载后删除数据。 也可以监控本地文件夹,把文件夹内新产生的文件或者文件夹整体上传到...
GetButton部分为从FTP服务器下传一个文件; PutButton部分为向FTP服务器上传一个文件。 别忘了在程序中还要引入两个库文件(import sun.net.*,import sun.net.ftp.*)。 以下是这三部分的JAVA源程序: (1)显示FTP...
这是我使用java实现的linux和ftp服务器文件上传下载工具,需要电脑安装jdk8, 启动命令,java -jar linuxAndFtp.jar 启动成功后,浏览器访问:http://localhost:9999 服务器的账号密码通过服务器列表页面管理,添加的...
JAVA获取FTP文件列表,功能齐全。包括:链接到服务器,上传文件到FTP服务器,从FTP服务器上下载文件并返回下载文件长度,返回FTP目录下的文件列表,删除FTP上的文件及目录,关闭链接。
java上传文件至ftp服务器,含jar,源码
java+jsp代码实现从FTP服务器上传下载文件。 里面给了java方法和jsp页面的代码。主要是实现从FTP服务器上面下载文件到自己本地文件夹下面。另外还给了几个网站供大家参考。
java实现文件上传到fpt,里面一些很简单的操作适合初学者。
该项目是简单介绍程序调用FTP上传和下载文件到服务器的方法。
Java实现文件上传到ftp服务器
对FTP服务器文件的基本操做:上传下载递归目录遍历等。
Java 程序实现的 FTP 跨服务器文件上传下载功能。
已经测试 完全没问题 大可以满足你的简单需求 修改液很容易 里面有详细的注释说明
主要介绍了JAVA技术实现上传下载文件到FTP服务器(完整),本文使用 Apache Jakarta Commons Net(commons-net-3.3.jar) 基于FileZilla Server服务器实现FTP服务器上文件的上传/下载/删除等操作,需要的朋友可以参考下