51,410
社区成员
发帖
与我相关
我的任务
分享public void loginFtp(String path, String ip, Integer port, String username,
String password) {
log.info("ftp ip=" + ip);
log.info("ftp port=" + port);
log.info("username=" + username);
try {
ftp = new FtpClient(ip, port);
ftp.login(username, password);// 登录
ftp.cd(path);
} catch (IOException e) {
log.error(e.getMessage());
String msg = " 登陆ftp:" + ip + "时发生如下错误!" + e.getMessage();
LifeUtil.instance().mail(msg);
e.printStackTrace();
}
}public synchronized void downLoadFtpFile(String filename,
String newfilename, String path, String ip, Integer port,
String username, String password) throws IOException {
this.loginFtp(path, ip, port, username, password);
TelnetInputStream is = null;
FileOutputStream os = null;
try {
is = ftp.get(filename);
java.io.File outfile = new java.io.File(LOCAL_PATH + newfilename);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
log.info("download file " + filename + " success");
} catch (FileNotFoundException e) {
log.error(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
this.closeServer();
}
}package cn.com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;
@SuppressWarnings("static-access")
public class FtpLogin {
private static Log log = LogFactory.getLog(FtpLogin.class);
private FtpClient ftp;
private static String LOCAL_PATH = "D://";
/**
* 登陆ftp
*
* @param path
* 路径
* @param ip
* ip地址
* @param port
* 端口
* @param username
* 用户名
* @param password
* 密码
*/
public void loginFtp(String path, String ip, Integer port, String username,
String password) {
log.info("ftp ip=" + ip);
log.info("ftp port=" + port);
log.info("username=" + username);
try {
ftp = new FtpClient(ip, port);
ftp.login(username, password);// 登录
ftp.cd(path);
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
/**
* 下载文件
*
* @param filename
* 要下载的文件名
* @param newfilename
* 新的文件名
* @param path
* 路径
* @param ip
* ip地址
* @param port
* 端口
* @param username
* 用户名
* @param password
* 密码
* @throws IOException
*/
public synchronized void downLoadFtpFile(String filename,
String newfilename, String path, String ip, Integer port,
String username, String password) throws IOException {
this.loginFtp(path, ip, port, username, password);
TelnetInputStream is = null;
FileOutputStream os = null;
try {
is = ftp.get(filename);
File outfile = new File(LOCAL_PATH + newfilename);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
log.info("download file " + filename + " success");
} catch (FileNotFoundException e) {
log.error(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
this.closeServer();
}
}
/**
* 关闭ftp服务
*/
public void closeServer() {
if (ftp != null) {
try {
ftp.closeServer();
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
}
/**
* 获取ftp上所有文件的文件名
*
* @throws IOException
*/
@SuppressWarnings("unchecked")
public List getFtpFileList(String path, String ip, Integer port,
String username, String password) throws IOException {
List ftpFileList = new ArrayList();
this.loginFtp(path, ip, port, username, password);
BufferedReader reader = null;
InputStreamReader in = null;
try {
in = new InputStreamReader(ftp.list());
reader = new BufferedReader(in);
String line = null;
while ((line = reader.readLine()) != null) {
int pos;
if ((pos = line.lastIndexOf(" ")) < 0) { // 取文件名(如果文件名为空)
continue;
}
String downFileName = line.substring(pos).trim();
if ((line.charAt(0) == '-')) { // line的第一个字母是d的话,是目录,否则不是,这个是Unix的ls
// -l的长格式输出。
ftpFileList.add(downFileName);
}
}
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (in != null) {
in.close();
}
this.closeServer();
}
return ftpFileList;
}
public static void main(String[] args) throws Exception {
FtpLogin f = new FtpLogin();
f.loginFtp(".", "127.0.0.1", 21, "admin", "admin");
List ls = f.getFtpFileList(".", "127.0.0.1", 21, "admin", "admin");
for (int i = 0; i < ls.size(); i++) {
String name = (String)ls.get(i);
f.downLoadFtpFile(name, name, ".", "127.0.0.1", 21, "admin", "admin");
}
}
}