2017-05-05 56 views
0

我想通過java文件選擇器將多個文件上傳到Amazon s3桶。爲此,我使用下面的代碼。此代碼可以將文件上傳至s3,但下次我上傳另一個文件時,將替換上一個文件。我知道這是由String key =「squre.jpg」造成的;在代碼中。我的問題是如何上傳多個文件而無需替換上一個。先謝謝了。使用java filechooser在amazon s3桶中上傳多個文件

imageUpload.setOnMouseClicked(new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent event) { 
      FileChooser fileChooser=new FileChooser(); 
      fileChooser.setInitialDirectory(new File("c:\\")); 
      fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG Images","*.jpg"), 
        new FileChooser.ExtensionFilter("JPEG Images","*.jpeg"), 
        new FileChooser.ExtensionFilter("PNG Images","*.png")); 
      File file=fileChooser.showOpenDialog(null); 

      if (file!=null){ 
       try { 

AWSCredentials Credentials = new BasicAWSCredentials(
      "AWSAccessKeyId", 
      "AWSSecretKey"); 

    AmazonS3Client amazonS3Client = new AmazonS3Client(Credentials); 
    String bucketName = "awsimagetrading"; 
    String key = "squre.jpg"; 
        System.out.println("Uploading a new object to S3 from a file\n"); 
        AmazonS3 s3client = new AmazonS3Client(Credentials); 
        s3client.putObject(new PutObjectRequest(bucketName,key,file)); 
        URL url = amazonS3Client.generatePresignedUrl(bucketName,key,Date.from(Instant.now().plus(5,ChronoUnit.MINUTES))); 
        System.out.println(url); 
        //label.setText(url.toString()); 

       } catch (AmazonClientException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 
+0

是什麼阻止您爲每次上傳使用不同的密鑰?或者我在這裏錯過了什麼? – GhostCat

回答

0

從你的代碼看起來你需要使用文件選擇器的openMultipleDialog,然後你可以設置關鍵文件的名稱(file.getName())。這裏是修改後的代碼..

imageUpload.setOnMouseClicked(new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent event) { 
      FileChooser fileChooser=new FileChooser(); 
      fileChooser.setInitialDirectory(new File("c:\\")); 
      fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG Images","*.jpg"), 
        new FileChooser.ExtensionFilter("JPEG Images","*.jpeg"), 
        new FileChooser.ExtensionFilter("PNG Images","*.png")); 

      List<File> selectedFiles = fileChooser.showOpenMultipleDialog(null); 
      if (selectedFiles != null) { 
       for (File file : selectedFiles) { 
        try { 

         AWSCredentials Credentials = new BasicAWSCredentials(
           "AWSAccessKeyId", 
           "AWSSecretKey"); 

         AmazonS3Client amazonS3Client = new AmazonS3Client(Credentials); 
         String bucketName = "awsimagetrading"; 
         String key = file.getName(); 
         System.out.println("Uploading a new object to S3 from a file\n"); 
         AmazonS3 s3client = new AmazonS3Client(Credentials); 
         s3client.putObject(new PutObjectRequest(bucketName,key,file)); 
         URL url = amazonS3Client.generatePresignedUrl(bucketName,key,Date.from(Instant.now().plus(5,ChronoUnit.MINUTES))); 
         System.out.println(url); 
         //label.setText(url.toString()); 

        } catch (AmazonClientException e) { 
         e.printStackTrace(); 
        } 
       } 
     } 
    }); 
+0

非常感謝。它現在正在工作.. @jshcode – Sony

相關問題