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

来源:互联网 发布:深圳市云计算产业协会 编辑:程序博客网 时间:2024/04/30 11:40
目前关于文件上传,在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) {} 
} 
原创粉丝点击