使用java filechooser

时间:2017-05-05 03:38:16

标签: java amazon-web-services amazon-s3

我想通过java文件选择器将多个文件上传到Amazon s3存储桶。为此,我使用以下代码。此代码可以将文件上传到s3,但下次当我上传另一个文件时,会替换上一个文件。我知道它是由String key =“squre.jpg”引起的;在代码中。我的问题是如何上传多个文件而不替换前一个文件。提前致谢。

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();
                }
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

从您的代码中看起来您需要使用filechooser的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();
                    }
                }
        }
    });