0

I have a file to upload (say abc.pdf). Very first time I want to upload this file as a temp file (say abc.pdf.temp). Then , if the file is successfully transferred (fully transferred) then I need to rename it to its original name (abc.pdf). But if the file is not fully transferred then I need to delete the temp file that I uploaded initially since I don't want to keep a corrupted file in the server. Is this achievable to do using this JSch library. Below is the sample code. Does this code make sense to achieve this?

Sample Code:

originalFile = 'abc.pdf';
tempFile = 'abc.pdf.temp';
fileInputStream = createobject("java", "java.io.FileInputStream").init('C:\abc.pdf'); 
SftpChannel.put(fileInputStream,tempFile);

// Comparing remote file size with local file
if(SftpChannel.lstat(tempFile).getSize() NEQ localFileSize){
    // Allow to Resume the file transfer since the file size is different
    SftpChannel.put(fileInputStream,tempFile,SftpChannel.RESUME); 
    if(SftpChannel.lstat(tempFile).getSize() NEQ localFileSize){
       // Check again if the file is not fully transferred (During RESUME) then
       // deleting the file since dont want to keep a corrupted file in the server.
       SftpChannel.rm(tempFile);
    }
}else{//assuming file is fully transferred
    SftpChannel.rename(tempFile ,originalFile);
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user3742125
  • 617
  • 2
  • 9
  • 31
  • This is not a code-writing service. What specific problem are you having with implementing this? What did you try already? Show us your code. – Martin Prikryl Nov 20 '17 at 12:54
  • Can I use superuser.com to get this info? – user3742125 Nov 20 '17 at 12:56
  • I do not understand. You have a programming question. So you should ask here. But you need to ask a question withing a scope of this site. That should include your current code and your specific problem. You didn't ask a specific problem. You are asking us to do your job for you. – Martin Prikryl Nov 20 '17 at 13:02
  • provided the code by updating the original question. – user3742125 Nov 20 '17 at 14:21

1 Answers1

0
  1. It's very unlikely that after the put finishes without throwing, the file size won't match. It can hardly happen. Even if it happens, it makes little sense to call RESUME. If something catastrophic goes wrong that is not detected by put, RESUME is not likely to help.

    And even if you want to try with RESUME, it does not make sense to try once. If you believe it makes sense to retry, you have to keep retrying until you succeed, not only once.

  2. You should catch exception and resume/delete/whatever. That's the primary recovery mechanism. This is 100x more likely to happen than 1.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • if we want to use RESUME in the catch exception then we should have a file exists check..right??. how can i do this remote file exist check..using "ls" method?? or any other way?? – user3742125 Nov 20 '17 at 15:28
  • No need to do any check. As I've told you already, the `RESUME` checks itself. But if you want, you can use the `lstat` or `stat` for the check. See https://stackoverflow.com/q/11968878/850848 – Martin Prikryl Nov 20 '17 at 15:30
  • lstat or stat is not used for a file exist check.. It is just "ls"..right?? Or is there any way to find file existence using lstat or stat – user3742125 Nov 20 '17 at 15:55
  • `ls` is a very inefficient check for a single file existence. See the answer by @zelinka for check using `lstat` (for `stat`, it's the same). – Martin Prikryl Nov 20 '17 at 16:10