FTP 实践
来源:互联网 发布:上海企业网站排名优化 编辑:程序博客网 时间:2024/06/05 12:02
public class FtpUtil
{
private Logger logger = LoggerFactory.getLogger(getClass());
private String url;
private int port;
private String username;
private String password;
private String remotePath;
private String fileName;
private String localPath;
private String path;
private InputStream input;
/**
* Description: 从FTP服务器下载文件
*
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param remotePath
* FTP服务器上的相对路径
* @param fileName
* 要下载的文件名
* @param localPath
* 下载后保存到本地的路径
* @return
*/
public boolean downFile()
{// String url, int port, String username,String
// password, String remotePath, String fileName,
// String localPath
// 初始表示下载失败
boolean success = false;
// 创建FTPClient对象
FTPClient ftp = new FTPClient();
try
{
int reply;
// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.connect(url, port);
// 登录ftp
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return success;
} // 转到指定下载目录
ftp.changeWorkingDirectory(remotePath);
// 列出该目录下所有文件
FTPFile[] fs = ftp.listFiles();
// 遍历所有文件,找到指定的文件
for (FTPFile ff : fs)
{
String ftpFileName = StringUtils.unicodeToUtf8(ff.getName());
if (ftpFileName.equals(fileName))
{
// 根据绝对路径初始化文件
File localFile = new File(localPath + "/" + ftpFileName);
// 输出流
OutputStream is = new FileOutputStream(localFile);
// 下载文件
ftp.retrieveFile(ff.getName(), is);
// 删除原文件
ftp.deleteFile(fileName);
is.close();
// 成功下到文件
success = true;
}
}
// 退出ftp
ftp.logout();
} catch (IOException e)
{
logger.error("IOException", e);
} finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
return success;
}
/**
* Description: 向FTP服务器上传文件
*
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param path
* FTP服务器保存目录
* @param filename
* 上传到FTP服务器上的文件名
* @param input
* 输入流
* @return 成功返回true,否则返回false
*/
public boolean uploadFile()
{// String url, int port, String
// username,String
// password, String path, String filename,
// InputStream input
// 初始表示上传失败
boolean success = false;
// 创建FTPClient对象
FTPClient ftp = new FTPClient();
try
{
int reply;
// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.connect(url, port);
// 登录ftp
ftp.login(username, password);
// 看返回的值是不是230,如果是,表示登陆成功
reply = ftp.getReplyCode();
// 以2开头的返回值就会为真
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return success;
}
// 转到指定上传目录
ftp.changeWorkingDirectory(path);
// 将上传文件存储到指定目录
ftp.storeFile(StringUtils.utf8ToUnicode(fileName), input);
// 关闭输入流
input.close();
// 退出ftp
ftp.logout();
// 表示上传成功
success = true;
} catch (IOException e)
{
logger.error("FTP服务器连接异常!", e);
} finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
} catch (IOException ioe)
{
}
}
}
return success;
}
public static void main(String args[])
{
FtpUtil ftpUtil = new FtpUtil();
System.out.println("123123========");
// 下载FTP服务器上文件示例
ftpUtil.setUrl("12.72.24.14");
ftpUtil.setPort(21);
ftpUtil.setUsername("d14");
ftpUtil.setPassword("123456");
ftpUtil.setRemotePath("/err/filePath/");
ftpUtil.setFileName("df.txt");
ftpUtil.setLocalPath("f://jk//");
ftpUtil.downFile();
// 上传本地文件到FTP服务器上示例
// ftpUtil.setUrl("11.12.1.2");
// ftpUtil.setPort(21);
// ftpUtil.setUsername("sdf");
// ftpUtil.setPassword("sdf");
ftpUtil.setPath("/unibssfilePath/");
ftpUtil.setFileName("shucl_RSP.txt");
String str = "2312|23|01|1231|11|23|3|01|2|123";
String[] strArr = str.split("\\|");
String path = "f://jk//sdf_RSP.txt";
File file = new File(path);
try
{
BufferedWriter ow = new BufferedWriter(new FileWriter(file));
for (String s : strArr)
{
System.out.println(s);
ow.write(s);
ow.write("\r\n");
System.out.println("sdf========11");
}
ow.close();
} catch (Exception e)
{
e.printStackTrace();
}
try
{
ftpUtil.setInput(new FileInputStream(path));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
ftpUtil.uploadFile();
}
/**
* 获得文件行记录
*
*/
public List<String> getFileLineRecords(File file)
{
List<String> lines = new ArrayList<String>();
FileReader fileReader = null;
try
{
fileReader = new FileReader(file);
} catch (FileNotFoundException e)
{
e.printStackTrace();
return null;
}
BufferedReader bufReader = new BufferedReader(fileReader);
String line = null;
try
{
// 行记录加载
while ((line = bufReader.readLine()) != null)
{
lines.add(line);
}
} catch (IOException e)
{
e.printStackTrace();
return null;
} finally
{
try
{
bufReader.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
// 判断该文件是否有行记录
if (lines.size() > 0)
{
return lines;
} else
{
return null;
}
}
public String getUrl()
{
return url;
}
public void setUrl(String url)
{
this.url = url;
}
public int getPort()
{
return port;
}
public void setPort(int port)
{
this.port = port;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getRemotePath()
{
return remotePath;
}
public void setRemotePath(String remotePath)
{
this.remotePath = remotePath;
}
public String getFileName()
{
return fileName;
}
public void setFileName(String fileName)
{
this.fileName = fileName;
}
public String getLocalPath()
{
return localPath;
}
public void setLocalPath(String localPath)
{
this.localPath = localPath;
}
public String getPath()
{
return path;
}
public void setPath(String path)
{
this.path = path;
}
public InputStream getInput()
{
return input;
}
public void setInput(InputStream input)
{
this.input = input;
}
}
{
private Logger logger = LoggerFactory.getLogger(getClass());
private String url;
private int port;
private String username;
private String password;
private String remotePath;
private String fileName;
private String localPath;
private String path;
private InputStream input;
/**
* Description: 从FTP服务器下载文件
*
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param remotePath
* FTP服务器上的相对路径
* @param fileName
* 要下载的文件名
* @param localPath
* 下载后保存到本地的路径
* @return
*/
public boolean downFile()
{// String url, int port, String username,String
// password, String remotePath, String fileName,
// String localPath
// 初始表示下载失败
boolean success = false;
// 创建FTPClient对象
FTPClient ftp = new FTPClient();
try
{
int reply;
// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.connect(url, port);
// 登录ftp
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return success;
} // 转到指定下载目录
ftp.changeWorkingDirectory(remotePath);
// 列出该目录下所有文件
FTPFile[] fs = ftp.listFiles();
// 遍历所有文件,找到指定的文件
for (FTPFile ff : fs)
{
String ftpFileName = StringUtils.unicodeToUtf8(ff.getName());
if (ftpFileName.equals(fileName))
{
// 根据绝对路径初始化文件
File localFile = new File(localPath + "/" + ftpFileName);
// 输出流
OutputStream is = new FileOutputStream(localFile);
// 下载文件
ftp.retrieveFile(ff.getName(), is);
// 删除原文件
ftp.deleteFile(fileName);
is.close();
// 成功下到文件
success = true;
}
}
// 退出ftp
ftp.logout();
} catch (IOException e)
{
logger.error("IOException", e);
} finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
return success;
}
/**
* Description: 向FTP服务器上传文件
*
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param path
* FTP服务器保存目录
* @param filename
* 上传到FTP服务器上的文件名
* @param input
* 输入流
* @return 成功返回true,否则返回false
*/
public boolean uploadFile()
{// String url, int port, String
// username,String
// password, String path, String filename,
// InputStream input
// 初始表示上传失败
boolean success = false;
// 创建FTPClient对象
FTPClient ftp = new FTPClient();
try
{
int reply;
// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.connect(url, port);
// 登录ftp
ftp.login(username, password);
// 看返回的值是不是230,如果是,表示登陆成功
reply = ftp.getReplyCode();
// 以2开头的返回值就会为真
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return success;
}
// 转到指定上传目录
ftp.changeWorkingDirectory(path);
// 将上传文件存储到指定目录
ftp.storeFile(StringUtils.utf8ToUnicode(fileName), input);
// 关闭输入流
input.close();
// 退出ftp
ftp.logout();
// 表示上传成功
success = true;
} catch (IOException e)
{
logger.error("FTP服务器连接异常!", e);
} finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
} catch (IOException ioe)
{
}
}
}
return success;
}
/**
*/public static void main(String args[])
{
FtpUtil ftpUtil = new FtpUtil();
System.out.println("123123========");
// 下载FTP服务器上文件示例
ftpUtil.setUrl("12.72.24.14");
ftpUtil.setPort(21);
ftpUtil.setUsername("d14");
ftpUtil.setPassword("123456");
ftpUtil.setRemotePath("/err/filePath/");
ftpUtil.setFileName("df.txt");
ftpUtil.setLocalPath("f://jk//");
ftpUtil.downFile();
// 上传本地文件到FTP服务器上示例
// ftpUtil.setUrl("11.12.1.2");
// ftpUtil.setPort(21);
// ftpUtil.setUsername("sdf");
// ftpUtil.setPassword("sdf");
ftpUtil.setPath("/unibssfilePath/");
ftpUtil.setFileName("shucl_RSP.txt");
String str = "2312|23|01|1231|11|23|3|01|2|123";
String[] strArr = str.split("\\|");
String path = "f://jk//sdf_RSP.txt";
File file = new File(path);
try
{
BufferedWriter ow = new BufferedWriter(new FileWriter(file));
for (String s : strArr)
{
System.out.println(s);
ow.write(s);
ow.write("\r\n");
System.out.println("sdf========11");
}
ow.close();
} catch (Exception e)
{
e.printStackTrace();
}
try
{
ftpUtil.setInput(new FileInputStream(path));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
ftpUtil.uploadFile();
}
/**
* 获得文件行记录
*
*/
public List<String> getFileLineRecords(File file)
{
List<String> lines = new ArrayList<String>();
FileReader fileReader = null;
try
{
fileReader = new FileReader(file);
} catch (FileNotFoundException e)
{
e.printStackTrace();
return null;
}
BufferedReader bufReader = new BufferedReader(fileReader);
String line = null;
try
{
// 行记录加载
while ((line = bufReader.readLine()) != null)
{
lines.add(line);
}
} catch (IOException e)
{
e.printStackTrace();
return null;
} finally
{
try
{
bufReader.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
// 判断该文件是否有行记录
if (lines.size() > 0)
{
return lines;
} else
{
return null;
}
}
public String getUrl()
{
return url;
}
public void setUrl(String url)
{
this.url = url;
}
public int getPort()
{
return port;
}
public void setPort(int port)
{
this.port = port;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getRemotePath()
{
return remotePath;
}
public void setRemotePath(String remotePath)
{
this.remotePath = remotePath;
}
public String getFileName()
{
return fileName;
}
public void setFileName(String fileName)
{
this.fileName = fileName;
}
public String getLocalPath()
{
return localPath;
}
public void setLocalPath(String localPath)
{
this.localPath = localPath;
}
public String getPath()
{
return path;
}
public void setPath(String path)
{
this.path = path;
}
public InputStream getInput()
{
return input;
}
public void setInput(InputStream input)
{
this.input = input;
}
}
0 0
- FTP 实践
- Perl FTP实践
- 实践:win7 搭建ftp服务器
- centos下ftp服务器搭建实践
- CentOS7搭建FTP服务器实践记录
- 鸟哥私房菜实践(12)- FTP
- vsftpd安装以及配置FTP虚拟用户实践
- vsftpd安装以及配置FTP虚拟用户实践
- FTP
- ftp
- ftp
- ftp
- ftp
- ftp
- FTP
- FTP
- ftp
- ftp
- 【Hello,world 】如果你说“我要先学习一下再开始做”那么你永远不会真正开始。
- Valid Anagram--JavaScript
- hdu Kth number
- Configure Oracle GoldenGate for Oracle to Oracle Database Synchronization
- Qt之QHeaderView自定义排序(QSortFilterProxyModel)
- FTP 实践
- shell学习
- 奕新集团--单实例到RAC节点 GG同步
- 异常
- Java中的对象克隆
- CF Round 8VC (B. Island Puzzle)判断两个环相等
- ARC下OC对象和CF对象之间的桥接(bridge)
- GoldenGate常见异常处理
- hadoop基础----hadoop理论(三)-----hadoop分布式文件系统HDFS详解