互联网技术IT交流圈Java基础

Java基础——通过SCP连接到服务器取文件

2018-12-19  本文已影响8人  莫问以
IP=128.1.136.128
PORT=22
CHARSET=GBK
TIMEOUT=96000
USER=social
PASSWORD=social
DOWNPATH=/home/social/down
package com.unify.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class PropertyUtils extends PropertyPlaceholderConfigurer {

    public static final Logger logger = Logger.getLogger(PropertyUtils.class);

    private static Map<String, String> propertyMap;
    
    @Override
    protected void processProperties(
            ConfigurableListableBeanFactory beanFactoryToProcess,
            Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        propertyMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            propertyMap.put(keyStr, value);
        }
    }

    public static String getValue(String name) {
        String value = propertyMap.get(name);
        if (StringUtils.isBlank(value)) {
            return "";
        } else {
            return value;
        }
    }
    
    public static String getKey(String value) {
        String key = null;
        Set<String> set = propertyMap.keySet();
        Iterator<String> iterator = set.iterator();
        while ( iterator.hasNext()) {
             key = (String) iterator.next();
             String va = propertyMap.get(key);
             if (va.equals(value) == true ) {
                 return key;
             }
        }
        return null;
    }
    /**
     * 不带/
     * 
     * @return
     */
    public static String getRoot() {
        String rootKey = "socialServers.root";
        String cmsRoot = System.getProperty(rootKey);
        Enumeration<?> enu = System.getProperties().propertyNames();
        if (cmsRoot.endsWith(java.io.File.separatorChar + "")) {
            cmsRoot = cmsRoot.substring(0, cmsRoot.length() - 1);
        }
        logger.info(cmsRoot);
        return cmsRoot;
    }
}
package com.unify.host;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

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 com.socialServer.util.Lg;
import com.unify.util.DocDirect;
import com.unify.util.SeqGenerator;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.SFTPv3DirectoryEntry;
import ch.ethz.ssh2.SFTPv3FileAttributes;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

/**
 * 配置文件加载
 * 
 * @author guxf
 */
public class ScpClient {
    private static final Logger logger = Logger.getLogger(ScpClient.class);

    private static String ip; // 服务IP
    private static int port; // 服务端口
    private static String charSet;// 字符集
    private static int timeout; // 超时时间 毫秒

    private static String user; // 用户名
    private static String password; // 密码
    private static String downPath; // 下载地址
    public static Properties prop = new Properties(); // 配置文件加载

