使用ftpclient構造自己的ftp類
所有操作需要放在子執行緒
public class ftpmanager
public ftpmanager(string ip, int port, string username, string password)
private boolean uploadfile(string remotepath, string remotefilename, file file) else
if (ftpclient.changeworkingdirectory(remotepath)) else
fileinputstream fileinputstream = new fileinputstream(file);
bufferedinputstream bufferedinputstream = new bufferedinputstream(fileinputstream);
succeed = ftpclient.storefile(remotefilename, bufferedinputstream);
bufferedinputstream.close();
fileinputstream.close();
} catch (filenotfoundexception e) catch (ioexception e)
return succeed;
}private void downloadfile(string serverpath)
bufferedoutputstream.flush();
bufferedoutputstream.close();
fileoutputstream.close();
inputstream.close();
} catch (ioexception e)
closeconnection();
}private void openconnection() else
ftpclient.login(ftpusername, ftppassword);
reply = ftpclient.getreplycode();
if (!ftpreply.ispositivecompletion(reply)) else
} catch (ioexception e)
}private void closeconnection() catch (ioexception e) }}
/*** 通過可變長陣列傳入多個檔案或單個檔案
*/public class uploadfiles extends asynctask
@override
protected boolean doinbackground(file... params)
return succeed;
}@override
protected void onpostexecute(boolean succeed) else
} else }}
/*** uid_日期_時間.mp4,
* 日期格式為:yyyy-mm-dd, 日期格式為:hh:mm:ss, 為24小時格式
* old name: 2016-11-2_15:43:23.mp4
* new name: 65555_2016-11-2_15:43:23.mp4
* @param file
* @return
*/private string constructremotefilename(file file)
string year_month_day = oldname.split("_")[0];
string hour_minute_second = oldname.split("_")[1];
int uid = user.getuid();
string newname = string.format("uid_%d_%s_%s", uid, year_month_day, hour_minute_second);
errrpt.debug("ftp", "upload new remote filename: " + newname);
return newname;
}/**
* 在子執行緒中傳送file list的檔案
* seg_corp_uid_日期_時間.txt
* 日期格式為:yyyy-mm-dd, 日期格式為:hh:mm:ss, 為24小時格式**/
private class sendsegfile implements runnable else
}@override
public void run()
file file = new file(dir, filename);
if (!file.exists()) catch (ioexception e)
}string content = "";
for (string s : contents)
try catch (filenotfoundexception e) catch (ioexception e)
string remotepath = "/home/" + ftpusername + "/video/logs/" + nowtime.split("_")[0];
boolean succeed = uploadfile(remotepath, filename, file);
if (succeed) else
closeconnection();}}
//public class downloadsinglefile extends asynctask
}
JAVA中使用FTPClient上傳
一 上傳檔案 原理就不介紹了,大家直接看 吧 description 向ftp伺服器上傳檔案 version1.0 param url ftp伺服器hostname param port ftp伺服器端口 param username ftp登入賬號 param password ftp登入密碼 p...
Vsftpd使用FtpClient上傳檔案踩過的坑
1.切換工作目錄返回false怎麼破?ftpclient.changeworkingdirectory images 切換工作目錄是目錄直接寫相對於local user目錄的路徑即可。2.上傳檔案損壞怎麼破?設定檔案上傳形式為二進位制格式即可。ftpclient.setfiletype ftp.bi...
apache 的FTPClient使用以及注意事項
tomcat apache jk進行集群後,要進行共享,經過網上的搜尋可以有多種方式實現。一種是使用jcifs。jcifs可以實現網路中共享檔案的讀寫,但是前提是,檔案必須共享,還要在同乙個區域網內。所以如果電腦上禁止了檔案共享的話,就行不通了。一種是使用sun提供的ftpclient。ftpcli...