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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM