java操作远端服务器文件

1. 依赖

<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>


2.代码
/**
     * @param sourceFile 源文件路径
     * @param targetDir  拆分后文件存放目录
     * @param filePrefix 生成文件的前缀名
     * @param fileSuffix 生成文件的后缀名
     * @param fileCount  拆分文件行数
     */
    public static void remoteFile(String sourceFile, String targetDir , String filePrefix, String fileSuffix,long fileCount) throws Exception {
        BufferedReader reader = null;
        BufferedWriter writer = null;
        Connection connection = null;
        try {
            if( !targetDir.endsWith("\\") ){
                targetDir += File.separator;
            }

            connection = new Connection("ip");
            connection.connect();
            boolean isAuthenticated = connection.authenticateWithPassword("root", "password");
            if(!isAuthenticated) {
                throw new IOException("服务器验证失败!");
            }

            SCPClient scpClient = connection.createSCPClient();
            SCPInputStream scpInputStream = scpClient.get(sourceFile);
            // 输入缓冲流
            reader = new BufferedReader(new InputStreamReader(scpInputStream, "UTF-8"));
            String str = null;

            // 行数
            long len = 0;
            logger.info("开始写入......请等待......");
            long startTime = System.currentTimeMillis();
            while ((str = reader.readLine()) != null) {
                // 当前行文件
                long fileNum = (len / fileCount) + 1;
                String fileName = targetDir + filePrefix + "_" + fileNum + "." + fileSuffix;
                File targetFile = new File(fileName);
                if (!targetFile.getParentFile().exists()) {
                    targetFile.getParentFile().mkdirs();
                }
                // 使用 BufferedWriter 如果不进行flush或者close写入不了内容。
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true)));
                writer.write(str + System.lineSeparator() );
                writer.flush();
                len ++;
            }
            logger.info( "写入完毕,一共 " + len + " 记录,耗时:" + ( System.currentTimeMillis() - startTime ) / 1000 + " s" );
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            if(writer != null) {
                try {
                    writer.close();
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }
            }
            if(reader != null) {
                try {
                    reader.close();
                }catch (Exception e) {
                    logger.error(e.getMessage());
                }
            }

            if(connection != null) {
                try {
                    connection.close();
                }catch (Exception e) {
                    logger.error(e.getMessage());
                }
            }
        }
    }
View Code

 




posted @ 2020-08-12 09:53  静思,随心  阅读(376)  评论(0编辑  收藏  举报