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

[经验分享] 公用ftp工具组件

[复制链接]

尚未签到

发表于 2016-6-8 10:46:16 | 显示全部楼层 |阅读模式
  代码:
  FtpTool

package com.wirelesscity.tools.ftp;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import com.wirelesscity.common.StringUtil;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
@SuppressWarnings("restriction")
public class FtpTool {
private FtpClient ftpclient;
private String ipAddress; // FTP地址
private int ipPort; // 端口
private String userName; // 用户名
private String PassWord; // 密码
/**
* 初始化参数FTPTOOL对象
* @param ipAddress
* @param ipPort
* @param userName
* @param PassWord
* @throws IOException
*/
public FtpTool(String ip, int port, String username, String password)
throws IOException {
ipAddress = new String(ip);
ipPort = port;
if (StringUtil.isEmpty(String.valueOf(port))) {
port = 21;
}
ftpclient = new FtpClient(ipAddress, ipPort);
userName = new String(username);
PassWord = new String(password);
}
/**
* 登录FTP服务器
*
* @throws Exception
*/
public void login() throws Exception {
ftpclient.login(userName, PassWord);
}
/**
* 退出FTP服务器
*
* @throws Exception
*/
public void logout() throws Exception {
// 用ftpclient.closeServer()断开FTP出错时用下更语句退出
ftpclient.sendServer("QUIT\r\n");
int reply = ftpclient.readServerResponse(); // 取得服务器的返回信息
System.out.println("reply:" + reply);
}
/**
* 在FTP服务器上建立指定的目录,当目录已经存在的情下不会影响目录下的文件,这样用以判断FTP
* 上传文件时保证目录的存在目录格式必须以"/"根目录开头
*
* @param pathList
*            String
* @throws Exception
*/
public void buildList(String pathList) throws Exception {
ftpclient.ascii();
StringTokenizer s = new StringTokenizer(pathList, "/"); // sign
int count = s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpclient.sendServer("XMKD " + pathName + "\r\n");
} catch (Exception e) {
e = null;
}
int reply = ftpclient.readServerResponse();
}
ftpclient.binary();
}
/**
* 取得指定目录下的所有文件名,不包括目录名称 分析nameList得到的输入流中的数,得到指定目录下的所有文件名
*
* @param fullPath
*            String
* @return ArrayList
* @throws Exception
*/
public ArrayList fileNames(String fullPath) throws Exception {
ftpclient.ascii(); // 注意,使用字符模式
TelnetInputStream list = ftpclient.nameList(fullPath);
byte[] names = new byte[2048];
int bufsize = 0;
bufsize = list.read(names, 0, names.length); // 从流中读取
list.close();
ArrayList namesList = new ArrayList();
int i = 0;
int j = 0;
while (i < bufsize) {
if (names == 10) { // 字符模式为10,二进制模式为13
// 文件名在数据中开始下标为j,i-j为文件名的长度,文件名在数据中的结束下标为i-1
String tempName = new String(names, j, i - j);
namesList.add(tempName);
j = i + 1; // 上一次位置字符模式
}
i = i + 1;
}
return namesList;
}
/**
* 上传文件到FTP服务器,destination路径以FTP服务器的"/"开始,带文件名、 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖
*
* @param source
*            String
* @param destination
*            String
* @throws Exception
*/
public void upFile(String source, String destination) throws Exception {
buildList(destination.substring(0, destination.lastIndexOf("/")));
ftpclient.binary(); // 此行代码必须放在buildList之后
TelnetOutputStream ftpOut = ftpclient.put(destination);
TelnetInputStream ftpIn = new TelnetInputStream(new FileInputStream(
source), true);
byte[] buf = new byte[204800];
int bufsize = 0;
while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
ftpOut.write(buf, 0, bufsize);
}
ftpIn.close();
ftpOut.close();
}
/**
* JSP中的流上传到FTP服务器, 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖 字节数组做为文件的输入流,此方法适用于JSP中通过
* request输入流来直接上传文件在RequestUpload类中调用了此方法, destination路径以FTP服务器的"/"开始,带文件名
*
* @param sourceData
*            byte[]
* @param destination
*            String
* @throws Exception
*/
public void upFile(byte[] sourceData, String destination) throws Exception {
buildList(destination.substring(0, destination.lastIndexOf("/")));
ftpclient.binary(); // 此行代码必须放在buildList之后
TelnetOutputStream ftpOut = ftpclient.put(destination);
ftpOut.write(sourceData, 0, sourceData.length);
// ftpOut.flush();
ftpOut.close();
}
/**
* 从FTP文件服务器上下载文件SourceFileName,到本地destinationFileName 所有的文件名中都要求包括完整的路径名在内
*
* @param SourceFileName
*            String
* @param destinationFileName
*            String
* @throws Exception
*/
public void downFile(String SourceFileName, String destinationFileName)
throws Exception {
ftpclient.binary(); // 一定要使用二进制模式
TelnetInputStream ftpIn = ftpclient.get(SourceFileName);
byte[] buf = new byte[204800];
int bufsize = 0;
FileOutputStream ftpOut = new FileOutputStream(destinationFileName);
while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
ftpOut.write(buf, 0, bufsize);
}
ftpOut.close();
ftpIn.close();
}
/**
* 从FTP文件服务器上下载文件,输出到字节数组中
*
* @param SourceFileName
*            String
* @return byte[]
* @throws Exception
*/
public byte[] downFile(String SourceFileName) throws Exception {
ftpclient.binary(); // 一定要使用二进制模式
TelnetInputStream ftpIn = ftpclient.get(SourceFileName);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
byte[] buf = new byte[204800];
int bufsize = 0;
while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
byteOut.write(buf, 0, bufsize);
}
byte[] return_arraybyte = byteOut.toByteArray();
byteOut.close();
ftpIn.close();
return return_arraybyte;
}
}
FtpServiceImpl  

