JavaShuo
欄目
標籤
ftp 上傳下載
時間 2019-11-12
標籤
ftp
上傳
下載
欄目
FTP
简体版
原文
原文鏈接
package lemote.test.mid.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import lemote.test.mid.properties.DefaultValue;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import android.os.Environment;
public class FtpUnit {
private FTPClient ftpClient = null;
private String SDPATH;
public FtpUnit(){
SDPATH =Environment.getExternalStorageDirectory()+"/";
}
/**
* 鏈接Ftp服務器
*/
public void connectServer(){
if(ftpClient == null){
int reply;
try{
ftpClient = new FTPClient();
ftpClient.setDefaultPort(21);
ftpClient.configure(getFtpConfig());
ftpClient.connect("172.16.18.175");
ftpClient.login("anonymous","");
ftpClient.setDefaultPort(21);
reply = ftpClient.getReplyCode();
System.out.println(reply+"----");
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
}
ftpClient.enterLocalPassiveMode();
ftpClient.setControlEncoding("gbk");
}catch(Exception e){
e.printStackTrace();
}
}
}
/**
* 上傳文件
* @param localFilePath--本地文件路徑
* @param newFileName--新的文件名
*/
public void uploadFile(String localFilePath,String newFileName){
connectServer();
//上傳文件
BufferedInputStream buffIn=null;
try{
buffIn=new BufferedInputStream(new FileInputStream(SDPATH+"/"+localFilePath));
System.out.println(SDPATH+"/"+localFilePath);
System.out.println("start="+System.currentTimeMillis());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.storeFile("a1.mp3", buffIn);
System.out.println("end="+System.currentTimeMillis());
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(buffIn!=null)
buffIn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
/**
* 下載文件
* @param remoteFileName --服務器上的文件名
* @param localFileName--本地文件名
*/
public void loadFile(String remoteFileName,String localFileName){
connectServer();
System.out.println("==============="+localFileName);
//下載文件
BufferedOutputStream buffOut=null;
try{
buffOut=new BufferedOutputStream(new FileOutputStream(SDPATH+localFileName));
long start = System.currentTimeMillis();
ftpClient.retrieveFile(remoteFileName, buffOut);
long end = System.currentTimeMillis();
System.out.println(end-start);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(buffOut!=null)
buffOut.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
/**
* 設置FTP客服端的配置--通常能夠不設置
*
@return
*/
private static FTPClientConfig getFtpConfig(){
FTPClientConfig ftpConfig=new FTPClientConfig(FTPClientConfig.SYST_UNIX);
ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);
return ftpConfig;
}
/**
* 關閉鏈接
*/
public void closeConnect(){
try{
if(ftpClient!=null){
ftpClient.logout();
ftpClient.disconnect();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
相關文章
1.
ftp上傳下載
2.
FTP上傳下載
3.
ftp上傳下載,ftp上傳下載怎麼操做
4.
python ftp 上傳下載
5.
FTP文件上傳下載
6.
python FTP上傳和下載
7.
ftp上傳工具,ftp上傳工具下載,使用教程
8.
ftp下載工具,最佳的ftp上傳下載工具
9.
【FTP】FTP文件上傳下載-支持斷點續傳
10.
Qt5.X FTP上傳與下載
更多相關文章...
•
PHP 文件上傳
-
PHP教程
•
MySQL下載步驟詳解
-
MySQL教程
•
Flink 數據傳輸及反壓詳解
•
Tomcat學習筆記(史上最全tomcat學習筆記)
相關標籤/搜索
上傳下達
上上下下
下載
ftp
下傳
上載
上傳
上下
文件上傳和下載教科書
FTP
SQLite教程
Redis教程
PHP 7 新特性
數據傳輸
0
分享到微博
分享到微信
分享到QQ
每日一句
每一个你不满意的现在,都有一个你没有努力的曾经。
最新文章
1.
Duang!超快Wi-Fi來襲
2.
機器學習-補充03 神經網絡之**函數(Activation Function)
3.
git上開源maven項目部署 多module maven項目(多module maven+redis+tomcat+mysql)後臺部署流程學習記錄
4.
ecliple-tomcat部署maven項目方式之一
5.
eclipse新導入的項目經常可以看到「XX cannot be resolved to a type」的報錯信息
6.
Spark RDD的依賴於DAG的工作原理
7.
VMware安裝CentOS-8教程詳解
8.
YDOOK:Java 項目 Spring 項目導入基本四大 jar 包 導入依賴,怎樣在 IDEA 的項目結構中導入 jar 包 導入依賴
9.
簡單方法使得putty(windows10上)可以免密登錄樹莓派
10.
idea怎麼用本地maven
本站公眾號
歡迎關注本站公眾號,獲取更多信息
相關文章
1.
ftp上傳下載
2.
FTP上傳下載
3.
ftp上傳下載,ftp上傳下載怎麼操做
4.
python ftp 上傳下載
5.
FTP文件上傳下載
6.
python FTP上傳和下載
7.
ftp上傳工具,ftp上傳工具下載,使用教程
8.
ftp下載工具,最佳的ftp上傳下載工具
9.
【FTP】FTP文件上傳下載-支持斷點續傳
10.
Qt5.X FTP上傳與下載
>>更多相關文章<<