设为首页 收藏本站
查看: 1539|回复: 0

[经验分享] 使用J-FTP上传下载(记录)

[复制链接]

尚未签到

发表于 2016-6-9 11:35:10 | 显示全部楼层 |阅读模式
目前关于文件上传,在java的开源世界里有好多工具包,但是基本上都是基于http协议,目前有个关于ftp上传的工具包,在这里我记录下来,为以后的技术做存档

JFTP是一个用JAVA写的FTP客户端程序。功能强大不仅支持FTP,还支持其它协议如SMB, SFTP, NFS, HTTP等。在传输文件的同时还可以浏览FTP服务器上的资源,也可以浏览局域网上的Windows共享资源等
由于项目需要用到ftp的类库,比较了很多觉得JFTP不错,简单实用。
http://www.javaeye.com/topic/116634
上传:

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;
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) {}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-228201-1-1.html 上篇帖子: ANT 远程发布到FTP服务器的方法 下篇帖子: 基于Apache FTP实现的文件上传下载工具
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表