Skip to content

File size is zero while copying them from Network to local path using JSch

I am trying to write a service which will copy multiple files from one network location to local project file , while i am running the code i am having below observations

  1. If there is only one file and say it is of 520kb while copying through the network we are having 519kb.
  2. If there are multiple files the files are copied into the local project path but all of them are of 0kb and only the last file from the network is having data rest are 0kb .
  3. The Files are initially 0kb but after sometimes they are showing actual size.

I am not able to understand the issue here below is my code

public boolean createCopyOnNetwork()throws Exception {
    boolean isAbleToCopy =false;
     List<ChannelSftp.LsEntry> files = findFile(getChannelUsingSftp(), filemask);
     byte[] buffer = new byte[1024];
     for(ChannelSftp.LsEntry entry : files) {
         bis = new BufferedInputStream(getChannelUsingSftp().get(entry.getFilename()));
         newFile = new File("downloadedFiles\"+entry.getFilename());
         os = new FileOutputStream(newFile);
         bos = new BufferedOutputStream(os);
     while( (readCount = > 0) {
         bos.write(buffer, 0, readCount);
    return isAbleToCopy;

public List<ChannelSftp.LsEntry> findFile(ChannelSftp sftpChannel, String filemask) throws SftpException, JSchException {"Getting file List using findFile ");
    List<ChannelSftp.LsEntry> list = getChannelUsingSftp().ls(filemask);
    return list;
public ChannelSftp getChannelUsingSftp() throws JSchException, SftpException {"Getting Sftp");
    channel = getSessionOnNetwork(smbUsername,host,port).openChannel("sftp");
    ChannelSftp sftpChannel = (ChannelSftp)channel;;
    return sftpChannel;
public  Session getSessionOnNetwork(String smbUsername,String host,int port) throws JSchException {"Getting session on Network");
    session= jsch.getSession(smbUsername,host,port);
    session.setConfig("StrictHostKeyChecking", "no");
    return session;



You missing calls to close() each stream after EACH use inside the loop. Using try with resources will help clean-up all streams immediately after use, and change code structure to make use of built in calls of InputStream and Files.

for(ChannelSftp.LsEntry entry : files) {
    Path path = Path.of("downloadedFiles",entry.getFilename());
    try(InputStream is = getChannelUsingSftp().get(entry.getFilename());
        OutputStream os = Files.newOutputStream(path))

You may find that there is a second issue here relating to use of getChannelUsingSftp() inside the loop, so that each file retrieval makes a reconnection to ftp server and is not re-using the connection made to same server when generating the list of files as for findFile. Assigning to a local variable should help avoid excessive server connections:

ChannelSftp ftp = getChannelUsingSftp();