package com.wirelesscity.tools.ftp;
import java.io.IOException;
public class FtpServiceImpl implements FtpService{
private String ftpIpAddress;
private int ftpPort;
private String ftpUserName;
private String ftpPassword;
//FTP路径配置
private String ftpLocalPathRoot;
private String ftpRemotePathRoot;
public String getFtpLocalPathRoot() {
return ftpLocalPathRoot;
}
public void setFtpLocalPathRoot(String ftpLocalPathRoot) {
this.ftpLocalPathRoot = ftpLocalPathRoot;
}
public String getFtpRemotePathRoot() {
return ftpRemotePathRoot;
}
public void setFtpRemotePathRoot(String ftpRemotePathRoot) {
this.ftpRemotePathRoot = ftpRemotePathRoot;
}
public void setFtpIpAddress(String ftpIpAddress) {
this.ftpIpAddress = ftpIpAddress;
}
public void setFtpPort(int ftpPort) {
this.ftpPort = ftpPort;
}
public void setFtpUserName(String ftpUserName) {
this.ftpUserName = ftpUserName;
}
public void setFtpPassword(String ftpPassword) {
this.ftpPassword = ftpPassword;
}
public String getFtpIpAddress() {
return ftpIpAddress;
}
public int getFtpPort() {
return ftpPort;
}
public String getFtpUserName() {
return ftpUserName;
}
public String getFtpPassword() {
return ftpPassword;
}
public void testUpload(){
// TODO Auto-generated method stub
try {
FtpTool ftpTool = new FtpTool(this.getFtpIpAddress(),
this.getFtpPort(), this.getFtpUserName(),
this.getFtpPassword());
ftpTool.login();
ftpTool.buildList("file/user/tttt");
String source = "D:/config.rar";
String destination = "/file/user/tttt/test.jar";
ftpTool.upFile(source, destination);
ftpTool.logout();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 建立文件夹路径
* @param pathList
* @throws Exception
*/
public void buildList(String pathList) throws Exception{
FtpTool ftpTool = new FtpTool(this.getFtpIpAddress(),
this.getFtpPort(), this.getFtpUserName(),
this.getFtpPassword());
ftpTool.login();
ftpTool.buildList(pathList);
ftpTool.logout();
}
/**
* 上传文件到FTP服务器,
* destination路径以FTP服务器的"/"开始,
* 带文件名、 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖
* @param source
*            String
* @param destination
*            String
* @throws Exception
*/
public void upFile(String source, String destination) throws Exception{
FtpTool ftpTool = new FtpTool(this.getFtpIpAddress(),
this.getFtpPort(), this.getFtpUserName(),
this.getFtpPassword());
ftpTool.login();
ftpTool.upFile(source, destination);
ftpTool.logout();
}
/**
* JSP中的流上传到FTP服务器, 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖 字节数组做为文件的输入流,此
* 方法适用于JSP中通过
* request输入流来直接上传文件在RequestUpload类中调用了此方法,
* destination路径以FTP服务器的"/"开始,带文件名
* @param sourceData
*            byte[]
* @param destination
*            String
* @throws Exception
*/
public void upFile(byte[] sourceData, String destination) throws Exception {
FtpTool ftpTool = new FtpTool(this.getFtpIpAddress(),
this.getFtpPort(), this.getFtpUserName(),
this.getFtpPassword());
ftpTool.login();
ftpTool.upFile(sourceData, destination);
ftpTool.logout();
}

/**
* @param args
*/
public static void main(String[] args) {
FtpServiceImpl ftpUtil=new FtpServiceImpl();
ftpUtil.testUpload();
}
}
FtpService:  

package com.wirelesscity.tools.ftp;
public interface FtpService{
public String getFtpLocalPathRoot() ;
public String getFtpRemotePathRoot();
public void testUpload();
/**
* 建立文件夹路径
* @param pathList
* @throws Exception
*/
public void buildList(String pathList) throws Exception;
/**
* 上传文件到FTP服务器,
* destination路径以FTP服务器的"/"开始,
* 带文件名、 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖
* @param source
*            String
* @param destination
*            String
* @throws Exception
*/
public void upFile(String source, String destination) throws Exception;
/**
* JSP中的流上传到FTP服务器, 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖 字节数组做为文件的输入流,此
* 方法适用于JSP中通过
* request输入流来直接上传文件在RequestUpload类中调用了此方法,
* destination路径以FTP服务器的"/"开始,带文件名
* @param sourceData
*            byte[]
* @param destination
*            String
* @throws Exception
*/
public void upFile(byte[] sourceData, String destination) throws Exception ;

}


  

运维网声明 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-227770-1-1.html 上篇帖子: ftp vsftpd详细配置 下篇帖子: android传送照片到FTP服务器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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