简体   繁体   中英

Upload retry mechanism using JSch library

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);
}
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM