您的位置:首页 > 编程语言 > Java开发

JAVA中使用FTPClient上传下载

2017-10-09 15:36 453 查看
在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件、下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件,我创建的是mvn项目,所以在pom.xml文件中添加依赖:
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/oro/oro -->
<dependency>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
<version>2.0.8</version>
</dependency>


自己写代码前可以先看下官方的API,官网写的还是很详细的(http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html),简单截个图看下:



好了,简单写个例子,很简单,一看就明白了
/**
* 向FTP服务器上传文件
*
* @param url        FTP服务器url
* @param port       FTP服务器端口
* @param username
* @param password
* @param path       本地文件目录
* @param remoteFile FTP服务器保存文件名
*/
public void sendToServer(String url, int port, String username, String password,
String path, String remoteFile) {
//创建ftp客户端
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("GBK");
try {
//链接ftp服务器
ftpClient.connect(url, port);
//登录ftp
ftpClient.login(username, password);
int reply = ftpClient.getReplyCode();
System.out.println(reply);
//如果reply返回230就算成功了,如果返回530密码用户名错误或当前用户无权限下面有详细的解释。
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return;
}
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

ftpClient.changeWorkingDirectory("/data");//修改工作目录
File file = new File(path);
InputStream input = new FileInputStream(file);
ftpClient.storeFile(remoteFile, input);//文件名若是不指定就会上传到root目录下
input.close();
ftpClient.logout();

} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

}

}

/**
* Description: 从FTP服务器下载文件
*
* @param remotePath FTP服务器上的相对路径
* @param fileName   要下载的文件名
* @param localPath  下载后保存到本地的路径
*/
public static boolean download(String url, int port, String username, String password,
String remotePath, String fileName, String localPath) {
boolean success = false;
//创建ftp客户端
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("GBK");
try {
int reply;
ftpClient.connect(url, port);
//如果采用默认端口,可以使用ftpClient.connect(url)的方式直接连接FTP服务器
ftpClient.login(username, password);//登录
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return false;
}
ftpClient.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());

OutputStream is = new FileOutputStream(localFile);
ftpClient.retrieveFile(ff.getName(), is);
is.close();
}
}

ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}


参考:

http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html

http://wuhongyu.iteye.com/blog/436386

http://blog.csdn.net/hbcui1984/article/details/2720204

http://dengli19881102.iteye.com/blog/1028957

http://blog.csdn.net/dzy784858512/article/details/41279709

http://blog.csdn.net/u012540337/article/details/20720785
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: