你試過提高它包含沒有SFTP correcty文件例外嗎?
的異常類可以是這個樣子:
import java.io.File;
public class SFTPBulkFailedTransportException extends RuntimeException {
public SFTPBulkFailedTransportException(File[] untransmittedFiles){
setUntransmittedFiles(untransmittedFiles);
}
public File[] getUntransmittedFiles() {
return this.untransmittedFiles;
}
private void setUntransmittedFiles(File[] untransmittedFiles) throws IllegalArgumentException {
this.untransmittedFiles = untransmittedFiles;
}
private File[] untransmittedFiles;
}
如果拋出該異常的所有尚未傳送的文件,您可以訪問他們,如果你捕獲這個異常。這種異常會在批量傳輸文件的方法中拋出。
如果你把它放在一起:
import java.util.ArrayList;
import java.util.List;
File[] filesToSend; // all files to send
while(filesToSend.length != 0){
try{
sendbulk(filesToSend);
}catch(SFTPBulkFailedTransportException exception){
// assign failed files to filesToSend
// because of the while loop, sendbulk is invoked again
filesToSend = exception.getUntransmittedFiles();
}
}
public void sendbulk(File[] filesToSend) throws SFTPBulkFailedTransportException{
List<File> unsuccesfullFiles = new ArrayList<File>();
for(File file : filesToSend){
try{
sendSingle(file);
}catch(IllegalArgumentException exception){
unsuccesfullFiles.add(file);
}
}
if(!unsuccesfullFiles.isEmpty()){
throw new SFTPBulkFailedTransportException((File[]) unsuccesfullFiles.toArray());
}
}
public void sendSingle(File file) throws IllegalArgumentException{
// I am not sure if this is the right way to execute a command for your situation, but
// you can probably check the exit status of the sftp command (0 means successful)
String command = "REPLACE WITH SFTP COMMAND";
Process child = Runtime.getRuntime().exec(command);
// if sftp failed, throw exception
if(child.exitValue() != 0){
throw new IllegalArgumentException("ENTER REASON OF FAILURE HERE");
}
}
我希望這有助於。
但我怎麼知道哪些文件在第一次運行中沒有成功傳輸。如果我理解正確,上面的代碼會報告那些沒有被竊取的文件,但是當我第二次重新激活程序時,我只想從第一次運行中傳輸不成功的文件。 – user1588737
至於沒有發送的文件,你會從異常中獲得它們。我會更新我的答案,以說明如何完成。但我不知道如何看看sftp是否工作,您如何爲單個文件調用它? –
我通過程序調用一個shell腳本。有些東西像「sftp」。根據框架,程序的工作方式是 - 多次調用相同的腳本,但每次都使用不同的文件。如果文件已成功傳輸,腳本的返回狀態將爲0。我試圖瞭解如何知道在第二次運行時再次傳輸哪些文件(因此我正在考慮將信息存儲在某處 - 數據庫/緩存/文件等) –
user1588737