java - 出现异常时恢复上次下载的文件
问题描述
我正在使用 Java SDK 1.11.534
在我的工具中,我声明了一个名为“down”的下载,使用TransferManager
,
自通话以来:
down.waitForCompletion();
是一个阻塞调用并停止ProgressBar
确认,ProgressListener
我不得不介绍SwingWorker
如下:
SwingWorker worker = new SwingWorker<Void,Integer>(){
@Override
protected void process(List<Integer> chunks) {
int j = chunks.get(chunks.size()-1);
if (i<=fileNum) jLabel4.setText("Scaricamento file " + i+ " di " + fileNum + " del DCP "+ DCPname+" in corso, attendere....");
else jLabel4.setText("Scaricamento DCP "+ DCPname+" completato con successo.");
}
@Override
protected Void doInBackground(){
for (S3ObjectSummary file: fileList){
if((!isPresent(destination,file.getKey().substring(file.getKey().lastIndexOf("/") + 1),file.getSize())) && (!(file.getKey().substring(0, file.getKey().length()-1).equals(DCPname)))){
publish(i);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, file.getKey());
down = tx.download(getObjectRequest,new File(percorso+File.separator + file.getKey().substring(file.getKey().lastIndexOf("/") + 1)));
down.addProgressListener(progressListener);
try {
down.waitForCompletion();
} catch (AmazonClientException ex) {
ex.printStackTrace();
tx.shutdownNow(true);
//jButton4.setEnabled(true);
jButton4.doClick();
} catch (InterruptedException ex) {
ex.printStackTrace();
tx.shutdownNow(true);
//jButton4.setEnabled(true);
jButton4.doClick();
}
i++;
}
这是代码的一部分,其中doInBackground()
显示了要执行的操作。
有时会发生AmazonClientException
报告:
并非所有来自 S3inputstream 的字节都被读取
这会导致文件损坏并在异常时停止程序本身。
在我的代码开头(此处未报告)到达SwingWorker
声明之前,我声明当jButton4
单击该操作时,该操作开始检查下载文件夹中的文件与 Amazon s3 上的文件之间是否存在大小不匹配,以及是否存在截断文件它被删除,名称再次添加到下载列表中。
所以到目前为止我发现的唯一解决方案是添加以下行代码:
jButton4.doClick();
在异常代码中,这意味着当遇到异常时,进度会重新启动并检查被截断的文件并重新启动下载并添加这样的文件。
我的问题是:
SDK中有什么方法可以恢复或更好地取消然后在出现异常时再次下载文件而不重新启动程序?我发现了以下用途:
jButton4.doClick();
不是专业的编码方式。
解决方案
您可以将点击操作方法的内容提取到一个新方法中,然后调用该方法。
推荐阅读
- c++ - 如何实现一个唯一ID队列,其中元素可以在C++中“碰撞”到顶部?
- reactjs - 对大范围值反应 rc-slider 灵敏度
- javascript - Javascript 不在正确的 Div 中输出
- javascript - 如何防止将相同元素从一个数组两次添加到另一个数组?
- constraints - 用约束解决变分问题
- database - ORACLE 数据库同步
- java - 如何从当前位置 + 500 米计算新的经纬度
- python - 使用 requests.post() 将文件上传到 Django
- javascript - 如何制定 (Field A * Field B + Field C) < f 的猫鼬查询?
- ios - 在 iOS 13 中,tableView 中 UIDatePicker 的无效更新在旋转时崩溃