• Java实现文件上传到服务器(FTP方式)


    Java实现文件上传到服务器(FTP方式)

    1,jar包:commons-net-3.3.jar

    2,实现代码:

        //FTP传输到数据库服务器
        private boolean uploadServerByFtps(){
            boolean flag = true;
            //获取客户端数据文件路径
            String client_path = Pub.getPropertiesValue("analysis", "analysis.client.data.path");
            //获取数据库服务器上的存放数据文件路径
            String server_path = Pub.getPropertiesValue("analysis", "analysis.server.data.path");
            
            String hostname = Pub.getPropertiesValue("analysis", "ftp.hostname");
            String ftpusername =  Pub.getPropertiesValue("analysis", "ftp.username");
            String ftppwd = Pub.getPropertiesValue("analysis", "ftp.pwd");
            int port = 21;
            logger.info("ftp server infor:hostname:"+hostname+" ftpusername:"+ftpusername+" ftppwd:"+ftppwd+" port:"+port);
            
            //遍历客户端数据文件路径下的txt文件,然后采用FTP上传
            File client_dir = new File(client_path);  
            if(!client_dir.exists()){
                logger.info("The file directory on the host you want to read does not exist");
                  return flag;
            }
            File client_files[] =  client_dir.listFiles();
            if(client_files.length == 0){
                logger.info("You want to read the file directory without any files");
                  return flag;
            }
            //开始遍历文件
            //创建ftp客户端
            FTPClient ftpClient = new FTPClient();
            ftpClient.setControlEncoding("GBK");
            for(int i=0;i<client_files.length;i++){ 
                String getfileName = client_files[i].getName();
                String getfileNamePath = client_files[i].getPath();
                //只对txt文件做处理
                if((getfileName.substring(getfileName.lastIndexOf(".")).trim().equals(".txt"))){
                    //todo:增加控制文件(已经传输过的文件)的传输
                    
                     try {
                        //链接ftp服务器
                        ftpClient.connect(hostname, port);
                        //登录ftp
                        ftpClient.login(ftpusername, ftppwd);
                        int reply = ftpClient.getReplyCode();
                        if (!FTPReply.isPositiveCompletion(reply)) {
                            ftpClient.disconnect();
                            logger.info("Returns a 530 password username error or the current user does not have permission to close the FTP connection");
                            return false;
                        }
                        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                        // ftpClient.makeDirectory("path");//在root目录下创建文件夹
                        String server_file_name = server_path+ getfileName;
                        logger.info("server_file_name>>>>>>: " + server_file_name);
                        logger.info("getfileNamePath>>>>>>: " + getfileNamePath);
                        
                        //读取源文件流(客户端文件)
                        InputStream client_fileInput = new FileInputStream(getfileNamePath);
                        //传送到服务端
                        ftpClient.storeFile(server_file_name, client_fileInput);//文件你若是不指定就会上传到root目录下
                        client_fileInput.close();
                        ftpClient.logout();
                        logger.info("ftp is success");
                    } catch (SocketException e) {
                        // TODO Auto-generated catch block
                        logger.info("port problem: "+e.toString());
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        logger.info("IO problem: "+e.toString());
                        e.printStackTrace();
                    } catch (Exception e) {
                        logger.info("ftp problem: "+e.toString());
                        e.printStackTrace();
                    }finally {
                        if (ftpClient.isConnected()) {
                            try {
                                ftpClient.disconnect();
                            } catch (IOException ioe) {
                                ioe.printStackTrace();
                                logger.info("IO problem: "+ioe.toString());
                            }
                        }
    
                    }
                }
            }
            
            return flag;
        }
  • 相关阅读:
    .NET互操作技术杂谈
    VS2008 Remote Debug HOW TO
    Entity Framework一对多关系或一对一关系删除子对象的方法
    Associations in EF Code First: Part 1 – Introduction and Basic Concepts
    如何管理Entity Framework中得事务
    在asp.net mvc3中编译视图文件
    entity framework中对关系使用默认规则与配置
    使用Entity Framework时动态生成lamda表达式
    asp.net mvc 身份验证中返回绝对路径的ReturnUrl
    Entity Framework 事务处理SaveChanges(false)
  • 原文地址:https://www.cnblogs.com/lizm166/p/8108544.html
Copyright © 2020-2023  润新知