How do I transfer files with resume support over SFTP using JSch?

When transferring files over SFTP using JSch, resuming partially transferred files (either uploads or downloads) can be implemented by handling offsets for files that are already partially transferred.

This guide explains how to:

  • Resume downloads by continuing from the last transferred byte of a local file.
  • Resume uploads by appending to a remote file.

Handling Download with Resume Support

To resume a download:

  1. Check the current size of the local file.
  2. Skip already downloaded bytes from the remote file using InputStream.skip().
  3. Append remaining content to the local file.

Code for Resuming Download

package org.kodejava.jsch;

import com.jcraft.jsch.*;
import java.io.*;

public class SFTPResumeDownload {

   public static void main(String[] args) {
      String host = "sftp.example.com";
      String username = "user";
      String password = "password";
      String localFile = "local/path/to/file.txt";
      String remoteFile = "/remote/path/to/file.txt";

      JSch jsch = new JSch();
      Session session = null;
      ChannelSftp sftpChannel = null;

      try {
         // Setup SFTP connection
         session = jsch.getSession(username, host, 22);
         session.setPassword(password);
         session.setConfig("StrictHostKeyChecking", "no"); // Disable key checking
         session.connect();

         Channel channel = session.openChannel("sftp");
         channel.connect();
         sftpChannel = (ChannelSftp) channel;

         // Resume download logic
         File file = new File(localFile);
         long localFileSize = file.exists() ? file.length() : 0;
         long remoteFileSize = sftpChannel.lstat(remoteFile).getSize();

         if (localFileSize >= remoteFileSize) {
            System.out.println("File already fully downloaded.");
            return;
         }

         try (InputStream inputStream = sftpChannel.get(remoteFile);
              OutputStream outputStream = new FileOutputStream(file, true)) {
            inputStream.skip(localFileSize); // Skip downloaded portion

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
               outputStream.write(buffer, 0, bytesRead);
            }

            System.out.println("Download resumed and completed.");
         }
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         if (sftpChannel != null) sftpChannel.disconnect();
         if (session != null) session.disconnect();
      }
   }
}

Handling Upload with Resume Support

To resume an upload:

  1. Check the size of the remote file.
  2. Skip already uploaded bytes from the local file.
  3. Use the ChannelSftp.APPEND flag to append remaining bytes to the remote file.

Code for Resuming Upload

public static void resumeUpload(ChannelSftp sftpChannel, String localFile, String remoteFile) throws SftpException, IOException {
    File file = new File(localFile);
    long remoteFileSize = 0;

    try {
        remoteFileSize = sftpChannel.lstat(remoteFile).getSize(); // Check remote file size
    } catch (SftpException e) {
        System.out.println("Remote file does not exist. Starting upload from the beginning.");
    }

    System.out.println("Resuming upload from byte: " + remoteFileSize);

    try (InputStream inputStream = new FileInputStream(file)) {
        inputStream.skip(remoteFileSize); // Skip already uploaded bytes

        // Append mode upload
        sftpChannel.put(inputStream, remoteFile, ChannelSftp.APPEND);
        System.out.println("Resume upload completed.");
    }
}

Explanation of Key Steps

  1. Session Setup:
    • A secure session is established with the SFTP server using user credentials.
    • StrictHostKeyChecking is disabled for simplicity, but proper key validation is recommended for production.
  2. Resume Logic:
    • Download: The remote file is read as an InputStream, skipping already downloaded bytes. The local file is opened in append mode.
    • Upload: The local file is read as an InputStream, skipping already uploaded bytes, and the put method with ChannelSftp.APPEND is used to continue the upload.
  3. Error Handling:
    • If the remote file or local file does not exist, appropriate error handling ensures either the upload/download starts from the beginning or exits gracefully.
  4. File Integrity: To ensure file integrity, consider validating the file with hash checks or checksums after transfer.

Notes

  • Increase the buffer size (byte[] buffer = new byte[1024]) for better performance for larger files.
  • Consider implementing retries or reconnect logic if the SFTP session disconnects during a transfer.
  • Always confirm proper permissions for writing to the destination and reading from the source.

Conclusion

The above solution demonstrates how to implement resumable file transfer via SFTP using JSch. It ensures efficient and reliable file transfers by avoiding redundant retransmission of already transferred data.


Maven Dependencies

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>

Maven Central

How do I create zip file in Servlet for download?

The example below is a servlet that shows you how to create a zip file and send the generated zip file for user to download. The compressing process is done by the zipFiles method of this class.

For a servlet to work you need to configure it in the web.xml file of your web application which can be found after the code snippet below.

package org.kodejava.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@WebServlet(urlPatterns = "/zipservlet")
public class ZipDownloadServlet extends HttpServlet {
    public static final String FILE_SEPARATOR = System.getProperty("file.separator");

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            // The path below is the root directory of data to be
            // compressed.
            String path = getServletContext().getRealPath("data");

            File directory = new File(path);
            String[] files = directory.list();

            // Checks to see if the directory contains some files.
            if (files != null && files.length > 0) {

                // Call the zipFiles method for creating a zip stream.
                byte[] zip = zipFiles(directory, files);

                // Sends the response back to the user / browser. The
                // content for zip file type is "application/zip". We
                // also set the content disposition as attachment for
                // the browser to show a dialog that will let user 
                // choose what action will he do to the content.
                ServletOutputStream sos = response.getOutputStream();
                response.setContentType("application/zip");
                response.setHeader("Content-Disposition", "attachment; filename=\"DATA.ZIP\"");

                sos.write(zip);
                sos.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Compress the given directory with all its files.
     */
    private byte[] zipFiles(File directory, String[] files) throws IOException {
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ZipOutputStream zos = new ZipOutputStream(baos)) {
            byte[] bytes = new byte[2048];

            for (String fileName : files) {
                String path = directory.getPath() +
                        ZipDownloadServlet.FILE_SEPARATOR + fileName;
                try (FileInputStream fis = new FileInputStream(path);
                     BufferedInputStream bis = new BufferedInputStream(fis)) {

                    zos.putNextEntry(new ZipEntry(fileName));

                    int bytesRead;
                    while ((bytesRead = bis.read(bytes)) != -1) {
                        zos.write(bytes, 0, bytesRead);
                    }
                    zos.closeEntry();
                }
            }

            zos.close();
            return baos.toByteArray();
        }
    }
}

Maven dependencies

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

Maven Central