jdk.ftp


package ftp.jdk;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;
import ftp.FtpUser;
/**
* JavaJDK自带ftp操作
* @author cKF29080
* @version [版本号, Nov 24, 2010]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class FtpJdk {

private FtpClient ftpClient =null;

public boolean loginFtp(String path)
throws Exception {
boolean isSuccess = false;
Properties properties = new Properties();
try {
File file=new File(path);
InputStream fileInputStream=new FileInputStream(file);
properties.load(fileInputStream);
String ip = properties.getProperty("Ip");
String name = properties.getProperty("Ip");
String ps = properties.getProperty("Ip");
// saveDir = new String(saveDir.getBytes("ISO-8859-1"),"UTF-8");
fileInputStream.close();
ftpClient = new FtpClient(ip);
String str = ftpClient.getResponseString();
System.out.println("log: "+str);
ftpClient.login(name,ps);
isSuccess = true;
} catch (Exception ex) {
throw new Exception("Connect ftp server error:" + ex.getMessage());
}
return isSuccess;
}

/**
* 下载文件
* @param remotePath ftp目录
* @param localPath 下载本地目录
* @param filename 下载文件名称
* @return [参数说明]
* @return boolean [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public boolean downloadFile(String remotePath,String localPath, String filename) {
try {

if (remotePath.length() == 0){

return false;
}
/**更改目录*/
ftpClient.cd(remotePath);
String str = ftpClient.getResponseString() ;
System.out.println("log:"+str) ;
/****设置为 二进制输出***/
ftpClient.binary();
/***获得文件流***/
TelnetInputStream is = ftpClient.get(filename);
/**构造本地路径**/
File file =new File(localPath);
if (!file.exists()) {
file.mkdirs();
}
File file_out = new File(file, filename);
/**输出流**/
FileOutputStream os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
ftpClient.closeServer();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值