常见的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;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;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]