    static {
        try {
            String val = null;
            logger.info("开始读取配置文件!");
            InputStream inStream = ScpClient.class.getResourceAsStream("/readTxtConfig.properties");
            prop.load(inStream); // 配置文件已经读取成功

            val = prop.getProperty("IP");
            /** 判断IP是否存在 存在则赋值 */
            if (val != null && val.trim().length() > 0) {
                ip = val;
            }
            val = prop.getProperty("PORT");
            if (val != null && val.trim().length() > 0) {
                port = Integer.valueOf(val);
            }
            val = prop.getProperty("TIMEOUT");
            if (val != null && val.trim().length() > 0) {
                timeout = Integer.valueOf(val);
            }
            val = prop.getProperty("CHARSET");
            if (val != null && val.trim().length() > 0) {
                charSet = val;
            } else {
                charSet = "UTF-8"; /** 默认GBK */
            }
            val = prop.getProperty("USER");
            if (val != null && val.trim().length() > 0) {
                user = String.valueOf(val);
            }
            val = prop.getProperty("PASSWORD");
            if (val != null && val.trim().length() > 0) {
                password = String.valueOf(val);
            }

            val = prop.getProperty("DOWNPATH");
            if (val != null && val.trim().length() > 0) {
                downPath = String.valueOf(val);
            }

            logger.info("--加载核心配置--\n" + "IP:" + ip + "\t" + "端口:" + port + "\t" + "账号:" + user + "\t" + "密码:"
                    + password + "\t" + "编码:" + charSet);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getIp() {
        return ip;
    }

    public static int getPort() {
        return port;
    }

    public static String getCharSet() {
        return charSet;
    }

    public static int getTimeout() {
        return timeout;
    }

    public static String getUser() {
        return user;
    }

    public static String getPassword() {
        return password;
    }

    public static Properties getProp() {
        return prop;
    }

    public static String getDownPath() {
        return downPath;
    }

    /**
     * 读取服务器地址文件夹中TXT文件 返回获取到的txt数
     */
    public static int downFile(String path, Connection con) {
        if (con != null) {
            Session ssh = null;
            try {
                ssh = con.openSession();
                ssh.execCommand("find " + downPath + " -name '*.txt'");
                InputStream is = new StreamGobbler(ssh.getStdout());
                BufferedReader brs = new BufferedReader(new InputStreamReader(is));
                String line = "";
                int i = 0;
                while (true) {
                    String lineInfo = brs.readLine(); // txt文件路径
                    if (lineInfo != null) {
                        line = line + lineInfo;
                    } else {
                        break;
                    }
                    i++;
                    Lg.info("第[" + i + "]个Txt文件路径是--" + lineInfo);

                    SCPClient scp = con.createSCPClient();

                    DocDirect docPath = new DocDirect();
                    String doc = docPath.returnDoc();
                    try {
                        scp.get(lineInfo, docPath.returnDoc());
                        Lg.info("文件存储路径为--"+doc);
                        Lg.info("第[" + i + "]个Txt文件下载成功!");
                    } catch (Exception e) {
                        Lg.error("doc路径错误--" + doc);
                        Lg.error("文件下载失败!");
                    }
                }
                brs.close();
                // 关闭连接
                con.close();
                return i;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 连接的Session和Connection对象都需要关闭
                if (ssh != null) {
                    ssh.close();
                }
            }
        }
        return 0;
    }

    // 判断是否连接成功!
    public static Connection getConnect(String hostIp, String username, String password, int port) {
        Connection conn = new Connection(hostIp, port);
        try {
            // 连接到主机
            conn.connect();
            // 使用用户名和密码校验
            boolean isconn = conn.authenticateWithPassword(username, password);
            if (!isconn) {
                Lg.info("用户名或者是密码错误!");
            } else {
                Lg.info("服务器连接成功!");
                return conn;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除路径下的TXT文件
     * 
     * @author guxf
     * 
     * @param filePath
     *            路径
     * 
     */
    public static int deleteServerFile(String filePath) {
        File file = new File(filePath);
        File[] files = file.listFiles();// 获取文件列表
        int j = 0;
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isFile())
                continue;// 如果不是文件就跳过(排除文件夹等)
            String fileName = files[i].getName();
            if (fileName.endsWith(".txt"))
                files[i].delete();// 后缀名为txt就删除
            j++;
        }
        return j;
    }

    // 启动定时任务 定时下载
    public static void downTxt() {
        Lg.info(SeqGenerator.getDateString() + "日文件下载自动任务启动!");
        Connection ss = getConnect(ip, user, password, port);
        downFile(downPath, ss);
    }

    // 启动定时任务 定时删除
    public static void deleteTxt() {
        Lg.info(SeqGenerator.getDateString() + "日删除txt文件自动任务启动!");
        int cou;
        try {
            DocDirect doc = new DocDirect();
            cou = deleteServerFile(doc.returnDoc());
            Lg.info(SeqGenerator.getDateString() + "日删除doc下" + cou + "个文件");
        } catch (IOException e) {
            e.printStackTrace();
        }
        int couServer = deleteServerFile(downPath);
        Lg.info(SeqGenerator.getDateString() + "日删除服务端" + couServer + "个文件");
    }

    public static void main(String[] args) {
        downTxt();
        System.err.println("下载完成!");
    }
}

下载以后,存储路径,写成工具类DocDirect.java,代码:

package com.unify.util;

import java.io.File;
import java.io.IOException;

public class DocDirect {
    
    public String returnDoc() throws IOException{
        String path = new File(this.getClass().getResource("/").getPath()).getParentFile().getParentFile().getParentFile()
                .getCanonicalFile()+File.separator + "socialServers"
                + File.separator + "WEB-INF" + File.separator + "doc";
                
        return path;
    }
    
    public static void main(String[] args) {
         // 获取txt存取目录  
        DocDirect doc = new DocDirect();
        try {
            String aa = doc.returnDoc();
            System.out.println(aa);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

利用SCPClient判断DOWNPATH路径下所有txt文件,然后scpClient.get(downPath, putPath)进行下载,在这期间,遇见一个坑:
putPath写死是不可以的,当前系统是Windows系统,但是项目完成以后,是要部署到Linux系统的,所以putPath这个路径还不方便放在配置文件里,这个路径应该是项目根路径下的某个文件夹中,所以,专门写了DocDirect.java这个工具类。

这期间遇到了一个坑,通过如下代码,输出txt存储路径:

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class ttt {
    
     public void showURL() throws IOException {
         // 第一种:获取类加载的根路径 
         File f = new File(this.getClass().getResource("/").getPath());
         System.out.println("1--"+f);

         // 获取当前类的所在工程路径; 如果不加“/”  获取当前类的加载目录
         File f2 = new File(this.getClass().getResource("").getPath());
         System.out.println("2--"+f2);

         // 第二种:获取项目路径   
         File directory = new File("");// 参数为空
         String courseFile = directory.getCanonicalPath();
         System.out.println("3(1)--"+courseFile);

         // 第三种
         URL xmlpath = this.getClass().getClassLoader().getResource("");
         System.out.println("3(2)--"+xmlpath);

         // 第四种
         System.out.println("4--"+System.getProperty("user.dir"));
        
         // 获取当前工程路径
         // 第五种:  获取所有的类路径 包括jar包的路径
         System.out.println("5--"+System.getProperty("java.class.path"));
     }
     
     public static void main(String[] args) {
         ttt tt =new ttt();
         try {
            tt.showURL();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }   
}

调用该类的main方法,路径输出没有任何问题,执行下载的main方法,也能下载,但是通过spring配置的自动任务进行下载,就发现路径输出有误,下载也抛了异常,这是为什么呢?
因为linux和window上面的分盘是不一样的,所以通过获取tomcat路径来获取该文件路径才是可取之道,tomcat解析该路径和本地main方法解析出的路径是完全不一致的,所以才更改DocDirect.java这个工具类为上面所示,经测,完全可用!!

上一篇下一篇

猜你喜欢

热点阅读