设为首页 收藏本站
查看: 2282|回复: 0

[经验分享] 常见的FTP工具类

[复制链接]

尚未签到

发表于 2016-6-8 09:32:14 | 显示全部楼层 |阅读模式
/** Created on Nov 26, 2009** TODO To change the template for this generated file go to* Window - Preferences - Java - Code Style - Code Templates*/package com.suning.commerce.order.commands;/**-----------------------------------------------------------------* IBM Confidential** OCO Source Materials** WebSphere Commerce** (C) Copyright IBM Corp. 2009** The source code for this program is not published or otherwise* divested of its trade secrets, irrespective of what has* been deposited with the U.S. Copyright Office.*-----------------------------------------------------------------*/import java.io.DataInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.RandomAccessFile;import org.apache.commons.lang.StringUtils;import sun.net.TelnetInputStream;import sun.net.TelnetOutputStream;import sun.net.ftp.FtpClient;/*** @author Athena* * TODO To change the template for this generated type comment go to Window -* Preferences - Java - Code Style - Code Templates*/public class SNFtpUtil extends FtpClient {private String server;private String user;private String password;private int port;private String path;/*** 连接FTP服务器*/public void connectServer() throws Exception {openServer(server, port);login(user, password);System.out.println("Login success!");binary();}/*** 关闭FTP服务器连接*/public void closeConnect() {try {closeServer();} catch (IOException e) {e.printStackTrace();}}public void upload(InputStream in, String remotefilename)throws IOException {TelnetOutputStream os = null;try {os = put(remotefilename);byte[] bytes = new byte[1024];int c;while ((c = in.read(bytes)) != -1) {os.write(bytes, 0, c);}} finally {in.close();os.close();}}public SNFtpUtil popFtpConfigSERVER(String ip, String port, String user,String password, String path) throws IOException{SNFtpUtil ftp = new SNFtpUtil();ftp.setServer(ip);ftp.setPort((new Integer(port)).intValue());ftp.setUser(user);ftp.setPassword(password);ftp.setPath(path);try {ftp.connectServer();} catch (Exception e) {e.printStackTrace();}System.out.println("before path is" + path);ftp.cd(path);System.out.println("after path is" + path);return ftp;}/*** 上传文件*/public void upload(String localFilePath, String remoteFilePath) {try {TelnetOutputStream os = put(remoteFilePath);File file_in = new File(localFilePath);FileInputStream is = new FileInputStream(file_in);byte[] bytes = new byte[1024];int ch;while ((ch = is.read(bytes)) != -1) {os.write(bytes, 0, ch);}is.close();os.close();System.out.println("Upload file succesfully!");} catch (IOException e) {e.printStackTrace();}}/*** 下载文件*/public void download(String localFilePath, String remoteFilePath) {try {int ch;File fi = new File(localFilePath);RandomAccessFile raf = new RandomAccessFile(fi, "rw");raf.seek(0);TelnetInputStream fget = get(remoteFilePath);DataInputStream puts = new DataInputStream(fget);while ((ch = puts.read()) >= 0) {raf.write(ch);}fget.close();raf.close();System.out.println("Dowload one log successfully");} catch (IOException e) {e.printStackTrace();}}/*** 远程serverFTP配置* * @return SNFtpUtil ftp帮助类* @throws IOException*/public SNFtpUtil popFtpConfigSERVER_SIT(String ip, String port,String user, String password, String path) throws IOException{SNFtpUtil ftp = new SNFtpUtil();ftp.setServer(ip);if (StringUtils.isNotEmpty(port))ftp.setPort((new Integer(port)).intValue());ftp.setUser(user);ftp.setPassword(password);ftp.setPath("/");try {ftp.connectServer();} catch (Exception e) {e.printStackTrace();}System.out.println("before path is" + path);ftp.cd(path);System.out.println("after path is" + path);return ftp;}/*** 删除文件,可以是文件或文件夹* * @param fileName*            要删除的文件名* @return 删除成功返回true,否则返回false*/public boolean delete(String fileName) {File file = new File(fileName);if (!file.exists()) {System.out.println("删除文件失败:" + fileName + "不存在!");return false;} else {if (file.isFile())return deleteFile(fileName);elsereturn deleteDirectory(fileName);}}/*** 删除单个文件* * @param fileName*            要删除的文件的文件名* @return 单个文件删除成功返回true,否则返回false*/public boolean deleteFile(String fileName) {File file = new File(fileName);// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除if (file.exists() && file.isFile()) {if (file.delete()) {System.out.println("删除单个文件" + fileName + "成功!");return true;} else {System.out.println("删除单个文件" + fileName + "失败!");return false;}} else {System.out.println("删除单个文件失败:" + fileName + "不存在!");return false;}}/*** 删除目录及目录下的文件* * @param dir*            要删除的目录的文件路径* @return 目录删除成功返回true,否则返回false*/public boolean deleteDirectory(String dir) {// 如果dir不以文件分隔符结尾,自动添加文件分隔符if (!dir.endsWith(File.separator))dir = dir + File.separator;File dirFile = new File(dir);// 如果dir对应的文件不存在,或者不是一个目录,则退出if ((!dirFile.exists()) || (!dirFile.isDirectory())) {System.out.println("删除目录失败:" + dir + "不存在!");return false;}boolean flag = true;// 删除文件夹中的所有文件包括子目录File[] files = dirFile.listFiles();for (int i = 0; i

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-227683-1-1.html 上篇帖子: 使用Commons-net包编写FTP客户端出现异常。 下篇帖子: FTP服务器搭建
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表