现今FTP上传和下载应用广泛,很多银行和保险公司等都在使用,FTP上传和下载方法也有很多,下面列出一种常用的也是我实现过的方式。
第一步,连接FTP;
//连接ftp服务器
private boolean connectServer(String ip, String user, String password,
String path) throws IOException {
// server:FTP服务器的IP地址;user:登录FTP服务器的用户名;password:登录FTP服务器的用户名的口令;path:FTP服务器上的
// 路径
try {
ftpClient = new FtpClient();
ftpClient.openServer(ip);
ftpClient.login(user, password);
if (path.length() != 0) { // path是ftp服务下主目录的子目录
ftpClient.cd(path);
}
ftpClient.binary(); // 用2进制上传
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
第二步创建目标目录(这是为上传做准备,如果有此目录就不会创建,没有才会创建,如果是下载就没必要了)
/**
* 创建目标目录
* @param dir
* @return
*/
public boolean createDir(String dir) {
try {
connectServer(server, user, password, path);
ftpClient.ascii();
StringTokenizer s = new StringTokenizer(dir, "/"); // sign
s.countTokens();
String pathName = ftpClient.pwd();
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpClient.sendServer("MKD " + pathName + "\r\n");
} catch (Exception e) {
e = null;
return false;
}
ftpClient.readServerResponse();
}
ftpClient.binary();
return true;
} catch (IOException e1) {
e1.printStackTrace();
return false;
}
}
第三步上传或者下载
上传:
/**
* 上传到指定的FTP服务器
* @param vtsFilePath
* @param strEdorAcceptNo
* @return
* @throws Exception
*/
public boolean uploadFile(String vtsFilePath, String strEdorAcceptNo,String uploadPath)
throws Exception {
if (server == null || "".equals(server)) {
return false;
}
TelnetOutputStream os = null;
BufferedInputStream bis = null;
try {
// 连接ftp服务器
ftpClient.openServer(server);
ftpClient.login(user, password);
// path是ftp服务下主目录的子目录
path=path+"/"+uploadPath;
if (path != null && path.length() != 0) {
try {
ftpClient.cd(path);
} catch (Exception e) {
if(createDir(path)){
ftpClient.cd(path);
}
}
}
// 用2进制上传
ftpClient.binary();
try {
File file = new File(vtsFilePath);
FileInputStream fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
if (bis == null) {
return false;
}
// 用ftp上传后的新文件名
System.out.println(strEdorAcceptNo);
os = ftpClient.put(strEdorAcceptNo);
byte[] bytes = new byte[1024];
int c;
while ((c = bis.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
bis.close();
os.close();
logger.debug("文件上传成功!");
// }
} catch (IOException e) {
// ExcAct.doHandle(e);
logger.debug("上传文件失败!");
e.printStackTrace();
return false;
}
finally {
if (bis != null) {
bis.close();
}
if (os != null) {
os.close();
}
}
} catch (IOException e) {
if (e instanceof java.net.UnknownHostException) {
logger.debug("您输入的IP地址无法解析!");
} else if (e instanceof sun.net.ftp.FtpLoginException) {
logger.debug("您输入的用户名或密码不正确!");
} else if (e instanceof java.io.FileNotFoundException) {
logger.debug("您输入的文件路径不存在!");
} else {
logger.debug(e.getMessage());
}
return false;
}
finally {
try {
if (ftpClient != null) {
ftpClient.closeServer();
}
} catch (IOException e) {
logger.debug("关闭与ftp服务器的连接时失败!");
return false;
}
}
return true;
}
下载:
/**
* FTP文件下载
* @param savePath 存放文件的地址(本地)
* @param fileName 要获取的文件名字(服务器上要存在的)
*/
public boolean download(String savePath, String fileName,String downloadPath) {
try {
path=path+"/"+downloadPath;
connectServer(server, user, password, path);
} catch (Exception e) {
e.printStackTrace();
logger.debug("服务器开启失败!");
return false;
}
TelnetInputStream ftpIn = null;
FileOutputStream ftpOut = null;
try {
ftpIn = ftpClient.get(fileName); // fileName为FTP服务器上要下载的文件名
byte[] buf = new byte[204800];
int bufsize = 0;
ftpOut = new FileOutputStream(savePath + "\\" + fileName); // 存放在本地硬盘的物理位置
while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
ftpOut.write(buf, 0, bufsize);
}
logger.debug("文件下载成功!");
} catch (Exception e) {
logger.debug("下载文件失败!");
e.printStackTrace();
return false;
} finally {
if (ftpIn != null) {
try {
ftpIn.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
if (ftpOut != null) {
try {
ftpOut.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
if (ftpClient != null) {
try {
ftpClient.closeServer();
} catch (IOException e) {
logger.debug("关闭服务器失败!");
e.printStackTrace();
return false;
}
}
}
return true;
}
注意:connectServer(server, user, password, path);中第四个参数如果是"/",是FTP用户对应的文件夹。