/**
* ftp上传工具类
*
*/
public class ftputil {
static logger logger = logger.getlogger(ftputil.class.getname());
private string ip;
private string username;
private string password;
private int port = -1;
private string path;
private outputstream os = null;
private fileinputstream is = null;
private ftpclient ftpclient;
public ftpclient getftpclient() { return ftpclient; }
public void setftpclient(ftpclient ftpclient) {
this.ftpclient = ftpclient;
}
public ftputil(){
}
public ftputil(string serverip, string username, string password) {
this.ip = serverip;
this.username = username;
this.password = password;
}
public ftputil(string serverip, int port, string username, string password) {
this.ip = serverip;
this.username = username;
this.password = password;
this.port = port;
this.ftpclient= new ftpclient();
}
/**
*
* @return 是否连接服务器
*/
public boolean ftplogin() {
boolean islogin = false;
ftpclientconfig ftpclientconfig = new ftpclientconfig();
ftpclientconfig.setservertimezoneid(timezone.getdefault().getid());
this.ftpclient.setcontrolencoding("gbk");
this.ftpclient.configure(ftpclientconfig);
try {
if (this.port > 0) {
this.ftpclient.connect(this.ip, this.port);
}else {
this.ftpclient.connect(this.ip);
}
int reply = this.ftpclient.getreplycode();
if (!ftpreply.ispositivecompletion(reply)) {
this.ftpclient.disconnect();
logger.info("登录ftp服务失败!");
return islogin;
}
if(this.ftpclient.login(this.username, this.password)){
this.ftpclient.enterlocalpassivemode(); // 设置传输协议
this.ftpclient.setfiletype(ftpclient.binary_file_type);
logger.info(this.username + "成功登陆ftp服务器");
islogin = true;
}else{
logger.info(this.username + "登录ftp服务失败!");
}
}catch (exception e) {
e.printstacktrace();
logger.error( e.getmessage());
}
this.ftpclient.setbuffersize(1024 * 2);
this.ftpclient.setdatatimeout(30 * 1000);
return islogin;
}
/**
* @退出关闭服务器链接
*/
public void ftplogout() {
if (null != this.ftpclient && this.ftpclient.isconnected()) {
try {
boolean reuslt = this.ftpclient.logout();// 退出ftp服务器
if (reuslt) {
logger.info("成功退出服务器");
}
}catch (ioexception e) {
e.printstacktrace();
logger.warn("退出ftp服务器异常!" + e.getmessage());
}finally {
try {
this.ftpclient.disconnect();
}catch (ioexception e) {
e.printstacktrace();
logger.error("关闭ftp服务器的连接异常!");
}
}
}
}
/**
* 上传ftp文件
* @param localfile 当地文件
* @param romotuploadepath 上传服务器路径
*/
public boolean uploadfile(file localfile, string romotuploadepath) {
bufferedinputstream instream = null;
boolean success = false;
try {
this.ftpclient.changeworkingdirectory(romotuploadepath);
instream = new bufferedinputstream(new fileinputstream(localfile));
success = this.ftpclient.storefile(localfile.getname(), instream);
if (success == true) {
system.out.println(localfile.getname() + "上传成功");///
return success;
}
}catch (filenotfoundexception e) {
e.printstacktrace();
}catch (ioexception e) {
e.printstacktrace();
}finally {
if (instream != null) {
try {
instream.close();
}catch (ioexception e) {
e.printstacktrace();
}
}
}
return success;
}
/**上传文件夹
* @param localdirectory 本地文件夹
* @param remotedirectorypath 远程路径
*/
public boolean uploaddirectory(string localdirectory,string remotedirectorypath) {
file src = new file(localdirectory);
system.out.println(src.getname());
try {
remotedirectorypath = remotedirectorypath + src.getname() + "/";
boolean makedirflag = ftpclient.makedirectory(remotedirectorypath);
system.out.println("localdirectory : " + localdirectory);
system.out.println("remotedirectorypath : " + remotedirectorypath);
system.out.println("src.getname() : " + src.getname());
system.out.println("remotedirectorypath : " + remotedirectorypath);
system.out.println("makedirflag : " + makedirflag);
// ftpclient.listdirectories();
}catch (ioexception e) {
e.printstacktrace();
}
file[] allfile = src.listfiles();
if(allfile.length==0||allfile.length>0){
for (int currentfile = 0;currentfile if (!allfile[currentfile].isdirectory()) {
string srcname = allfile[currentfile].getpath().tostring();
uploadfile(new file(srcname), remotedirectorypath);
}
}
for (int currentfile = 0;currentfile if (allfile[currentfile].isdirectory()) {
uploaddirectory(allfile[currentfile].getpath().tostring(), //递归
remotedirectorypath);
}
}
}
/*for (int currentfile = 0;currentfile if (!allfile[currentfile].isdirectory()) {
string srcname = allfile[currentfile].getpath().tostring();
uploadfile(new file(srcname), remotedirectorypath);
}
}
for (int currentfile = 0;currentfile if (allfile[currentfile].isdirectory()) {
uploaddirectory(allfile[currentfile].getpath().tostring(), //递归
remotedirectorypath);
}
} */
return true;
}
0
0
相关文章
如何在Java中比较自定义类对象的字段值并检测相邻输入的电量突变
如何正确将 JSONObject 发送至 AWS SQS 队列
Java中如何为Stack实现底部插入功能:静态方法调用与类设计要点
如何在 Android Studio 中为小米手机正确申请和检测运行时权限
如何在 macOS 上正确运行 Java TCP 服务器/客户端通信程序
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具









