我有一个类,它将图像加载到亚马逊 S3 并返回该图像的公共(public) url。当我将该类作为 Java 应用程序运行时,一切正常。当我通过自上而下的方法创建 SOAP Web 服务并尝试在 Web 服务代码中调用该代码时,它失败了。我确切地知道它在哪里失败,但我不知道为什么,而且我无法调试它,不打印任何内容到控制台,尽管我正在 try catch 错误。
我收到java.lang.reflect.InitationTargetException。我搜索了这里和亚马逊论坛,找不到任何如何解决此问题的建议(我看到了很多有关此错误的问题,但没有解决方案)。
这是我的类,用于上传图像并获取 url,它可以很好地用作 Java 应用程序:
import java.io.File;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class S3Uploader {
private String url = null;
public S3Uploader(String filePath){
setUrl(s3uploadAndGetUrl(filePath));
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String s3uploadAndGetUrl(String file) {
// get access to S3
//TODO protect amazon credentials, refer to amazon documentation about proper way of providing credentials.
String result = null;
URL url;
try {
AWSCredentials awsCreds = new BasicAWSCredentials(
"myCredentials",
"myCredentials");
System.out.println("Credentials loaded.");
AmazonS3Client s3Client = new AmazonS3Client(awsCreds);
System.out.println("Client created.");
// image to upload
File image = new File(file);
// set expiration date of images' urls
Calendar origDay = Calendar.getInstance();
Calendar expirationDay = (Calendar) origDay.clone();
expirationDay.add(Calendar.DAY_OF_YEAR, 30);
Date expirationDate = expirationDay.getTime();
// set bucket and images names
String bucketName = s3Client.listBuckets().get(0).getName();
String objectKey = image.getName();
// upload image
PutObjectRequest putObject = new PutObjectRequest(bucketName,
objectKey, image);
s3Client.putObject(putObject);
// create and retrieve image url
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(
bucketName, objectKey);
generatePresignedUrlRequest.setMethod(HttpMethod.GET);
generatePresignedUrlRequest.setExpiration(expirationDate);
url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
result = url.toString();
System.out.println("Pre-Signed URL = " + url.toString());
System.out.println();
} catch (AmazonServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = e.getCause().getMessage();
} catch (AmazonClientException e) {
// TODO Auto-generated catch block
System.out.println(e.getCause().getMessage());
result = e.getCause().getMessage();
}
return result;
}
}
现在我调用此类的 Web 服务代码:
/**
* UrlsCreatorSOAPImpl.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/
public class UrlsCreatorSOAPImpl implements org.example.www.UrlsCreator.UrlsCreator_PortType{
public java.lang.String getImagesUrls(java.lang.String Url) throws java.rmi.RemoteException {
S3Uploader upload = new S3Uploader("C:/Users/Maksym/Dropbox/Greece/Matala/IMG_20140508_134143.jpg");
return upload.getUrl();
}
}
这是唯一打印到控制台的内容:
INFO: Credentials loaded.
还有 SOAP 请求:
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://www.example.org/UrlsCreator/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <q0:GetImagesUrls>
<Url>asdasdadsa</Url>
</q0:GetImagesUrls>
</soapenv:Body>
</soapenv:Envelope>
和 SOAP 响应:
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <soapenv:Fault>
<faultcode>soapenv:Server.userException</faultcode>
<faultstring>java.lang.reflect.InvocationTargetException</faultstring>
- <detail>
<ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">Maksym-PC</ns1:hostname>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
如果可以的话请提供帮助,如果我需要提供任何其他信息,请告诉我。提前致谢。
更新1:我2-3天前下载了java aws sdk,所以它应该是最新的。以下是 WEB-INF/lib 下的 jar 文件列表:
aws-java-sdk-1.7.10-javadoc.jar
aws-java-sdk-1.7.10.jar
aws-java-sdk-flow-build-tools-1.7.10.jar
axis.jar
commons-codec-1.3.jar
commons-discovery-0.2.jar
commons-logging-1.1.1.jar
commons-logging.jar
httpclient-4.2.3.jar
httpcore-4.2.jar
jackson-annotations-2.1.1.jar
jackson-core-2.1.1.jar
jackson-databind-2.1.1.jar
jaxrpc.jar
joda-time-2.2.jar
saaj.jar
wsdl4j.jar
解决方案:事实证明 - 问题出在 glassfish 上,或者只是我的笔记本电脑上,我不知道。我在另一台笔记本电脑上从头开始尝试,使用相同的 eclipse 版本,相同的 glassfish 并且它有效,我仍然不知道我的笔记本电脑出了什么问题(其他网络服务运行顺利,清理项目和服务器也没有帮助)。
无论如何,感谢您的帮助=)
最佳答案
确保运行代码时使用的库 API 版本与开发/编译代码时使用的库 API 版本相同。对于远程 S3 API,请确保客户端的版本与服务器端的版本兼容。
关于java - 在 Java 中的 SOAP over HTTP Web 服务中调用 Amazon S3 客户端时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23861488/