/**
* 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;
}
以上就是ftp上传实例代码的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号