我试图在2天后在谷歌上找到解决方案,但没有任何解决方案。
DefaultAWSCredentialsProviderChain credentialProviderChain = new DefaultAWSCredentialsProviderChain();
TransferManager tx = new TransferManager(credentialProviderChain.getCredentials());
Upload myUpload = tx.upload(existingBucketName, keyName, new File("C:/Users/Desktop/Management_System.jpg"));
// While the transfer is processing, you can work with the transfer object
while (myUpload.isDone() == false) {
System.out.println(myUpload.getProgress().getPercentTransferred() + "%");
}
myUpload.waitForCompletion();
tx.shutdownNow();
此代码工作正常并上传了一张图片Management_System.jpg
,该图片已放入我的桌面以成功获取s3存储桶。我有一个带有文件字段的html表单。我想浏览计算机中的任何图像并将其上传到我的s3存储桶中。我也尝试了下面的代码,当我在亚马逊ec2上测试时,它在我的localhost上工作正常然后它会抛出类似这样的错误“ImageIO写无效的原生参数”。我将字段输入到我的表单名称[照片]。所以我的代码如下:
BufferedImage src = ImageIO.read(photo.getInputStream());
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(src, "JPG", os);
byte[] buffer = os.toByteArray();
InputStream is = new ByteArrayInputStream(buffer);
AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
if(isValidFile(s3client, existingBucketName, info.getPhoto())){
s3client.deleteObject(new DeleteObjectRequest(existingBucketName, info.getPhoto()));
}
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(buffer.length);
meta.setContentType(photo.getContentType());
s3client.putObject(new PutObjectRequest(existingBucketName, keyName, is, meta));
s3client.setObjectAcl(existingBucketName, keyName, CannedAccessControlList.PublicRead);
请指导我在哪里做错了?我在ec2实例上使用tomcat 7,我的localhost版本也是tomcat 7.我不明白是版本兼容性问题还是别的......
更新答案:
最后我让它发挥作用。这是我的完整文件上传代码通过html表单字段。我在这里张贴它是为了帮助别人,如果你发现它有用,请投票给我。感谢
AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(DashboardController.class.getResourceAsStream("/AWSCredentials.properties")));
TransferManager tx = new TransferManager(new PropertiesCredentials(DashboardController.class.getResourceAsStream("/AWSCredentials.properties")));
//AmazonS3 s3 = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());
if(isValidFile(s3, BucketName, info.getPhoto())){
s3.deleteObject(new DeleteObjectRequest(BucketName, info.getPhoto()));
}
S3Object s3Object = new S3Object();
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType(photo.getContentType());
meta.setContentLength(photo.getSize());
meta.setHeader("filename", NewkeyName);
ByteArrayInputStream bis = new ByteArrayInputStream(photo.getBytes());
s3Object.setObjectContent(bis);
//s3.putObject(new PutObjectRequest(BucketName, NewkeyName, bis, omd));
Upload myUpload = tx.upload(BucketName, NewkeyName, bis, meta);
while (myUpload.isDone() == false) {
System.out.println(myUpload.getProgress().getPercentTransferred() + "%");
}
if(myUpload.isDone() == true){
System.out.println("100%");
}
s3.setObjectAcl(BucketName, NewkeyName, CannedAccessControlList.PublicRead);
myUpload.waitForCompletion();
tx.shutdownNow();
s3Object.close();
答案 0 :(得分:1)
问题似乎是在S3代码之前。
错误消息与ImageIO.write有关。
litle搜索显示OpenJDK doesn't include JPEG support。
你可以在没有S3的情况下测试这部分代码(确保使用相同的JRE)。
你为什么使用ImageIO?在将它放入S3之前进行转码