package util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;
public class FtpManager {
private static final Logger log = Logger.getLogger(FtpManager.class);
String localfilename;
String remotefilename;
public FTPClient ftpClient;
private boolean result;// 登录是否成功
//ftp超时时间
private static int ftptimes = Integer.parseInt(Configuration.readConfigString("ftptimes", "config"));
//连接到ftp服务
public FtpManager(String Ip, int Port, String Ftpname, String Ftppass, String path) {
connectServer(Ip, Port, Ftpname, Ftppass, path);
}
/**
* 连接上ftp服务器
*
* @param ip
* @param port
* @param user用户名
* @param password密码
* @param path服务器上的路径
*/
public boolean connectServer(String Ip, int Port, String Ftpname, String Ftppass, String path) {
try {
ftpClient = new FTPClient();
ftpClient.setConnectTimeout(ftptimes);
ftpClient.connect(Ip, Port);
ftpClient.login(Ftpname, Ftppass);
result = true;
if (path.length() != 0) {
ftpClient.changeWorkingDirectory(path);
}
// 设置以二进制方式传输
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
log.debug("ftp[" + Ip + "]登录成功:");
} catch (Exception ex) {
result = false;
log.error("ftp[" + Ip + "]登录失败:");
}
return result;
}
/**
* 把远程系统上的目录切换到参数remoteDirectory所指定的目录
*
* @param remoteDirectory
*/
public void cd(String remoteDirectory) throws IOException {
ftpClient.changeWorkingDirectory(remoteDirectory);
}
/**
* 在远端系统浏览上一级目录
*
* @param remoteDirectory
*/
public void cdup() throws IOException {
ftpClient.changeToParentDirectory();
}
/**
* 发送命令 cmd给服务器
*
* @param cmd
* @throws IOException
*/
public void sendServer(String cmd) throws IOException {
ftpClient.sendSiteCommand(cmd);
}
/**
* 关闭ftp连接
*/
public void closeConnect() {
try {
ftpClient.disconnect();
} catch (IOException ex) {
log.error("断开FTP服务器失败", ex);
}
}
/**
* 上传到ftp服务器
*
* @throws Exception
*/
public boolean upload(String remoteFile, String localFile) {
boolean result = false;
OutputStream os = null;
java.io.File file_in = new java.io.File(localFile);
FileInputStream is = null;
try {
log.debug(localFile + "FTP文件开始上传。。。。。");
os = ftpClient.storeFileStream(remoteFile);
is = new FileInputStream(file_in);
byte[] bytes = new byte[4096];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
log.debug(localFile + "FTP文件上传成功");
result = true;
} catch (Exception ex) {
result = false;
log.error(localFile + ",FTP上传文件失败", ex);
} finally {
try {
if (is != null)
is.close();
if (os != null)
os.close();
} catch (IOException e) {
log.error("关闭流失败", e);
}
}
log.debug(localFile + "FTP文件结束上传。。。。。结果:" + result);
return result;
}
/**
* 指定路径从ftp服务器下载
*
* @param remoteFile
* @param localFile
*/
public void download(String remoteFile, String localFile) throws Exception {
try {
InputStream is = ftpClient.retrieveFileStream(remoteFile);
File file_in = new File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
os.close();
is.close();
} catch (IOException ex) {
log.error("FTP下载文件失败", ex);
}
}
/**
* 判断Ftp目录是否存在,如果不存在则创建目录
* */
public void isDirExist(String dir) {
try {
if (!ftpClient.changeWorkingDirectory(dir)) {
if (ftpClient.makeDirectory(dir)) {
ftpClient.changeWorkingDirectory(dir);
}
}
} catch (Exception e) {
log.error("目录创建失败", e);
}
}
public static void main(String agrs[]) {
try {
FtpManager f = new FtpManager("125.88.109.47", 21, "hzdracom","hzdracom", "");
// 获取文件列表
// f.isDirExist("upload/hftest");
f.upload("hftest/hf/wppp.tif", "d:\\mcs\\201502\\4154720150211101913.tif");
f.closeConnect();
System.out.println("=====================");
} catch (Exception ex) {
System.out.println(ex);
}
}
public boolean isResult() {
return result;
}
public void setResult(boolean result) {
this.result = result;
}
}
apache方式ftp文件操作
最新推荐文章于 2024-08-06 19:36:49 发布