Skip to content
Advertisement

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

JavaScript

Advertisement

Answer

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.

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement