常见的FTP工具类

/* * 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); else return 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值