ftpClient类囊括了Java对于ftp服务器上的一系列操作。
ftpClient类继承自ftp类,增加了上传文件流、下载文件流、获得目录名称和文件名称等一系列操作。
创建ftpClient对象
ftpClient = new FTPClient();
连接ftpClient对象,为该对象添加一个connect方法
public boolean connect() {
boolean isConnectionSuccess = false;
if (ftpClient == null) {
ftpClient = new FTPClient();
}
try {
ftpClient.connect(host, port);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
return false;
}
isConnectionSuccess = ftpClient.login(userName, password);
if (!isConnectionSuccess) {
return false;
}
if (!ftpClient.setFileType(FTP.BINARY_FILE_TYPE)) {
return false;
}
if (workingDirectory != null) {
ftpClient.makeDirectory(workingDirectory);
if (!ftpClient.changeWorkingDirectory(workingDirectory)) {
return false;
}
}
ftpClient.enterLocalPassiveMode();
} catch (IOException e) {
if (ftpClient != null && ftpClient.isConnected())
try {
ftpClient.disconnect();
} catch (IOException e1) {
return false;
}
}
return isConnectionSuccess;
}
关闭连接
public void disConnect() {
if (ftpClient != null && ftpClient.isConnected())
try {
ftpClient.disconnect();
} catch (IOException e1) {
logger
.error("[error]--disConnect exception:"
+ e1.getMessage());
}
}
上传至ftp方法 ftpclient.appendFile(string filename,fileinputstream fileinputstream)
public boolean uploadFile(File file) {
boolean isUpload = false;
InputStream in = null;
if (file != null) {
try {
in = new FileInputStream(file);
isUpload = ftpClient.appendFile(file.getName(), in);
} catch (IOException e) {
return false;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
return false;
}
}
}
} else {
return false;
}
return isUpload;
}
从ftp下载文件
public boolean downloadFile(String remoteFile,String localFile) throws IOException
{
boolean flag=false;
File outfile=new File(localFile);
FileOutputStream out=null;
if(!outfile.exists())
{
String[] names = ftpClient.listNames(remoteFile);
if(names.length!=0)
{
try
{
out=new FileOutputStream(outfile);
flag=ftpClient.retrieveFile(remoteFile, out);
}
catch (IOException e) {
flag = false;
return flag;
} finally {
try {
if (out != null)
{
out.close();
flag=true;
logger.info("Download successfully...");
}
} catch (Exception e) {
e.printStackTrace();
}
finally
{
return flag;
}
}
}
else
{
logger.error("File not found from the FTP...");
flag=false;
return flag;
}
}
else
{
logger.error("The localFile exists,please renamed the localFile!");
flag=false;
return flag;
}
}
请注意ftpclient 的listFiles()和listNames()的区别:
1.listFiles()可以列出目录下所有的文件 返回对象为ftpFile类的数组ftpFile[]。
2.重载:同时在listFiles(string filename)增加参数可以用于查询 当前目录下所有名称包含"filename"的文件,返回同样是ftpFile[]。
3.技巧:可以根据ftpFile[]数组的length来确认文件是否存在。
4.区别:listFiles()和listNames()的区别在于listFiles()返回文件数组ftpFile[],而listNames()返回文件名称数组string[]。
分享到:
相关推荐
org.apache.commons.net.ftp.FTPClient包,不错,在实际项目和产品中使用过。
org.apache.commons.net.ftp.FTPClient FTP工具类,实现上传、下载、压缩到输出流下载等功能
Apache Commons Net » 3.3 org.apache.commons.net.ftp.FTPClient
FTPClient封装了所有必要的功能来存储和检索从FTP服务器上的文件。 这个类负责所有与FTP服务器交互的底层细节,并提供了便捷的更高层次的接口。 正如来自所有类SocketClient ,您必须首先连接到与服务器connect做...
commons-ftp中ftpClient类的API
FTP应用的jar包,主要用于java开发FTP上传下载
commons-ftp中ftpClient类的API.pdf
apache commons scxml 2.0 只有这里才有哦,官网上面只有0.9版本,需要自己下载源码编译,这里我已经编译好了,连同依赖的jar包都在这里了。想知道怎么使用SCXML,可以参考我的博客:...
NULL 博文链接:https://vakinge.iteye.com/blog/487486
赠送原API文档:commons-pool2-2.0-javadoc.jar; 赠送源代码:commons-pool2-2.0-sources.jar; 赠送Maven依赖信息文件:commons-pool2-2.0.pom; 包含翻译后的API文档:commons-pool2-2.0-javadoc-API文档-中文...
标签:asm-commons-2.0.jar.zip,asm,commons,2.0,jar.zip包下载,依赖包
Apache_Commons_Net_2_API.chm
ftpjar 包全,java的commons-net-2.0.jar telnet ftp
赠送原API文档:commons-net-3.3-javadoc.jar; 赠送源代码:commons-net-3.3-sources.jar; 赠送Maven依赖信息文件:commons-net-3.3.pom; 包含翻译后的API文档:commons-net-3.3-javadoc-API文档-中文(简体)版....
sun.net.ftp.FtpClient,it.sauronsoftware.ftp4j.FTPClient,org.apache.commons.net.ftp.FTPClient三种不同的方式操作FTP
commons-net-2.0-javadoc.jar 包
赠送原API文档:commons-net-3.8.0-javadoc.jar; 赠送源代码:commons-net-3.8.0-sources.jar; 赠送Maven依赖信息文件:commons-net-3.8.0.pom; 包含翻译后的API文档:commons-net-3.8.0-javadoc-API文档-中文...
本jar包用于演示如何使用servlet和commons-fileupload-1.2.1.jar和commons-io-2.0.jar两个jar包实现文件上传的功能及源码展示