java调用FTP服务
2015-10-29 10:55 D_L 阅读(1011) 评论(0) 编辑 收藏 举报前段时间,公司要编写一个接口用来调用三方开发商提供的ftp服务进行文件的下载。对此做了一些代码备份。
下面把完整的测试代码贴上来大家一些分享一下。
//引入包
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.jfree.util.Log;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class ftptest
{
//定义私有变量
private FtpClient ftpClient;
public FtpClient getFtpClient() {
return ftpClient;
}
public void setFtpClient(FtpClient ftpClient) {
this.ftpClient = ftpClient;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String address; //ftp地址
private int port; //ftp端口
private String username; //ftp用户名
private String password; //ftp密码
//使用下面这个文件连接符的原因是为了屏蔽掉不同平台使用时候的影响,提升一点兼容性。
private static final String code = File.separator;//文件连接符,在windows里面是\\在linux系统里面是/是不一样的
//构造函数
public ftptest(String address, int port, String username, String password)
{
this.address = address;
this.port = port;
this.username = username;
this.password = password;
}
public static void main(String[] args)
{
//实例化
ftptest client = new ftptest("***.***.***.***", **, "***", "***");
try
{
//调用例子,获取文件列表
List list = client.getList("路径");
//下载文件
client.downloadFile("路径");
//上传文件
client.uploadFile("路径");
}
catch (Exception ex)
{
Log.info(ftptest.class.getName() + ex.getMessage());
}
}
//关闭连接
public void closeServer() throws Exception {
ftpClient.closeServer();
}
public boolean connectServer(String address, int port, String user, String pwd)
throws Exception {
boolean isSuccess = false;
try {
ftpClient = new FtpClient();
ftpClient.openServer(address, port);
ftpClient.login(user, pwd);
isSuccess = true;
} catch (Exception ex) {
ex.printStackTrace();
Log.info(ftptest.class.getName() + ex.getMessage());
}
return isSuccess;
}
//目录获取
public List getList(String remotePath) {
List list = new ArrayList();
try {
//连接成功
if (connectServer(address, port, username, password))
{
BufferedReader br = new BufferedReader(new InputStreamReader(ftpClient.nameList(remotePath)));
//下面这两句是用来验证是否开启了数据下载的权限可以去掉 begin
System.out.println(remotePath);
String temp = ftpClient.getResponseString();
System.out.println(temp);
//下面这两句是用来验证是否开启了数据下载的权限可以去掉 end
String readLine = null;
int lastIndex = 0;
String readLine1 = br.readLine();
System.out.println(readLine1);
//去掉remotePath的后缀,可能是'/',也有可能是其他符号
if ((lastIndex = remotePath.lastIndexOf("/")) > 0
||(lastIndex = remotePath.lastIndexOf("//")) > 0
||(lastIndex = remotePath.lastIndexOf("\\")) > 0
||(lastIndex = remotePath.lastIndexOf(code)) > 0)
{
System.out.println(remotePath);
remotePath = remotePath.substring(0, lastIndex);
}
while ((readLine = br.readLine()) != null)
{
int index = readLine.lastIndexOf(code);
System.out.println(index);
list.add(readLine.substring(index+1, readLine.length()));
System.out.println(readLine.substring(index+1, readLine.length()));
System.out.println(readLine);
}
ftpClient.closeServer();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return list;
}
//文件下载
public void downloadFile(String remotePath) throws Exception {
try {
if (connectServer(address, port, username, password))
{
if (remotePath.length() != 0)
{
ftpClient.cd(remotePath);
}
ftpClient.binary();
//这里如果要和上面的文件列表连在一起使用的话需要循环调用,没有找到一起保存的代码
TelnetInputStream is = ftpClient.get("文件名");
File fp = new File(文件路径);
if (!fp.exists())
{
fp.mkdirs();
}
File file_out = new File("下载到那个路径/文件名");
FileOutputStream os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int readBye;
while ((readBye = is.read(bytes)) != -1)
{
os.write(bytes, 0, readBye);
}
is.close();
os.close();
ftpClient.closeServer();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
//文件上传
public void uploadFile(String remotePath) throws Exception {
try {
if (connectServer(address, port, username, password))
{
if (remotePath.length() != 0)
{
ftpClient.cd(remotePath);
}
ftpClient.binary();
TelnetOutputStream os = ftpClient.put("上传到那里");
File file_in = new File("从那个路径上传");
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int readBye;
while ((readBye = is.read(bytes)) != -1)
{
os.write(bytes, 0, readBye);
}
is.close();
os.close();
ftpClient.closeServer();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
以上是一些分享,大家有疑问可以提出一起讨论。