使用J-FTP上传下载(记录)

目前关于文件上传,在java的开源世界里有好多工具包,但是基本上都是基于http协议,目前有个关于ftp上传的工具包,在这里我记录下来,为以后的技术做存档
JFTP是一个用JAVA写的FTP客户端程序。功能强大不仅支持FTP,还支持其它协议如SMB, SFTP, NFS, HTTP等。在传输文件的同时还可以浏览FTP服务器上的资源,也可以浏览局域网上的Windows共享资源等
由于项目需要用到ftp的类库,比较了很多觉得JFTP不错,简单实用。
上传:
java 代码

import net.sf.jftp.net.ConnectionHandler;  
import net.sf.jftp.net.ConnectionListener;  
import net.sf.jftp.net.DataConnection;  
import net.sf.jftp.net.FtpConnection;  
import net.sf.jftp.net.BasicConnection;  
import net.sf.jftp.config.Settings;  

import java.io.*;  

import org.apache.commons.lang.StringUtils;  

/**  
* See FtpDownload.java for comments.  
*/  
public class FtpUpload implements ConnectionListener  
{  

private boolean isThere = false;  

private ConnectionHandler handler = new ConnectionHandler();  

private String host;  
private int port = 21;  
private String user;  
private String passwd;  

public FtpUpload(String host, String user, String passwd){  
this.host = host;  
this.user = user;  
this.passwd = passwd;  
}  

public FtpUpload(String host, int port, String user, String passwd){  
this.host = host;  
this.port = port;  
this.user = user;  
this.passwd = passwd;  
}  

public int upload(String dir, String file){  
FtpConnection con = new FtpConnection(host, port, "/");  

con.addConnectionListener(this);  

con.setConnectionHandler(handler);  

con.login(user, passwd);  

while(!isThere)  
{  
try { Thread.sleep(10); }  
catch(Exception ex) { ex.printStackTrace(); }  
}  

//make dirs  
String path = "";  
String[] paths = StringUtils.split(dir, "/");  
for(int i = 0; i < paths.length; i++){  
path += "/" + paths[i];  
if(!con.chdir(path)){ con.mkdir(path); }  
}  
con.chdir(dir);  
return con.upload(file);  
}  
public static void main(String argv[])  
{  
if(argv.length == 3)  
{  
FtpUpload f = new FtpUpload(argv[0], argv[2], argv[1]);  
}  
else  
{  
FtpUpload g =  
new FtpUpload("192.168.1.10", 2009, "test","test");  
g.upload("/", "C:/test.jpg");  
}  
}  


public void updateRemoteDirectory(BasicConnection con)  
{  
System.out.println("new path is: " + con.getPWD());  
}  

public void connectionInitialized(BasicConnection con)  
{  
isThere = true;  
}  

public void updateProgress(String file, String type, long bytes) {}  

public void connectionFailed(BasicConnection con, String why) {System.out.println("connection failed!");}  

public void actionFinished(BasicConnection con) {}  
}

下载:
java 代码

import net.sf.jftp.net.ConnectionHandler;  
import net.sf.jftp.net.ConnectionListener;  
import net.sf.jftp.net.DataConnection;  
import net.sf.jftp.net.FtpConnection;  
import net.sf.jftp.net.BasicConnection;  
import net.sf.jftp.config.Settings;  

import java.io.*;  

public class FtpDownload implements ConnectionListener  
{  
// is the connection established?  
private boolean isThere = false;  

public static long time = 0;  

// connection pool, not necessary but you should take a look at this class  
// if you want to use multiple event based ftp transfers.  
private ConnectionHandler handler = new ConnectionHandler();  

private String host;  
private int port = 21;  
private String user;  
private String passwd;  

public FtpDownload(String host, int port, String user, String passwd){  
this.host = host;  
this.port = port;  
this.user = user;  
this.passwd = passwd;  
}  

//creates a FtpConnection and downloads a file  
public byte[] downloadToBinary(String file)  
{  
// the ftp client default is very small, you may want to increase this  
Settings.bufferSize = 16384;  

long current = System.currentTimeMillis();  
//System.out.println("1) "+(System.currentTimeMillis()-current)+"ms.");  

// create a FtpConnection - note that it does *not* connect instantly  
FtpConnection con = new FtpConnection(host);  

// set updatelistener, interface methods are below  
con.addConnectionListener(this);  

// set handler  
con.setConnectionHandler(handler);  

// connect and login. this is from where connectionFailed() may be called for example  
con.login(user, passwd);  

// login calls connectionInitialized() below which sets isThere to true  
while(!isThere)  
{  
try { Thread.sleep(10); }  
catch(Exception ex) { ex.printStackTrace(); }  
}  

// get download input stream  
byte[] bytes = null;  
try{  
InputStream is = con.getDownloadInputStream(file);  
ByteArrayOutputStream bais = new ByteArrayOutputStream();  
int bit = 0;  
while((bit = is.read()) != -1){  
bais.write(bit);  
}  
bytes = bais.toByteArray();  
}catch(Exception e){}  
time = (System.currentTimeMillis()-current);  

System.out.println("Download took "+time+"ms.");  

return bytes;  
}  

// download welcome.msg from sourceforge or any other given file  
public static void main(String argv[])  
{  
FtpDownload f = new FtpDownload("192.168.1.10", 2009, "test","test");  
byte[] bs = f.downloadToBinary("/aaa.jpg");  
}  

// ------------------ needed by ConnectionListener interface -----------------  

// called if the remote directory has changed  
public void updateRemoteDirectory(BasicConnection con)  
{  
System.out.println("new path is: " + con.getPWD());  
}  

// called if a connection has been established  
public void connectionInitialized(BasicConnection con)  
{  
isThere = true;  
}  

// called every few kb by DataConnection during the trnsfer (interval can be changed in Settings)  
public void updateProgress(String file, String type, long bytes) {}  

// called if connection fails  
public void connectionFailed(BasicConnection con, String why) {System.out.println("connection failed!");}  

// up- or download has finished  
public void actionFinished(BasicConnection con) {}  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷月宫主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值