某些数据交换,我们需要通过ftp来完成。
sun.net.ftp.FtpClient 可以帮助我们进行一些简单的ftp客户端功能:下载、上传文件。
但如遇到创建目录之类的就无能为力了,我们只好利用第三方源码,比如 com.enterprisedt.net.ftp.FTPClient
下面写一些sun.net.ftp.FtpClient 的使用方法。
1、引入包
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import sun.net.*;
import sun.net.ftp.FtpClient;
2、我们建一个叫做FtpUtil的class
/**
* connectServer
* 连接ftp服务器
* @throws java.io.IOException
* @param path 文件夹,空代表根目录
* @param password 密码
* @param user 登陆用户
* @param server 服务器地址
*/
public void connectServer(String server, String user, String password, String path)
throws IOException
{
// server:FTP服务器的IP地址;user:登录FTP服务器的用户名
// password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径
ftpClient = new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
//path是ftp服务下主目录的子目录
if (path.length() != 0) ftpClient.cd(path);
//用2进制上传、下载
ftpClient.binary();
}
/**
* upload
* 上传文件
* @throws java.lang.Exception
* @return -1 文件不存在
* -2 文件内容为空
* >0 成功上传,返回文件的大小
* @param newname 上传后的新文件名
* @param filename 上传的文件
*/
public long upload(String filename,String newname) throws Exception
{
long result = 0;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
java.io.File file_in = new java.io.File(filename);
if (!file_in.exists()) return -1;
if (file_in.length()==0) return -2;
os = ftpClient.put(newname);
result = file_in.length();
is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
return result;
}
/**
* upload
* @throws java.lang.Exception
* @return
* @param filename
*/
public long upload(String filename)
throws Exception
{
String newname = "";
if (filename.indexOf("/")>-1)
{
newname = filename.substring(filename.lastIndexOf("/")+1);
}else
{
newname = filename;
}
return upload(filename,newname);
}
/**
* download
* 从ftp下载文件到本地
* @throws java.lang.Exception
* @return
* @param newfilename 本地生成的文件名
* @param filename 服务器上的文件名
*/
public long download(String filename,String newfilename)
throws Exception
{
long result = 0;
TelnetInputStream is = null;
FileOutputStream os = null;
try
{
is = ftpClient.get(filename);
java.io.File outfile = new java.io.File(newfilename);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
result = result + c;
}
} catch (IOException e)
{
e.printStackTrace();
}
finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
return result;
}
/**
* 取得某个目录下的所有文件列表
*
*/
public List getFileList(String path)
{
List list = new ArrayList();
try
{
DataInputStream dis = new DataInputStream(ftpClient.nameList(path));
String filename = "";
while((filename=dis.readLine())!=null)
{
list.add(filename);
}
} catch (Exception e)
{
e.printStackTrace();
}
return list;
}
/**
* closeServer
* 断开与ftp服务器的链接
* @throws java.io.IOException
*/
public void closeServer()
throws IOException
{
try
{
if (ftpClient != null)
{
ftpClient.closeServer();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String [] args) throws Exception
{
FtpUtil ftp = new FtpUtil();
try {
//连接ftp服务器
ftp.connectServer("10.163.7.15", "cxl", "1", "info2");
/** 上传文件到 info2 文件夹下 */
System.out.println("filesize:"+ftp.upload("f:/download/Install.exe")+"字节");
/** 取得info2文件夹下的所有文件列表,并下载到 E盘下 */
List list = ftp.getFileList(".");
for (int i=0;i<list.size();i++)
{
String filename = (String)list.get(i);
System.out.println(filename);
ftp.download(filename,"E:/"+filename);
}
} catch (Exception e) {
///
}finally
{
ftp.closeServer();
}
}
}
以上就是 使用 sun.net.ftp.FtpClient 操作ftp客户端的使用方法。
分享到:
相关推荐
NULL 博文链接:https://starbhhc.iteye.com/blog/2315455
sun.net.ftp.FtpClient,it.sauronsoftware.ftp4j.FTPClient,org.apache.commons.net.ftp.FTPClient三种不同的方式操作FTP
org.apache.commons.net.ftp.FTPClient包,不错,在实际项目和产品中使用过。
com.enterprisedt.net.ftp.FTPClient的jar包源代码以及文档 版本:edtftpj.2.2.2 2011-2-27
编译后可在bin下面生成System.Net.FtpClient.dll,用于C#在ftp上传时调用。
FTP应用的jar包,主要用于java开发FTP上传下载
C#的FTP操作类库,支持FTP文件的上传,下载等等,封装好的可直接引用FtpClient; 如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用。 那就是System...
com.enterprisedt.net.ftp.FTPClient类库的jar包,可以用来实现对FTP服务器端的处理。
NULL 博文链接:https://vakinge.iteye.com/blog/487486
org.apache.commons.net.ftp.FTPClient FTP工具类,实现上传、下载、压缩到输出流下载等功能
.net.ftp.FTPClient jar ,exaple, commons-net-2.0.jar
com.enterprisedt.net.ftp.FTPClient类库的jar包 edtFTPj-1.5.3.jar
Apache Commons Net » 3.3 org.apache.commons.net.ftp.FTPClient
org.apache.commons.net.ftp的官方完整jar包 放心使用
上传了收集的最新的 commons-io-2.4.jar 包 和 commons-net-3.3.jar 以及 FTP java调用例子。net 包是一个用于操作Internet基础协议(Finger,Whois,TFTP,Telnet,POP3,FTP,NNTP,以及SMTP)的底层API。Net包...
因为开发一个项目要用到FTP上传功能,用的是.NET平台,当时找了半天也没有关于FTP...找了很多资料,并参考了一下Java的sun.net.FtpClient类,总算可以拿出来用,所以就迫不及待的传上来给大家共享,相对性能方面还可以。
System.Net.FtpClient.Async System.Net.FtpClient 的异步扩展
这是一个FTP客户端的简单实现,使用语言为java,FTP服务器为FileZilla Server。该客户端软件使用的是org.apache.commons.net.ftp.FTPClient这个类,FTPClient这个类,该类具体的可查看官网的api文档,链接:...