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.
shell编译问题
2.
mipsel 编译问题
3.
添加xml
4.
直方图均衡化
5.
FL Studio钢琴卷轴之画笔工具
6.
中小企业为什么要用CRM系统
7.
Github | MelGAN 超快音频合成源码开源
8.
VUE生产环境打包build
9.
RVAS(rare variant association study)知识
10.
不看后悔系列!DTS 控制台入门一本通(附网盘链接)
本站公众号
欢迎关注本站公众号,获取更多信息
相关文章
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上传与下载
>>更多相关文章<<