将pdf或图像文件上传到aws bucket java

时间:2017-09-01 05:54:56

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

我必须将多部分文件上传到aws存储桶,我正在做的是

public String uploadOnBucket(MultipartFile file){

            InputStream inputStream = file.getInputStream();


            BufferedImage ImageFromConvert = ImageIO.read(inputStream);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(ImageFromConvert, extension, os);
            byte[] buffer = os.toByteArray();
            InputStream is = new ByteArrayInputStream(buffer);

            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentLength(buffer.length);
            AmazonS3 s3Client = AwsUtil.s3Authentication();
            s3Client.putObject(new PutObjectRequest(uploadedFileLocation, fileName, is, meta).withCannedAcl(CannedAccessControlList.Private));
            inputStream.close();

}

上面的代码适用于任何图像,但是当我尝试上传pdf时,它给了我一个例外,即"图像为空"。我可以理解IMageIO正在导致这个问题。但我无法解决这个问题。请帮助,我想上传文件来自多部分到aws桶文件是pdf还是图像。

1 个答案:

答案 0 :(得分:0)

我已经通过直接将输入流传递到存储桶来解决了我的问题。

InputStream is = file.getInputStream();
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentLength(is.available());
            AmazonS3 s3Client = AwsUtil.s3Authentication();
            s3Client.putObject(new PutObjectRequest(uploadedFileLocation, updatedFileName, is, meta)
                    .withCannedAcl(CannedAccessControlList.Private));