javaftpclient解压⽂件_JAVA中使⽤FTPClient实现⽂件上传下
载
在JAVA程序中,经常需要和FTP打交道,⽐如向FTP服务器上传⽂件、下载⽂件,本⽂简单介绍如何利⽤jakarta commons中的FTPClient(在commons-net包中)实现上传下载⽂件。
⼀、上传⽂件
原理就不介绍了,⼤家直接看代码吧
/**
* Description: 向FTP服务器上传⽂件
* @Version1.0 Jul 27, 2008 4:31:09 PM by 崔红保(cuihongbao@d-heaven)创建
* @param url FTP服务器hostname
* @param port FTP服务器端⼝
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存⽬录
* @param filename 上传到FTP服务器上的⽂件名
* @param input 输⼊流
* @return 成功返回true,否则返回false
*/
publicstaticbooleanuploadFile(String url,intport,String username, String password, String path, String filename, InputStream input) {
booleansuccess =false;
FTPClient ftp = newFTPClient();
try{
intreply;
//如果采⽤默认端⼝,可以使⽤t(url)的⽅式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
returnsuccess;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch(IOException e) {
e.printStackTrace();
} finally{
if(ftp.isConnected()) {
try{
ftp.disconnect();
} catch(IOException ioe) {
}
}
}
returnsuccess;
}
/**
* Description: 向FTP服务器上传⽂件
* @Version1.0 Jul 27, 2008 4:31:09 PM by 崔红保(cuihongbao@d-heaven)创建
* @param url FTP服务器hostname
* @param port FTP服务器端⼝
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存⽬录
* @param filename 上传到FTP服务器上的⽂件名
* @param input 输⼊流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
/
/如果采⽤默认端⼝,可以使⽤t(url)的⽅式直接连接FTP服务器ftp.login(username, password);//登录
reply = ReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
下⾯我们写两个⼩例⼦:
1.将本地⽂件上传到FTP服务器上,代码如下:
@Test
publicvoidtestUpLoadFromDisk(){
try{
FileInputStream in=newFileInputStream(newFile("D:/")); booleanflag = uploadFile("127.0.0.1",21,"test","test","D:/ftp","", in); System.out.println(flag);
} catch(FileNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void testUpLoadFromDisk(){
try {
FileInputStream in=new FileInputStream(new File("D:/"));
boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "", in); System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
2.在FTP服务器上⽣成⼀个⽂件,并将⼀个字符串写⼊到该⽂件中
@Test
publicvoidtestUpLoadFromString(){
try{
InputStream input = newByteArrayInputStream("test ftp".getBytes("utf-8")); booleanflag = uploadFile("127.0.0.1",21,"test","test","D:/ftp","", input); System.out.println(flag);
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Test
public void testUpLoadFromString(){
try {
InputStream input = new ByteArrayInputStream("test ftp".getBytes("utf-8")); boolean flag = uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "", input); System.out.println(flag);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
⼆、下载⽂件
从FTP服务器下载⽂件的代码也很简单,参考如下:
/**connect下载
* Description: 从FTP服务器下载⽂件
* @Version1.0 Jul 27, 2008 5:32:36 PM by 崔红保(cuihongbao@d-heaven)创建
* @param url FTP服务器hostname
* @param port FTP服务器端⼝
* @param username FTP登录账号
* @param password FTP登录密码
* @param remotePath FTP服务器上的相对路径
* @param fileName 要下载的⽂件名
* @param localPath 下载后保存到本地的路径
* @return
*/
publicstaticbooleandownFile(String url,intport,String username, String password, String remotePath,String fileName,String localPath) {
booleansuccess =false;
FTPClient ftp = newFTPClient();
try{
intreply;
//如果采⽤默认端⼝,可以使⽤t(url)的⽅式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
returnsuccess;
}
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器⽬录
FTPFile[] fs = ftp.listFiles();
for(FTPFile ff:fs){
Name().equals(fileName)){
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论