您是否尝试过引发包含未正确 sftp 的文件的异常?
异常类可能如下所示:
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");
}
}
我希望这有帮助。