`
ljs120ljs
  • 浏览: 7497 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

java ftp实例

阅读更多
package ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpLoginException;

public class FileUpdateFTP
{
private FtpClient ftpClient;//ftp
private String  ip;//主机(服务)
private int port;//端口
private String userName;//用户名
private String password;//密码
private String selectPath;//客户端选择路径
private String serverPath;//上传到指定的目录  如果未指定则上传到根目录


/**
* @param ip 主机(服务)
* @param port 端口
* @param userName 用户名
* @param password 密码
* @param selectPath 客户端选择路径
* @param serverPath 上传到服务器指定的目录  如果未指定则上传到根目录
*/
public FileUpdateFTP(String ip, int port, String userName, String password, String selectPath,
String serverPath)
{
super();
this.ip = ip;
this.port = port;
this.userName = userName;
this.password = password;
this.selectPath = selectPath;
this.serverPath = serverPath;
this.setServerPath(serverPath);
}

/**
* 进行服务器连接
* @param ip
* @param port
* @param userName
* @param password
* @param path
* @return
*/
public String connectServer()
{
String msg = "";
try
{
ftpClient = new FtpClient(ip, port);
ftpClient.openServer(ip);
ftpClient.login(userName, password);
ftpClient.binary();
}
catch (FtpLoginException e)
{
msg = "登陆主机:" + ip + "失败!请检查用户名或密码是否正确:" + e;

}
catch (IOException e)
{
msg = "连接主机:" + ip + "失败!请检查端口是否正确:" + e;
}
catch (SecurityException e)
{
msg = "无权限和主机:" + ip + "连接!请检查是否有访问权限:" + e;
}
return msg;
}
/**
* 获取文件及子文件
* @param dirname
* @throws Exception
* @throws Exception
*/
public void getFiles(String dirname) throws Exception
{
File dir = new File(dirname);
//获取所有目录
File[] files = dir.listFiles();
if (files!=null && !"".equals(files))
{
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
{// 判断是否是目录
//如果服务器上没有这个目录则创建
String tempDir = files[i].getPath().substring(getSelectPath().length()).replace("\\", "/");
tempDir = getServerPath()+tempDir;
if (!isDirExist(tempDir))
{
createDir(tempDir);
}
getFiles(files[i].getPath());
}
/*if (files[i].isHidden())
{// 判断是否是隐藏文件
System.out.println(files[i].getName() + "=>它是一个隐藏文");
}*/
if (files[i].isFile())
{// 判断是否是文件
fileUpdate(files[i]);
}
}
}
}

/**
* 上传文件
* @param file
* @return
*/
public boolean fileUpdate(File file)
{
boolean flag = false;
try
{
ftpClient.cd(this.cheangPath(file.getPath()));
ftpClient.binary();
TelnetOutputStream os = ftpClient.put(file.getName());
FileInputStream is = new FileInputStream(file);
byte[] bytes = new byte[2048];
int c;
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
}
is.close();
os.close();
flag = true;
}
catch (IOException ex)
{
ex.printStackTrace();
}
return flag;
}

/**
* 获取当前的FTP路径
*
* @param path
* @return
*/
private String cheangPath(String path)
{
path = path.substring(getSelectPath().length()).replace("\\", "/");

if ("".equals(path))
{
path = "/";
}
else
{
path = path.substring(0, path.lastIndexOf("/") + 1);
}
return getServerPath()+path;
}
/**
* 创建文件夹
*
* @param dir
* @param ftpClient
* @throws Exception
*/
private void createDir(String dir) throws Exception
{
ftpClient.ascii();
StringTokenizer s = new StringTokenizer(dir, "/"); // sign
s.countTokens();
String pathName = "";
while (s.hasMoreElements())
{

pathName = pathName + "/" + (String) s.nextElement();

try
{
ftpClient.sendServer("MKD " + pathName + "\r\n");
}
catch (Exception e)
{
e = null;
}
ftpClient.readServerResponse();
}
ftpClient.binary();

}
/**
* 检查文件夹是否存在
*
* @param dir
* @param ftpClient
* @return
*/
private Boolean isDirExist(String dir)
{
try
{
ftpClient.cd(dir);
}
catch (Exception e)
{

return false;
}
return true;
}
/**
* 关闭连接
*/
public void closeConnect()
{
try
{
if (ftpClient != null)
{
ftpClient.closeServer();
}
System.out.println("disconnect");
}
catch (IOException ex)
{
System.out.println("not disconnect");
System.out.println(ex);
}
}
public static List<ServerBean> getList()
{
List<ServerBean> serverBeanList= new ArrayList<ServerBean>();
ServerBean serverBean = null;

serverBean = new ServerBean("192.168.1.42", 21, "test", "test", "E:/SinaUC", "c:/SinaUC");
return serverBeanList;
}
public static void main(String[] args)
{
List<ServerBean> serverBeanList = getList();
ServerBean serverBean = null;

int count = 0;
boolean flag = true;
String msg = "";
FileUpdateFTP ftp = null;
for (int i = 0; i < serverBeanList.size(); )
{
count++;
System.out.println(count);

try
{
serverBean = serverBeanList.get(i);
ftp = new FileUpdateFTP(serverBean.getIp(), serverBean.getPort(),
serverBean.getUserName(), serverBean.getPassword(), serverBean.getSelectPath(), serverBean.getServerPath());
msg = ftp.connectServer();

if ("".equals(msg))
{
ftp.getFiles(serverBean.getSelectPath());
count = 0;
i++;
}
else
{
flag = false;
}
}
catch (SocketException e)
{
System.out.println("中途中断"+e);
i++;
count = 0;
flag = true;
}
catch (Exception e) {
System.out.println("连接失败");
}
finally
{
ftp.closeConnect();
}
if (count==3)
{
i++;
count = 0;
flag = true;
}
if (flag)
{
if ("".equals(msg))
{
msg = "操作成功";
}
System.out.println(msg);
}
}
}
public FtpClient getFtpClient()
{
return ftpClient;
}

public void setFtpClient(FtpClient ftpClient)
{
this.ftpClient = ftpClient;
}

public String getIp()
{
return ip;
}

public void setIp(String ip)
{
this.ip = ip;
}

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;
}


public String getSelectPath()
{
return selectPath;
}

public void setSelectPath(String selectPath)
{
this.selectPath = selectPath;
}

public String getServerPath()
{
return serverPath;
}

public void setServerPath(String serverPath)
{
this.serverPath = serverPath.replace("\\", "/");
}


}
分享到:
评论
1 楼 sh747665463 2012-08-27  
getList函数里面的ServerBean是什么啊?

相关推荐

Global site tag (gtag.js) - Google Analytics