如何在 Java 中使用 JSch 在 SFTP 服务器上将大文件拆分为多个文件

时间:2021-06-09 18:01:46

标签: java sftp jsch

我在 SFTP 位置有一个大约 500 GB 的非常大的文件。我需要将此文件拆分为较小的文件,并需要将其放在子目录中。我正在尝试使用 JSch 实用程序连接到 SFTP 服务器并拆分相同的服务器,但只创建了一个文件,并且没有创建更多文件。

JSch jsch = new JSch();
//create SSH connection
String host = "hostname";
String user = "username";
String password = "password";
Session session = jsch.getSession(user, host, 22);
java.util.Properties config = new java.util.Properties(); 
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(password);
session.connect();
long linesWritten = 0;
int  count = 1;
int  linesPerSplit = 3000;
String inputFilePath  = "/basedirector/largefile.txt";
String outputFolderPath = "/subfolder/smallfiles";
try {
    Channel channel = session.openChannel("sftp");
    channel.connect(60000);
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    File inputFile = new File(inputFilePath);
    InputStream stream = sftpChannel.get(inputFilePath);
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    String line = br.readLine();
    String outfileName = outputFolderPath + "\\" + "SmallFile";  
    Channel channel1 = session.openChannel("sftp");    
    channel1.connect();
    ChannelSftp sftpChannel1 = (ChannelSftp) channel1;
    sftpChannel1.cd(outputFolderPath);
    while (line != null) {
        String outfileName1 = "SmallFile"+ "_" + count + ".txt";
        System.out.println("is session connected" +session.isConnected());
        OutputStream outFile = sftpChannel1.put(outfileName1);
        OutputStreamWriter writer = new OutputStreamWriter(outFile);
        writer.write("Hello World");
        writer.close();
        while (line != null && linesWritten < linesPerSplit) {
            String str_Content=line + System.lineSeparator();
            System.out.println(linesWritten);
            line = br.readLine();
            linesWritten++;
        }
        sftpChannel1.exit();
        sftpChannel1.disconnect();
        sftpChannel1.connect();
        linesWritten = 0;//next file
        count++;//next file count
    }
   // channel.disconnect();
    br.close();
    //session.disconnect();
    inputFile.delete();
    System.out.println("end of the process");
} catch (Exception e) {
    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:0)

如果您需要在服务器上就地拆分文件(无需将其传输到本地机器并返回),您基本上不能使用 SFTP,除非您有支持该功能的服务器,而大多数服务器不支持。

这里涵盖了:
Using SFTP in Java, How do I transfer a file from one folder to another?

相关问题