在Exception上恢复上次下载的文件

问题描述 投票:0回答:1

我正在使用Java SDK 1.11.534

在我的工具中,我使用TransferManager声明了一个名为'down'的下载,

自召唤:

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时,操作开始检查下载文件夹中的文件与亚马逊s3上的文件之间是否存在大小不匹配,以及是否存在截断的文件被删除,名称再次添加到下载列表中。

所以我到目前为止找到的唯一解决方案是添加以下行代码:

jButton4.doClick();

在异常代码中,意味着当异常被命中时,进度重新启动并检查截断的文件并重新启动下载添加此类文件。

我的问题是:

在没有重新启动程序的情况下,SDK中是否有任何方法可以恢复或更好地取消然后再次下载文件?我发现用法:

jButton4.doClick();

不是一种专业的编码方式。

java exception amazon-s3 download
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.