ftp上传文件至服务器

上传action

public void putTxtToFTP() {
        System.out.println("开始执行定时器任务:维系一键体检订单量!");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1); //得到前一天
        Date date = calendar.getTime();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String yesterday = df.format(date);

        StringBuffer yjtjOrderTxt = new StringBuffer();
        yjtjOrderTxt.append("yjtywx_").append(yesterday).append(".txt");


        List<WxActiveData> yjtjOrderList = activeDataService.wxGetYjtjOrder();

        //FtpUtil ftpUtil = new FtpUtil("hexin","hx105155","134.64.105.155", "/yjtydd");  //测试库FTP
        FtpUtil ftpUtil = new FtpUtil("ahftp","ahdx@#$_123dic","192.168.0.28", "/yjtydd");     
        PrintWriter printWriter = null;
        File highFeeFile = new File(Contants.FILE_PATH_BAK + yjtjOrderTxt);//Contants.FILE_PATH_BAK = "/opt/wss/domains/tmp/"

//      File highFeeFile = new File("D:/" + highFeeTxt);
//      File gjmyFile = new File("D:/" + gjmyTxt);
        //File highFeeFile = new File("D:/" + yjtjOrderTxt);
        try {
            printWriter = new PrintWriter(new FileWriter(highFeeFile, true));
            for (int i = 0; i < yjtjOrderList.size(); i++){
                printWriter.println(yjtjOrderList.get(i).toString().trim());
            }
        } catch (IOException e) {
            System.out.println("主动服务 高额数据报表任务异常!");
        }finally{
            printWriter.close();
        }
        if(highFeeFile.exists()){
            System.out.println("主动服务定时器任务 高额数据 上传成功"+yjtjOrderTxt.toString());
            ftpUtil.uploadFile(highFeeFile, yjtjOrderTxt.toString());
        }else{
            System.out.println("主动服务定时器任务 高额数据 上传失败"+yjtjOrderTxt.toString());
        }

        System.out.println("维系一键体检订单报表结束");

    }

    public ActiveDataService getActiveDataService() {
        return activeDataService;
    }

    public void setActiveDataService(ActiveDataService activeDataService) {
        this.activeDataService = activeDataService;
    }

上传工具类

/**
 * FTP工具类
 * 
 * @author 李顺
 * 
 */
public class FtpUtil {

    private String host;

    private String userName;

    private String password;

    private int port = 21;

    private String pathname;

    private FTPClient ftpClient;

    private String fileName;

    public FtpUtil() {

    }

    public FtpUtil(String userName, String password, String host,
            String pathname) {
        this.userName = userName;
        this.password = password;
        this.host = host;
        this.pathname = pathname;
    }

