- 浏览: 176829 次
- 性别:
- 来自: 青岛
文章分类
最新评论
-
loveminer:
...
权限控制 -
killerover85:
在企业项目中如何用
weblogic ThreadPool has stuck threads -
just4you:
这排版和背景色看着真蛋疼
spring 安全框架 -
zfms:
-XX:-UseGCOverheadLimit
具体有什么 ...
java.lang.OutOfMemoryError: GC overhead limit exceeded
ftp
最近因工作需要,数据库中的数据需要从FTP服务中抽取数据文件然后校检再抽取到数据中。因为第一步需要从FTP服务中抽取数据文件。第二步采用JDBC批量数据更新。
1。采用Apache.FTPClient:
- /**
- * Apache.FTPClient FTP操作共公类
- *
- * @author 张明学
- *
- */
- public class FTPCommon {
- private FTPClient ftpClient;
- private FTPModel ftpModel;
- public FTPCommon(FTPModel ftp) {
- super();
- // 从配置文件中读取初始化信息
- this.ftpClient = new FTPClient();
- this.ftpModel = ftp;
- }
- /**
- * 连接并登录FTP服务器
- *
- */
- public boolean ftpLogin() {
- boolean isLogin = false;
- FTPClientConfig ftpClientConfig = new FTPClientConfig(
- FTPClientConfig.SYST_NT);
- ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
- this.ftpClient.setControlEncoding("GBK");
- this.ftpClient.configure(ftpClientConfig);
- try {
- if (this.ftpModel.getPort() > 0) {
- this.ftpClient.connect(ftpModel.getUrl(), ftpModel.getPort());
- } else {
- this.ftpClient.connect(ftpModel.getUrl());
- }
- // FTP服务器连接回答
- int reply = this.ftpClient.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- this.ftpClient.disconnect();
- return isLogin;
- }
- this.ftpClient.login(this.ftpModel.getUsername(), this.ftpModel
- .getPassword());
- this.ftpClient.changeWorkingDirectory(this.ftpModel.getRemoteDir());
- this.ftpClient.setFileType(FTPClient.FILE_STRUCTURE);
- LogUtil.infoOutPut("成功登陆FTP服务器:" + this.ftpModel.getUrl() + " 端口号:"
- + this.getFtpModel().getPort() + " 目录:"
- + this.ftpModel.getRemoteDir());
- isLogin = true;
- } catch (SocketException e) {
- e.printStackTrace();
- LogUtil.logPrint("连接FTP服务失败!", Constants.LOG_EXCEPTION);
- LogUtil.logPrint(e.getMessage(), Constants.LOG_EXCEPTION);
- } catch (IOException e) {
- e.printStackTrace();
- LogUtil.logPrint("登录FTP服务失败!", Constants.LOG_EXCEPTION);
- LogUtil.logPrint(e.getMessage(), Constants.LOG_EXCEPTION);
- }
- System.out.println(this.ftpClient.getBufferSize());
- this.ftpClient.setBufferSize(1024 * 2);
- this.ftpClient.setDataTimeout(2000);
- return isLogin;
- }
- /**
- * 退出并关闭FTP连接
- *
- */
- public void close() {
- if (null != this.ftpClient && this.ftpClient.isConnected()) {
- try {
- boolean reuslt = this.ftpClient.logout();// 退出FTP服务器
- if (reuslt) {
- LogUtil.info("退出并关闭FTP服务器的连接");
- }
- } catch (IOException e) {
- e.printStackTrace();
- LogUtil.exception("退出FTP服务器异常!");
- LogUtil.exception(e.getMessage());
- } finally {
- try {
- this.ftpClient.disconnect();// 关闭FTP服务器的连接
- } catch (IOException e) {
- e.printStackTrace();
- LogUtil.exception("关闭FTP服务器的连接异常!");
- LogUtil.exception(e.getMessage());
- }
- }
- }
- }
- /**
- * 检查FTP服务器是否关闭 ,如果关闭接则连接登录FTP
- *
- * @return
- */
- public boolean isOpenFTPConnection() {
- boolean isOpen = false;
- if (null == this.ftpClient) {
- return false;
- }
- try {
- // 没有连接
- if (!this.ftpClient.isConnected()) {
- isOpen = this.ftpLogin();
- }
- } catch (Exception e) {
- e.printStackTrace();
- LogUtil.exception("FTP服务器连接登录异常!");
- LogUtil.exception(e.getMessage());
- isOpen = false;
- }
- return isOpen;
- }
- /**
- * 设置传输文件的类型[文本文件或者二进制文件]
- *
- * @param fileType--FTPClient.BINARY_FILE_TYPE,FTPClient.ASCII_FILE_TYPE
- */
- public void setFileType(int fileType) {
- try {
- this.ftpClient.setFileType(fileType);
- } catch (IOException e) {
- e.printStackTrace();
- LogUtil.exception("设置传输文件的类型异常!");
- LogUtil.exception(e.getMessage());
- }
- }
- /**
- * 下载文件
- *
- * @param localFilePath
- * 本地文件名及路径
- * @param remoteFileName
- * 远程文件名称
- * @return
- */
- public boolean downloadFile(String localFilePath, String remoteFileName) {
- BufferedOutputStream outStream = null;
- boolean success = false;
- try {
- outStream = new BufferedOutputStream(new FileOutputStream(
- localFilePath));
- success = this.ftpClient.retrieveFile(remoteFileName, outStream);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (outStream != null) {
- try {
- outStream.flush();
- outStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return success;
- }
- /**
- * 下载文件
- *
- * @param localFilePath
- * 本地文件
- * @param remoteFileName
- * 远程文件名称
- * @return
- */
- public boolean downloadFile(File localFile, String remoteFileName) {
- BufferedOutputStream outStream = null;
- FileOutputStream outStr = null;
- boolean success = false;
- try {
- outStr = new FileOutputStream(localFile);
- outStream = new BufferedOutputStream(outStr);
- success = this.ftpClient.retrieveFile(remoteFileName, outStream);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (null != outStream) {
- try {
- outStream.flush();
- outStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != outStr) {
- try {
- outStr.flush();
- outStr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- return success;
- }
- /**
- * 上传文件
- *
- * @param localFilePath
- * 本地文件路径及名称
- * @param remoteFileName
- * FTP 服务器文件名称
- * @return
- */
- public boolean uploadFile(String localFilePath, String remoteFileName) {
- BufferedInputStream inStream = null;
- boolean success = false;
- try {
- inStream = new BufferedInputStream(new FileInputStream(
- localFilePath));
- success = this.ftpClient.storeFile(remoteFileName, inStream);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (inStream != null) {
- try {
- inStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return success;
- }
- /**
- * 上传文件
- *
- * @param localFilePath
- * 本地文件
- * @param remoteFileName
- * FTP 服务器文件名称
- * @return
- */
- public boolean uploadFile(File localFile, String remoteFileName) {
- BufferedInputStream inStream = null;
- boolean success = false;
- try {
- inStream = new BufferedInputStream(new FileInputStream(localFile));
- success = this.ftpClient.storeFile(remoteFileName, inStream);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (inStream != null) {
- try {
- inStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return success;
- }
- /**
- * 变更工作目录
- *
- * @param remoteDir--目录路径
- */
- public void changeDir(String remoteDir) {
- try {
- this.ftpClient.changeWorkingDirectory(remoteDir);
- LogUtil.info("变更工作目录为:" + remoteDir);
- } catch (IOException e) {
- e.printStackTrace();
- LogUtil.exception("变更工作目录为:" + remoteDir + "时出错!");
- LogUtil.exception(e.getMessage());
- }
- }
- /**
- * 变更工作目录
- *
- * @param remoteDir--目录路径
- */
- public void changeDir(String[] remoteDirs) {
- String dir = "";
- try {
- for (int i = 0; i < remoteDirs.length; i++) {
- this.ftpClient.changeWorkingDirectory(remoteDirs[i]);
- dir = dir + remoteDirs[i] + "/";
- }
- LogUtil.info("变更工作目录为:" + dir);
- } catch (IOException e) {
- e.printStackTrace();
- LogUtil.exception("变更工作目录为:" + dir + "时出错!");
- LogUtil.exception(e.getMessage());
- }
- }
- /**
- * 返回上级目录
- *
- */
- public void toParentDir(String[] remoteDirs) {
- try {
- for (int i = 0; i < remoteDirs.length; i++) {
- this.ftpClient.changeToParentDirectory();
- }
- LogUtil.info("返回上级目录");
- } catch (IOException e) {
- e.printStackTrace();
- LogUtil.exception("返回上级目录时出错!");
- LogUtil.exception(e.getMessage());
- }
- }
- /**
- * 返回上级目录
- *
- */
- public void toParentDir() {
- try {
- this.ftpClient.changeToParentDirectory();
- LogUtil.info("返回上级目录");
- } catch (IOException e) {
- e.printStackTrace();
- LogUtil.exception("返回上级目录时出错!");
- LogUtil.exception(e.getMessage());
- }
- }
- /**
- * 获得FTP 服务器下所有的文件名列表
- *
- * @param regex
- * @return
- */
- public String[] getListFiels() {
- String files[] = null;
- try {
- files = this.ftpClient.listNames();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return files;
- }
- public FTPClient getFtpClient() {
- return ftpClient;
- }
- public FTPModel getFtpModel() {
- return ftpModel;
- }
- public void setFtpModel(FTPModel ftpModel) {
- this.ftpModel = ftpModel;
- }
- }
2。采用FTP4J:
- /**
- * ftp4j FTP操作共公类
- *
- * @author 张明学
- *
- */
- public class FTP4JCommon {
- FTPClient ftpClient = null;
- FTPModel ftpModel = null;
- public FTP4JCommon() {
- }
- public FTP4JCommon(FTPModel ftpModel) {
- this.ftpModel = ftpModel;
- }
- /**
- * 连接并登录FTP服务器
- *
- */
- public boolean ftpLogin() {
- ftpClient = new FTPClient();
- try {
- // 建立连接
- ftpClient.connect(ftpModel.getUrl());
- ftpClient.setType(FTPClient.TYPE_AUTO);
- ftpClient.setCharset("GBK");
- } catch (Exception e) {
- e.printStackTrace();
- LogUtil.infoOutPut("与FTP服务器建立连接失败!");
- LogUtil.exception(e.getMessage());
- }
- try {
- ftpClient.login(ftpModel.getUsername(), ftpModel.getPassword());
- } catch (Exception e) {
- e.printStackTrace();
- LogUtil.infoOutPut("登录FTP服务器失败!");
- LogUtil.exception(e.getMessage());
- }
- return true;
- }
- /**
- * 退出并关闭FTP连接
- *
- */
- public void close() {
- if (null != ftpClient) {
- try {
- ftpClient.disconnect(true);
- } catch (Exception e) {
- e.printStackTrace();
- LogUtil.infoOutPut("安全退出FTP服务时异常!");
- LogUtil.exception(e.getMessage());
- }
- }
- }
- /**
- * 下载文件
- *
- * @param localFilePath
- * 本地文件名及路径
- * @param remoteFileName
- * 远程文件名称
- * @return
- * @throws Exception
- */
- public void downloadFile(String localFilePath, String remoteFileName)
- throws Exception {
- File localFile = new File(localFilePath);
- try {
- ftpClient.download(remoteFileName, localFile);
- } catch (Exception e) {
- e.printStackTrace();
- LogUtil.infoOutPut("下载" + remoteFileName + "时出现异常!");
- LogUtil.exception(e.getMessage());
- throw e;
- }
- }
- /**
- * 下载文件
- *
- * @param localFilePath
- * 本地文件名及路径
- * @param remoteFileName
- * 远程文件名称
- * @return
- * @throws Exception
- */
- public void downloadFile(File localFile, String remoteFileName)
- throws Exception {
- try {
- ftpClient.download(remoteFileName, localFile);
- } catch (Exception e) {
- e.printStackTrace();
- LogUtil.infoOutPut("下载" + remoteFileName + "时出现异常!");
- LogUtil.exception(e.getMessage());
- throw e;
- }
- }
- /**
- * 获得FTP 服务器下所有的文件名列表
- *
- * @param regex
- * @return
- */
- public String[] getListFiels() {
- String fileNames[] = null;
- try {
- fileNames = this.ftpClient.listNames();
- } catch (Exception e) {
- e.printStackTrace();
- LogUtil.infoOutPut("获取文件名列表时出现异常!");
- LogUtil.exception(e.getMessage());
- }
- return fileNames;
- }
- public FTPModel getFtpModel() {
- return ftpModel;
- }
- public void setFtpModel(FTPModel ftpModel) {
- this.ftpModel = ftpModel;
- }
- public FTPClient getFtpClient() {
- return ftpClient;
- }
- }
3。采用:jftp下载
- /**
- * jftp FTP下载操作
- *
- * @author 张明学
- *
- */
- public class JFTPDownloadCommon implements ConnectionListener {
- private boolean isThere = false;
- public static long time = 0;
- private FTPModel ftpModel = null;
- private ConnectionHandler handler = new ConnectionHandler();
- private FtpConnection ftpcon = null;
- public JFTPDownloadCommon(FTPModel ftpModel) {
- this.ftpModel = ftpModel;
- // 登录FTP
- this.ftpLogin();
- }
- /**
- * 连接并登录FTP服务器
- *
- */
- public boolean ftpLogin() {
- ftpcon = new FtpConnection(this.ftpModel.getUrl());
- ftpcon.addConnectionListener(this);
- ftpcon.setConnectionHandler(handler);
- // 登录
- ftpcon.login(ftpModel.getUsername(), ftpModel.getPassword());
- while (!isThere) {
- try {
- Thread.sleep(10);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- LogUtil.infoOutPut("是否登录成功:" + isThere);
- return isThere;
- }
- /**
- * 关闭与FTP服务器的连接
- *
- */
- public void close() {
- ftpcon.disconnect();
- LogUtil.infoOutPut("关闭与FTP的连接");
- }
- /**
- * 获得FTP 服务器下所有的文件名列表
- *
- * @param regex
- * @return
- */
- public String[] getListFiels() {
- ftpcon.exists("");
- Vector fileNameVector = ftpcon.currentFiles;
- String[] fileNames = new String[fileNameVector.size()];
- int i = 0;
- for (Iterator iter = fileNameVector.iterator(); iter.hasNext();) {
- String name = (String) iter.next();
- fileNames[i] = name;
- i++;
- }
- return fileNames;
- }
- /**
- * 将FTP服务器上的file下载为bype型数据
- *
- * @param remoteFileName
- * 文件名
- * @return
- */
- public byte[] downloadToBinary(String remoteFileName) {
- Settings.bufferSize = 16384;
- long current = System.currentTimeMillis();
- byte[] bytes = null;
- try {
- InputStream is = ftpcon.getDownloadInputStream(remoteFileName);
- 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("下载花费时间:" + time + "ms.");
- return bytes;
- }
- /**
- * 下载FTP服务器文件
- *
- * @param remoteFileName
- * FTP服务器文件名
- * @param localFile
- * 本地文件名
- * @return
- * @throws Exception
- */
- public void downloadToBinary(String remoteFileName, File localFile)
- throws Exception {
- Settings.bufferSize = 16384;
- byte[] bytes = null;
- InputStream is = ftpcon.getDownloadInputStream(remoteFileName);
- ByteArrayOutputStream bais = new ByteArrayOutputStream();
- int bit = 0;
- while ((bit = is.read()) != -1) {
- bais.write(bit);
- }
- bytes = bais.toByteArray();
- CopyByteDataToLoacal(localFile, bytes);
- }
- /**
- * 将二进制文件下载到本地
- *
- * @param localFile
- * 目标文件名
- * @param fileDatas
- * 文件数据
- * @throws IOException
- */
- public void CopyByteDataToLoacal(File localFile, byte[] fileDatas)
- throws IOException {
- FileOutputStream fileOutStream = null;
- BufferedOutputStream bufferOutStream = null;
- try {
- if (localFile.exists()) {
- localFile.delete();
- }
- fileOutStream = new FileOutputStream(localFile);
- bufferOutStream = new BufferedOutputStream(fileOutStream);
- bufferOutStream.write(fileDatas);
- } catch (FileNotFoundException e) {
- LogUtil.exception(e.getMessage());
- throw e;
- } catch (IOException e) {
- LogUtil.exception(e.getMessage());
- throw e;
- } finally {
- try {
- if (null != bufferOutStream) {
- bufferOutStream.flush();
- bufferOutStream.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (null != fileOutStream) {
- try {
- fileOutStream.flush();
- fileOutStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- public void connectionFailed(BasicConnection arg0, String arg1) {
- LogUtil.infoOutPut("与FTP服务器连接失败!");
- }
- public void connectionInitialized(BasicConnection arg0) {
- isThere = true;
- }
- public void updateRemoteDirectory(BasicConnection arg0) {
- }
- public void updateProgress(String arg0, String arg1, long arg2) {
- }
- public void actionFinished(BasicConnection arg0) {
- }
- }
上面都用到了一个ftpModel如下(get,set方法省了):
- /**
- * FTP实体对象
- *
- * @author 张明学
- *
- */
- public class FTPModel {
- private String ftpId;
- private String username;
- private String password;
- private String url;
- private int port;
- private String remoteDir;
- public FTPModel() {
- }
- public FTPModel(String username, String password, String url, int port,
- String remoteDir) {
- this.username = username;
- this.password = password;
- this.url = url;
- this.port = port;
- this.remoteDir = remoteDir;
- }
- }
上述仅仅列出了一点常用的操作,更多的操作需要参看它的们API文件。
还有,我采用的Serv-U作为FTP服务器,按装之后新建用户不法登录。后来找到了原因是:要把IIS服务中的FTP服务关掉。
相关推荐
详解ftp创建文件权限问题 一、问题 有一个这样的需求,admin为一个Linux为其FTP应用创建的一个有权限限制的用户,通过admin用户可以进行登录FTP服务,登录FTP服务后,创建文件夹,该文件夹的用户和用户组都是admin,...
如果用本FTP客户端登录某个FTP服务器,则先必须申请一个FTP空间,申请FTP空间时,它会给你一个FTP主机地址、用户名称、用户密码。 测试步骤: 1、项目编译并运行。 2、申请一个FTP空间(这里不讨论)。 3、输入FTP...
FTP服务器FTP服务器FTP服务器FTP服务器FTP服务器FTP服务器FTP服务器FTP服务器
首先需要取消“站点属性/允许匿名连接”,如图1。 接着进入“计算机管理/本地用户和组...回到IIS管理器,选择一个FTP站点“新建/虚拟目录/下一步”在别名处输入FTP01,选择“下一步”,路径选择刚刚创建的FTP01目
FTP 弱口令或匿名登录漏洞,一般指使用 FTP 的用户启用了匿名登录功能,或系统口令的长度太短、复杂度不够、仅包含数字、或仅包含字母等,容易被黑客攻击,发生恶意文件上传或更严重的入侵行为。 漏洞危害 黑客利用...
ftp是liunx下常用的软件之一。 1.将用户切换到root:su - ; 2.检查机器是否已经安装了ftp:rpm -qa|grep vsftpd; 如果没有返回任何结果,表示没有安装;如果返回文件包名,这表示已经安装了该服务; 3.开始安装:...
密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip...
ftp4j是个很年轻的开源项目,但是试用后发现很好很强大,如果你找一个纯java的FTP库,要支持socks4,socks4a,socks5,http代理,就是他了! 比apache的FTPClient(不支持代理)、半商业的edtFTPj(PRO支持代理,...
市面上比较常见的FTP搭建工具都过于专业,例如IIS的FTP功能,SERV-U软件等。虽然使用这些软件建立的FTP在功能上非常强大,但却存在两个明显不足,第一是FTP服务器初始工作比较繁琐,需要具备一定的计算机水平才能...
目前网络上有很多专业的FTP服务器软件,但是基于C#开发的版本很少,功能很全的源码就更少了,为此我专门将该资源分享出来,希望能给真正需要的读者提供微薄的帮助。 本软件简介: 1.具备FTP服务器的基本功能,如文件...
采用apache commons开发包,实现了FTP多... 另外要注意的是,输入的FTP目录和本地目录要正确,因为程序中没有处理这类异常(呵呵呵,毕竟这不是主要目的),范例 FTP目录:\FTP\hello.txt(\代表FTP根目录) 本地目录:C:\
webftp.zip ftp源码 webftp.zip ftp源码 webftp.zip ftp源码 webftp.zip ftp源码 webftp.zip ftp源码
Serv-U是一种被广泛运用的FTP服务器端软件,支持3x/9x/ME/NT/2K等全Windows系列。可以设定多个FTP服务器、限定登录用户的权限、登录主目录及空间大小等,功能非常完备。 它具有非常完备的安全特性,支持SSl FTP传输...
而文件传送协议FTP是因特网上使用得最广泛的文件传送协议。FTP使用客户服务器方式。 设计要求: 1) 以命令行形式运行 2) 该FTP客户端程序具有以下基本功能:能完成FTP链接的打开和关闭操作;能执行文件的上传和...
ftp检测\ftp扫描
FtpC是用vs2010写的一个简单的FTP客户端,说明CFtpConnection的基本用法,大家可参照它写出长期自动获取远程FTP服务器上数据的应用。如果你用vc6.0,只要把这个工程中的原理性代码复制过去就行了。 测试时FTP服务器...
ftp上传下载 java FTPClient - - ITeye技术网站 (2012年5月21日) FTP操作类:FTPClient - 石川 - 博客园 (2012年5月21日) JAVA中使用FTPClient上传下载 - hbcui1984的专栏 - 博客频道 - CSDN.NET (2012年5月21日) ...
当出现:除非Microsoft FTP 服务(FTPSVC)正在运行,否则无法启动FTP站点。服务目前已停止。(针对win10系统,亲测成功。其他系统应该差不多) 即如下提示时: 原因是:在自己电脑上搭建的ftp服务器,每次断电开机...
用java语言编写的ftp小工具,可以按指定时间监控ftp服务器,把服务器指定目录内新产生的文件或者文件夹下载到本地指定文件夹,下载后删除数据。 也可以监控本地文件夹,把文件夹内新产生的文件或者文件夹整体上传到...
FTP服务器单文件绿色版FTPServer