【问题标题】:Resume last downloaded file upon Exception出现异常时恢复上次下载的文件
【发布时间】:2019-09-08 11:37:16
【问题描述】:

我正在使用 Java SDK 1.11.534

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

自通话以来:

down.waitForCompletion();

是一个阻塞调用并停止ProgressListenerProgressBar 确认我不得不引入一个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();

不是专业的编码方式。

【问题讨论】:

    标签: java exception amazon-s3 download


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多