FTP

余生颓废 提交于 2019-12-04 11:22:48

添加依赖:

<!--添加FTP依赖-->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.3</version>
        </dependency>

从FTP协议中读取文件:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ResourceBundle;


/**
*
*ftp.hostname=172.19.xx.xx
*ftp.port=21
*ftp.username=admin
*ftp.password=123456
*ftp.pathname=/
 *
 * 初始化FTP
 */
@Component
public class FTPClientRequest {
    //private static final Log logger = LogFactory.getLog(FTPClientRequest.class);
    private static final Logger logger = Logger.getLogger(Async2tda.class);
    private static ResourceBundle resource = ResourceBundle.getBundle("application");
    private static FTPClient ftpClient = null;

    @Autowired
    private Async2tda async2tda;

    /**
     * 初始化FTP
     */
    public void initFTP(){
        try { ftpClient = new FTPClient();
            ftpClient.setConnectTimeout(5000);
            ftpClient.setControlEncoding("UTF-8");
            String hostname = resource.getString("ftp.hostname");
            Integer port = Integer.parseInt(resource.getString("ftp.port"));
            String username = resource.getString("ftp.username");
            String password = resource.getString("ftp.password");
            String pathname = resource.getString("ftp.pathname");
            ftpClient.connect(hostname,port);
            ftpClient.login(username,password);
            //跳转到文件目录
            ftpClient.changeWorkingDirectory(pathname);
            //设置连接模式
            ftpClient.enterLocalPassiveMode();
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
                logger.debug("FTP连接成功");
            }else{
                ftpClient.disconnect();
                logger.debug("FTP连接失败");
            }
        } catch (Exception e) {
            logger.error("FTP连接异常:"+e);
        }
    }

    /**
     * 读取FTP的XML文件
     */
    //@Async
    public void readFilesFTP(){
        try {
            if (ftpClient == null || !FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
                initFTP();
            }
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
                FTPFile[] ftpFiles = ftpClient.listFiles();
                for (FTPFile ftpFile : ftpFiles) {
                    String fileName = ftpFile.getName();
                    if (!StringUtils.isEmpty(fileName) && fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase().equals("xml")) {
                        String xmlStr = "";
                        InputStream inputStream = null;
                        InputStreamReader inputStreamReader = null;
                        BufferedReader bufferedReader = null;
                        try {
                            inputStream = ftpClient.retrieveFileStream(fileName);
                            inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
                            bufferedReader = new BufferedReader(inputStreamReader);
                            String len;
                            while ((len = bufferedReader.readLine()) != null){
                                xmlStr += len;
                            }

                        } catch (IOException e) {
                            logger.error("sendTdaXML read files error:"+e);
                        } finally {
                            if (bufferedReader != null){
                                bufferedReader.close();
                            }
                            if (inputStreamReader != null)
                            {
                                inputStreamReader.close();
                            }
                            if (!inputStream.equals(null)){
                                inputStream.close();
                            }
                        }
                        async2tda.sendTdaXML(xmlStr);
                        //ftpClient.deleteFile(fileName);
                        ftpClient.getReply();//循环读取文件流需要执行getReply,否则第二个文件流为null
                    }
                }
            }
        } catch (Exception e) {
            logger.error("readFilesFTP is error:"+e);
        }
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!