一、ftp文件上传
/**
* 获取FTPClient对象
*
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @return
*/
public static FTPClient getFTPClient(String ftpHost, String ftpUserName,
String ftpPassword) {
FTPClient ftpClient =null;
try {
ftpClient = new FTPClient();
ftpClient.setDataTimeout(20000);
ftpClient.connect(ftpHost);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
LogUtils.e(TAG,"未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
LogUtils.i(TAG,"FTP连接成功。");
}
} catch (SocketException e) {
e.printStackTrace();
LogUtils.e(TAG,"FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
LogUtils.e(TAG,"FTP的端口错误,请正确配置。");
}
return ftpClient;
}
/**
* Description: 向FTP服务器上传文件
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param fileName ftp文件名称
* @param input 文件流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String ftpPath,
String fileName, InputStream input) {
String ftpHost="ftp地址";
String ftpUserName="账号";
String ftpPassword="密码";
boolean success = false;
FTPClient ftpClient = null;
try {
int reply;
ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword);
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return success;
}
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
boolean b = ftpClient.makeDirectory(ftpPath);
if (b){
LogUtils.w(TAG,"make success "+ftpPath);
}else {
LogUtils.w(TAG,"make fail "+ftpPath);
}
ftpClient.changeWorkingDirectory(ftpPath);
ftpClient.storeFile(fileName, input);
input.close();
ftpClient.getReply();
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
二、ftp文件下载
/** Description: 从FTP服务器下载文件
* @param hostUrl FTP服务器host
* @param remotePath FTP服务器上的相对路径
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @return
*/
public static File ftpDownFile(String hostUrl, String remotePath, String fileName, String localPath, RemoteTask.IDownloadListener listener) {
FTPClient ftp = new FTPClient();
InputStream is=null;
OutputStream fileOutputStream=null;
try {
if (ftp.isConnected()) {//判断是否已登陆
ftp.disconnect();
}
int reply;
ftp.setDataTimeout(20000);
ftp.connect(hostUrl);
//ftp.connect(url, port);
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login("账号","密码");//登录
ftp.enterLocalPassiveMode();
ftp.setFileType(FTP.BINARY_FILE_TYPE);//设置使用二进制传输
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return null;
}
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for(FTPFile ff:fs){
if(ff.getName().equals(fileName)){
File localFile = new File(localPath+"/"+fileName);
if (!localFile.getParentFile().exists()){
localFile.getParentFile().mkdirs();
}
fileOutputStream = new FileOutputStream(localFile);
String remote=remotePath + "/" + ff.getName();
is=ftp.retrieveFileStream(new String(remote.getBytes("UTF-8"), "ISO-8859-1"));
if (is!=null){
byte[] buff=new byte[1024*3];
int len=-1;
long length = ff.getSize();
int downloadLength=0;//已经下载的长度
if(listener!=null){
listener.start();
}
while((len=is.read(buff))!=-1) {
fileOutputStream.write(buff, 0, len);
fileOutputStream.flush();
downloadLength += len;
double d = downloadLength * 100.0 / length;
//取两位小数
BigDecimal bigDecimal = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入保留小数点后两位 93.56789 = 93.57
float progress = bigDecimal.floatValue();
//更新进度
if (listener != null) {
listener.upgradeProgress(progress, null);
}
}
is.close();
return localFile;
}
}
}
// 主动调用一次getReply()把接下来的226消费掉. 这样做是可以解决is返回null问题
ftp.getReply();
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
} catch (RuntimeException e){
e.printStackTrace();
LogUtils.e(TAG,"ftpDownFile RuntimeException..."+e);
} finally {
if (is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
if (listener!=null){
listener.error("下载失败!");
}
return null;
}