sun.net.ftp.FtpClient 上传、下载简单实例

2 篇文章 0 订阅
sun.net.ftp.FtpClient 可以帮助我们进行一些简单的ftp客户端功能:下载、上传文件。
但如遇到创建目录之类的就无能为力了,我们只好利用第三方源码,比如 com.enterprisedt.net.ftp.FTPClient

下面写一些sun.net.ftp.FtpClient 的使用方法:

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;

/**
* 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("172.11.11.11", "wdd", "1", "ftp_file");
//上传文件到 ftp_file 文件夹下 注:server-u 上根目录是ftp,则ftp_file
是ftp的子目录,若连接到根目录则此参数为空
System.out.println("filesize:"+ftp.upload("f:/abc.doc")+"字节");
/** 取得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:/ftp_down/"+filename);
}
} catch (Exception e) {
///
}finally
{
ftp.closeServer();
}
}
}


ftp服务器为:serv-u , 客户端:FlashFXP
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 sun.net.ftp.ftpclient 工具类进行文件上传需要以下步骤: 1. 建立 FTP 连接:使用 `FtpClient` 类的 `openServer` 方法建立连接,需要传入 FTP 服务器的地址和端口号。 2. 登录 FTP 服务器:使用 `FtpClient` 类的 `login` 方法登录 FTP 服务器,需要传入 FTP 服务器的用户名和密码。 3. 切换到上传目录:使用 `FtpClient` 类的 `cd` 方法切换到上传目录。 4. 设置上传模式:使用 `FtpClient` 类的 `setBinaryMode` 方法设置上传模式为二进制模式。 5. 上传文件:使用 `FtpClient` 类的 `put` 方法上传文件,需要传入本地文件的路径和上传后的文件名。 6. 关闭连接:使用 `FtpClient` 类的 `closeServer` 方法关闭连接。 以下是一个简单上传文件的示例代码: ```java import sun.net.ftp.FtpClient; import java.io.FileInputStream; public class FtpUploader { public static void main(String[] args) throws Exception { String server = "ftp.example.com"; int port = 21; String username = "username"; String password = "password"; String localFilePath = "/path/to/local/file.txt"; String remoteFileName = "file.txt"; FtpClient ftpClient = new FtpClient(server, port); ftpClient.login(username, password); ftpClient.cd("/upload"); ftpClient.setBinaryMode(); FileInputStream fis = new FileInputStream(localFilePath); ftpClient.put(fis, remoteFileName); fis.close(); ftpClient.closeServer(); } } ``` 注意:`sun.net.ftp.FtpClient` 类是 Sun JDK 内部使用的类,不是公开的 API,因此可能会在不同版本的 JDK 中存在变化。建议使用第三方 FTP 客户端库,如 Apache Commons Net,来进行 FTP 操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值