`
zxw882011_98
  • 浏览: 10308 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

java上传文件到ftp服务器

阅读更多
    package brips.com.yxjx.pub; 
     
    import java.io.BufferedInputStream; 
    import java.io.BufferedOutputStream; 
    import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.text.SimpleDateFormat; 
    import java.util.ArrayList; 
    import java.util.HashMap; 
    import java.util.List; 
    import java.util.Map; 
    import java.util.Properties; 
     
    import org.apache.commons.io.FileUtils; 
    import org.apache.commons.net.ftp.FTP; 
    import org.apache.commons.net.ftp.FTPClient; 
    import org.apache.commons.net.ftp.FTPClientConfig; 
    import org.apache.commons.net.ftp.FTPFile; 
    import org.apache.commons.net.ftp.FTPReply; 
     
    public class FTPHandle { 
        private String username; 
     
        private String password; 
     
        private String ip; 
     
        private int port; 
     
        private Properties property = null;// 配置 
     
        private String configFile;// 配置文件的路径名 
     
        private FTPClient ftpClient = null; 
     
        private String filedir = "";// FTP文件路径 
     
        private final String[] FILE_TYPES = { "文件", "目录", "符号链接", "未知类型" }; 
         
        private static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm"); 
     
        /**
         * 设置参数
         * 
         * @param configFile
         *            --参数的配置文件
         */ 
        private void setArg(String configFile) { 
            property = new Properties(); 
            BufferedInputStream inBuff = null; 
            try { 
                inBuff = new BufferedInputStream(this.getClass().getResourceAsStream(configFile)); 
                property.load(inBuff); 
                username = property.getProperty("username"); 
                password = property.getProperty("password"); 
                ip = property.getProperty("ip"); 
                port = Integer.parseInt(property.getProperty("port")); 
                filedir = property.getProperty("filedir"); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } finally { 
                try { 
                    if (inBuff != null) 
                        inBuff.close(); 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                } 
            } 
        } 
     
        /**
         * 设置FTP客服端的配置--一般可以不设置
         * 
         * @return
         */ 
        private FTPClientConfig getFtpConfig() { 
            FTPClientConfig ftpConfig = new FTPClientConfig( 
                    FTPClientConfig.SYST_UNIX); 
            ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING); 
            return ftpConfig; 
        } 
     
        /**
         * 连接到服务器
         */ 
        public void connectServer() { 
            if (ftpClient == null) { 
                int reply; 
                try { 
                    setArg(configFile); 
                    ftpClient = new FTPClient(); 
    //              ftpClient.configure(getFtpConfig()); 
                    ftpClient.connect(ip); 
                    ftpClient.login(username, password); 
                    ftpClient.setDefaultPort(port); 
                    System.out.print(ftpClient.getReplyString()); 
                    reply = ftpClient.getReplyCode(); 
     
                    if (!FTPReply.isPositiveCompletion(reply)) { 
                        ftpClient.disconnect(); 
                        System.err.println("FTP server refused connection."); 
                    } 
                } catch (Exception e) { 
                    System.err.println("登录ftp服务器【" + ip + "】失败"); 
                    e.printStackTrace(); 
                } 
            } 
        } 
     
        /**
         * 进入到服务器的某个目录下
         * 
         * @param directory
         */ 
        public void changeWorkingDirectory() { 
            try { 
                ftpClient.changeWorkingDirectory(filedir); 
            } catch (IOException ioe) { 
                ioe.printStackTrace(); 
            } 
        } 
     
        /**
         * 上传文件
         * 
         * @param inputStream--文件输入流
         * @param newFileName--新的文件名
         */ 
        public void uploadFile(InputStream inputStream, String newFileName) { 
            changeWorkingDirectory();// 进入文件夹 
            // 上传文件 
            BufferedInputStream buffIn = null; 
            try { 
                buffIn = new BufferedInputStream(inputStream); 
                ftpClient.storeFile(newFileName, buffIn); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } finally { 
                try { 
                    if (buffIn != null) 
                        buffIn.close(); 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                } 
            } 
        } 
     
        /**
         * 列出服务器上文件和目录
         * 
         * @param regStr
         *            --匹配的正则表达式
         */ 
        @SuppressWarnings("unchecked") 
        public List listRemoteFiles(String regStr) { 
            List list = new ArrayList(); 
            try { 
                FTPFile[] files = ftpClient.listFiles(regStr); 
                if (files == null || files.length == 0) { 
                    System.out.println("There has not any file!"); 
                    return null; 
                } else { 
                    for (FTPFile file : files) { 
                        if (file != null) { 
                            Map map = new HashMap(); 
                            String filename = file.getName(); 
                            int filenamelen = filename.length(); 
                            if(filenamelen>4){ 
                                String filetype = filename.substring(filenamelen-3); 
                                if("txt".equals(filetype)){ 
                                    String name = file.getName(); 
                                    name = name.substring(0,name.length()-4); 
                                    map.put("filename", name); 
                                    map.put("filesize", FileUtils.byteCountToDisplaySize(file.getSize())); 
                                    map.put("scsj", dateFormat.format(file.getTimestamp().getTime())); 
                                    list.add(map); 
                                } 
                            } 
                        } 
                    } 
                } 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
            return list; 
        } 
        /**
         * 下载文件
         * @param remoteFileName --服务器上的文件名
         * @param localFileName--本地文件名
         */ 
        public void loadFile(String remoteFileName,String localFileName){ 
            //下载文件 
            BufferedOutputStream buffOut=null; 
            try{ 
                buffOut=new BufferedOutputStream(new FileOutputStream(localFileName)); 
                ftpClient.retrieveFile(remoteFileName, buffOut); 
            }catch(Exception e){ 
                e.printStackTrace(); 
            }finally{ 
                try{ 
                    if(buffOut!=null) 
                        buffOut.close(); 
                }catch(Exception e){ 
                    e.printStackTrace(); 
                } 
            } 
        } 
        /**
         * 删除文件
         */ 
        public void deleteFile(String filename){ 
            try{ 
                ftpClient.deleteFile(filename); 
            }catch(IOException ioe){ 
                ioe.printStackTrace(); 
            } 
        } 
        /**
         * 关闭连接
         */ 
        public void closeConnect(){ 
            try{ 
                if(ftpClient!=null){ 
                    ftpClient.logout(); 
                    ftpClient.disconnect(); 
                    System.out.println("Ftp have closed"); 
                } 
            }catch(Exception e){ 
                e.printStackTrace(); 
            } 
        } 
        public String getConfigFile() { 
            return configFile; 
        } 
     
        public void setConfigFile(String configFile) { 
            this.configFile = configFile; 
        } 
     
        public String[] getFILE_TYPES() { 
            return FILE_TYPES; 
        } 
     
        public FTPClient getFtpClient() { 
            return ftpClient; 
        } 
     
        public void setFtpClient(FTPClient ftpClient) { 
            this.ftpClient = ftpClient; 
        } 
     
        public String getIp() { 
            return ip; 
        } 
     
        public void setIp(String ip) { 
            this.ip = ip; 
        } 
     
        public String getPassword() { 
            return password; 
        } 
     
        public void setPassword(String password) { 
            this.password = password; 
        } 
     
        public int getPort() { 
            return port; 
        } 
     
        public void setPort(int port) { 
            this.port = port; 
        } 
     
        public Properties getProperty() { 
            return property; 
        } 
     
        public void setProperty(Properties property) { 
            this.property = property; 
        } 
     
        public String getUsername() { 
            return username; 
        } 
     
        public void setUsername(String username) { 
            this.username = username; 
        } 
     
        public String getFiledir() { 
            return filedir; 
        } 
     
        public void setFiledir(String filedir) { 
            this.filedir = filedir; 
        } 
    } 
分享到:
评论

相关推荐

    Java上传文件到FTP服务器,支持断点续传.rar

    java实现ftp断点续传,上传文件到FTP服务器,支持断点续传,同时支持LINUX主机,代码里面包括了比较多的java网络操作类,比如连接FTP的常规类、递归创建远程服务器目录 上传文件到服务器,新上传和断点续传操作,远程...

    java上传文件到ftp.txt

    ### Java上传文件到FTP知识点详解 #### 一、概述 在现代软件开发中,文件传输是一项常见的需求。其中,FTP(文件传输协议)是最常用的一种文件传输方式之一。本篇文章将详细阐述如何利用Java编程语言实现本地文件...

    java上传文件至FTP服务器

    Apache commons-net 上传文件至FTP服务器

    JAVA上传文件到FTP服务器

    NULL 博文链接:https://zhou-hong-liang.iteye.com/blog/305214

    java中生成xml文件,并上传至ftp服务器

    JAVA中生成xml文件到指定路径和上传到ftp服务器到指定路径的方法。

    java大文件上传至ftp服务器带进度条显示的

    总的来说,实现Java大文件上传至FTP服务器并显示同步进度条涉及的关键技术点包括:FTPClient的使用、文件的分块上传、多线程同步、GUI组件的更新以及异常处理。通过合理设计和优化,可以实现高效且用户体验良好的大...

    Java FTP 指定下载文件和上传文件到指定的服务器目录

    通过本文介绍的方法,您可以轻松实现Java应用程序中对FTP服务器的文件上传与下载操作,并能有效地指定文件上传和下载的目标目录。这对于企业级应用中的文件管理有着重要的作用。同时,在实际应用过程中还需注意安全...

    java实现文件上传到ftp

    本教程将详细介绍如何使用Java实现文件上传到FTP服务器,这适用于初学者熟悉FTP客户端编程的基础概念。 首先,我们要了解FTP的基本工作原理。FTP允许客户端连接到服务器,发送文件,接收文件,或者列出服务器上的...

    java定时从ftp服务器更新相关文件

    Java定时从FTP服务器更新相关文件是一项常见的任务,特别是在自动化数据同步和备份的场景中。这里主要涉及的技术点包括FTP协议、Java编程以及文件系统操作。本文将深入探讨这些知识点,并提供一个基于`ftp4j`库的...

    Java解析FTP服务器文本文件

    Java解析FTP服务器文本文件是指使用Java语言连接FTP服务器,上传、下载、递归目录遍历等基本操作的集合。在这个过程中,我们需要引入相关的jar包,例如cpdetector.jar、jchardet-1.0.jar、antlr.jar、commons-...

    java上传文件到ftp

    Java上传文件到FTP是一项常见的任务,特别是在自动化运维和数据传输中。这个场景描述了一个系统定期检查本地文件,然后将它们上传到多个FTP服务器并进行备份的过程。以下是对这个主题的详细解析: 首先,我们需要...

    springboot以FTP方式上传文件到远程服务器

    "Spring Boot 使用 FTP 方式上传文件到远程服务器" 在本文中,我们将详细介绍如何使用 Spring Boot 框架来实现 FTP 方式上传文件到远程服务器。FTP(File Transfer Protocol)是一种常用的文件传输协议,广泛应用于...

    java 调用ftp上传、下载文件到服务器

    如果你的FTP操作涉及到Web应用的部署,那么你可能需要将WAR文件上传到服务器的特定目录,例如`webapps`,然后由服务器自动解压并运行。 在实际应用中,可能还需要处理更复杂的情况,如错误处理、被动模式连接、SSL/...

    java实现的远程ftp文件浏览

    GetButton部分为从FTP服务器下传一个文件; PutButton部分为向FTP服务器上传一个文件。 别忘了在程序中还要引入两个库文件(import sun.net.*,import sun.net.ftp.*)。 以下是这三部分的JAVA源程序: (1)显示FTP...

    java编写的ftp文件实时监控下载上传

    用java语言编写的ftp小工具,可以按指定时间监控ftp服务器,把服务器指定目录内新产生的文件或者文件夹下载到本地指定文件夹,下载后删除数据。 也可以监控本地文件夹,把文件夹内新产生的文件或者文件夹整体上传到...

    java+jsp代码实现从FTP服务器上传下载文件

    根据给定的信息,本文将详细解释如何利用Java与JSP技术来实现从FTP服务器上传下载文件的功能,并且会对部分给出的代码片段进行解读。 ### Java + JSP 实现 FTP 文件上传下载 #### 一、JSP 页面代码实现 在JSP页面...

    使用java实现的linux和ftp服务器文件上传下载工具

    这是我使用java实现的linux和ftp服务器文件上传下载工具,需要电脑安装jdk8, 启动命令,java -jar linuxAndFtp.jar 启动成功后,浏览器访问:http://localhost:9999 服务器的账号密码通过服务器列表页面管理,添加的...

    java 读取FTP服务器文件

    这个库提供了丰富的FTP功能,包括连接、上传、下载和管理FTP服务器上的文件。 2. **添加依赖**: 在Maven项目中,可以在`pom.xml`文件中添加以下依赖: ```xml <groupId>commons-net</groupId> <artifactId>...

    java FTP服务器文件上传下载,创建多级文件夹.zip

    Java FTP服务器文件上传下载是Java开发中常见的网络编程任务,主要涉及到FTP(File Transfer Protocol)协议的应用,用于在客户端和服务器之间传输文件。本教程将详细讲解如何使用Java实现FTP服务器的文件上传、下载...

Global site tag (gtag.js) - Google Analytics