    public void uploadFile(File file, String fileName) {
        System.out.println("**************开始ftp上传******************");
        System.out.println("ftp服务器IP:" + host);
        System.out.println("ftp服务器userName:" + userName);
        System.out.println("ftp服务器password:" + password);
        System.out.println("ftp服务器pathname:" + pathname);
        System.out.println("*******************************************");

        ftpClient = new FTPClient();
        FileInputStream fis = null;

        try {
            // 输入流
            fis = new FileInputStream(file);
            System.out.println("===获取本地文件流====");
            // 连接服务器
            ftpClient.connect(host);
            System.out.println("====建立远程连接====");
            ftpClient.enterLocalPassiveMode();

            // 登录FTP
            ftpClient.login(userName, password);
            System.out.println("====登录FTP====");
            // 指定写入的目录
            ftpClient.changeWorkingDirectory(pathname);
            System.out.println("====指定写入的目录====");

            ftpClient.setBufferSize(1024*1024);

            ftpClient.setControlEncoding("GBK");

            // 写操作
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

            ftpClient.storeFile(fileName, fis);
            System.out.println("====写入文件====");
        } catch (Exception e) {
            System.out.println("上传异常====" + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {

            try {
                fis.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 上传集团文件
     * @param file
     * @param fileName
     */
    public Map uploadJTFile(File file, String fileName) {
        Map<String, String> m = new HashMap<String, String>();
        String flag = "0";
        System.out.println("**************开始ftp上传******************");
        System.out.println("ftp服务器IP:" + host);
        System.out.println("ftp服务器userName:" + userName);
        System.out.println("ftp服务器password:" + password);
        System.out.println("ftp服务器pathname:" + pathname);
        System.out.println("*******************************************");

        ftpClient = new FTPClient();
        FileInputStream fis = null;

        try {
            // 输入流
            fis = new FileInputStream(file);
            System.out.println("===获取本地文件流====");
            // 连接服务器
            ftpClient.connect(host,22);//集团的连接22端口
            System.out.println("====建立远程连接====");
            ftpClient.enterLocalPassiveMode();

            // 登录FTP
            ftpClient.login(userName, password);
            System.out.println("====登录FTP====");
            // 指定写入的目录
            ftpClient.changeWorkingDirectory(pathname);
            System.out.println("====指定写入的目录====");

            ftpClient.setBufferSize(1024*1024);

            ftpClient.setControlEncoding("UTF-8");

            // 写操作
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

            ftpClient.storeFile(fileName, fis);
            System.out.println("====写入文件====");
            m.put("flag", flag);
            m.put("msg", "");
        } catch (Exception e) {
            flag = "-1";
            System.out.println("上传异常====" + e.getMessage());
            m.put("flag", flag);
            m.put("msg", e.getMessage());
        } finally {
            try {
                fis.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return m;
    }


    /**
     * 端到端定时任务专用
     */
    public int downloadFileForClient(String remoteFileName, String localPath) {
        ftpClient = new FTPClient();
        int flag = -1;//成功
        try {
            // 连接服务器
            ftpClient.connect(host);
            ftpClient.enterLocalPassiveMode();
            // 登录FTP
            ftpClient.login(userName, password);
            // 指定写入的目录
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.setBufferSize(1024);
            ftpClient.setControlEncoding("UTF-8");
            // 写操作
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            FTPFile[] files = ftpClient.listFiles();
            if (files == null) {
                System.out.println("====远程FTP文件列表为空=====");
            }
            for (int i = 0; i < files.length; i++) {

                if (files[i] == null || files[i].getName() == null) {
                    System.out.println("====文件为空=====");
                    continue;
                }
                String name = files[i].getName();
                if(remoteFileName.equals(name)){
                    System.out.println("下载" + name + ".......");
                    File file = new File(localPath + name);
                    FileOutputStream fos = new FileOutputStream(file);
                    ftpClient.retrieveFile(name, fos);
                    flag = 0;
                    try {
                        fos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {

            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                    return flag;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }
    /**
     * ppm定时任务专用
     */
    public void downloadFile(String systemPath, String localPath) {
        ftpClient = new FTPClient();

        try {

            // 连接服务器
            ftpClient.connect(host);

            ftpClient.enterLocalPassiveMode();

            // 登录FTP
            ftpClient.login(userName, password);

            // 指定写入的目录
            ftpClient.changeWorkingDirectory(pathname);

            ftpClient.setBufferSize(1024);

            ftpClient.setControlEncoding("UTF-8");

            // 写操作
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

            FTPFile[] files = ftpClient.listFiles(systemPath);
            if (files == null) {
                System.out.println("====远程FTP文件列表为空=====");
            } else {
                System.out.println("====远程FTP文件列表长度=====" + files.length);
            }
            for (int i = 0; i < files.length; i++) {

                if (files[i] == null || files[i].getName() == null) {
                    System.out.println("====文件为空=====");
                    continue;
                }
                String name = files[i].getName();
                if (name.indexOf("PRODUCT_") > -1
                        && (name.toUpperCase().endsWith(".TXT"))
                        && (name.indexOf("_" + DateUtil.twoDayBefore()) > -1)) {
                    System.out.println("下载" + name + ".......");
                    File file = new File(localPath + name);
                    FileOutputStream fos = new FileOutputStream(file);
                    ftpClient.retrieveFile(systemPath + name, fos);
                    try {
                        fos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (name.indexOf("PROD_OFFER_") > -1
                        && (name.toUpperCase().endsWith(".TXT"))
                        && (name.indexOf("_" + DateUtil.twoDayBefore()) > -1)) {
                    System.out.println("下载" + name + ".......");
                    File file = new File(localPath + name);
                    FileOutputStream fos = new FileOutputStream(file);
                    ftpClient.retrieveFile(systemPath + name, fos);
                    try {
                        fos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (name.indexOf("OFFER_PROD_REL_") > -1
                        && (name.toUpperCase().endsWith(".TXT"))
                        && (name.indexOf("_" + DateUtil.twoDayBefore()) > -1)) {
                    System.out.println("下载" + name + ".......");
                    File file = new File(localPath + name);
                    FileOutputStream fos = new FileOutputStream(file);
                    ftpClient.retrieveFile(systemPath + name, fos);
                    try {
                        fos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (name.indexOf("PRICING_DESC_TYPE_") > -1
                        && (name.toUpperCase().endsWith(".TXT"))
                        && (name.indexOf("_" + DateUtil.twoDayBefore()) > -1)) {
                    System.out.println("下载" + name + ".......");
                    File file = new File(localPath + name);
                    FileOutputStream fos = new FileOutputStream(file);
                    ftpClient.retrieveFile(systemPath + name, fos);
                    try {
                        fos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (name.indexOf("TB_PTY_CODE_") > -1
                        && (name.toUpperCase().endsWith(".TXT"))
                        && (name.indexOf("_" + DateUtil.twoDayBefore()) > -1)) {
                    System.out.println("下载" + name + ".......");
                    File file = new File(localPath + name);
                    FileOutputStream fos = new FileOutputStream(file);
                    ftpClient.retrieveFile(systemPath + name, fos);
                    try {
                        fos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (name.indexOf("OFFER_GROUP_PRICING_DESC_") > -1
                        && (name.toUpperCase().endsWith(".TXT"))
                        && (name.indexOf("_" + DateUtil.twoDayBefore()) > -1)) {
                    System.out.println("下载" + name + ".......");
                    File file = new File(localPath + name);
                    FileOutputStream fos = new FileOutputStream(file);
                    ftpClient.retrieveFile(systemPath + name, fos);
                    try {
                        fos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                } 
                if(name.indexOf("_fail.txt") > -1 && (name.toUpperCase().endsWith(".TXT"))){
                    System.out.println("下载" + name + ".......");
                    File file = new File(localPath + name);
                    FileOutputStream fos = new FileOutputStream(file);
                    ftpClient.retrieveFile(systemPath + name, fos);
                    try {
                        fos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if(name.indexOf("_success") > -1 && (name.toUpperCase().endsWith(".TXT"))){
                    System.out.println("下载" + name + ".......");
                    System.out.println("new File :"+localPath + name);
                    System.out.println("new File2 :"+systemPath + name);

                    File file = new File(localPath + name);
                    FileOutputStream fos = new FileOutputStream(file);
                    ftpClient.retrieveFile(systemPath + name, fos);
                    try {
                        fos.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {

            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    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 String getPathname() {
        return pathname;
    }

    public void setPathname(String pathname) {
        this.pathname = pathname;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值