首页 > 解决方案 > 出现异常时恢复上次下载的文件

问题描述

我正在使用 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();

不是专业的编码方式。

标签: javaexceptionamazon-s3download

解决方案


您可以将点击操作方法的内容提取到一个新方法中,然后调用该方法。


推荐阅读