在eclipse中无法使用localhost将文件上传到amazon S3存储桶

时间:2018-06-05 05:59:36

标签: java amazon-s3

我正在尝试通过网页将文件上传到Amazon S3存储桶。

使用apache tomcat服务器在Eclipse中创建“动态Web项目”。创建JSP,Servlet和Java文件。我使用过aws-java-sdk-1.11.335.jar文件。尝试通过提交按钮单击上载图像文件,发生了Class Not Found Exception。有人可以帮助解决这个问题。

这是JSP,Servlet和类文件的代码:

的index.jsp

<form method="post" action="UploadFileServlet" enctype="multipart/form-data">
       <input type="submit" value="Upload" />
</form>

UploadFileServlet.java

@WebServlet("/UploadFileServlet")
public class UploadFileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;


public UploadFileServlet() {
    super();
}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    UploadFilestoAWSS3 uploadfilestoawss3 = new UploadFilestoAWSS3();
    uploadfilestoawss3.UploadFiles();

    PrintWriter out = response.getWriter();
    out.print("Files are uploaded to Bucket");

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
}

UploadFilestoAWSS3.java

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadResult;
import com.amazonaws.services.s3.model.PartETag;
import com.amazonaws.services.s3.model.UploadPartRequest;
import com.amazonaws.services.s3.model.UploadPartResult;

public class UploadFilestoAWSS3 {

    public void UploadFiles() throws IOException {
        String clientRegion = "region";
        String bucketName = "bucket name";
        String filePath = "C:\\Users\\......\\imagefile.jpg";

        File file = new File(filePath);
        String keyName = file.getName();
        long contentLength = file.length();
        long partSize = 10 * 1024 * 1024; // Set part size to 10 MB. 

        try {
            BasicAWSCredentials awsCreds = new BasicAWSCredentials("Access Key", "Secret Key");
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                                .withRegion(clientRegion)
                                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                                .build();

        List<PartETag> partETags = new ArrayList<PartETag>();

        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, keyName);
        InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);

        long filePosition = 0;
        for (int i = 1; filePosition < contentLength; i++) {
            partSize = Math.min(partSize, (contentLength - filePosition));

            UploadPartRequest uploadRequest = new UploadPartRequest()
                    .withBucketName(bucketName)
                    .withKey(keyName)
                    .withUploadId(initResponse.getUploadId())
                    .withPartNumber(i)
                    .withFileOffset(filePosition)
                    .withFile(file)
                    .withPartSize(partSize);

            UploadPartResult uploadResult = s3Client.uploadPart(uploadRequest);
            partETags.add(uploadResult.getPartETag());

            filePosition += partSize;
        }

        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, keyName,
                initResponse.getUploadId(), partETags);
        s3Client.completeMultipartUpload(compRequest);
    }
    catch(AmazonServiceException e) {
        e.printStackTrace();
    }
    catch(SdkClientException e) {
        e.printStackTrace();
    }

}

}

错误是:

Type Exception Report

Message Servlet execution threw an exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Root Cause
java.lang.NoClassDefFoundError: com/amazonaws/AmazonServiceException
com.shiva.amazonaws.UploadFileServlet.doGet(UploadFileServlet.java:51)
com.shiva.amazonaws.UploadFileServlet.doPost(UploadFileServlet.java:64)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Root Cause
java.lang.ClassNotFoundException: com.amazonaws.AmazonServiceException
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1292)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1121)
com.shiva.amazonaws.UploadFileServlet.doGet(UploadFileServlet.java:51)
com.shiva.amazonaws.UploadFileServlet.doPost(UploadFileServlet.java:64)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

1 个答案:

答案 0 :(得分:1)

问题是找不到某些Amazon Web Services SDK类,特别是com.amazonaws.AmazonServiceException,这是AWS SDK的基本异常类型 - 表明可能缺少所有AWS SDK文件。 Java类路径。

请注意,文件aws-java-sdk-1.11.335.jar不是整个AWS SDK(人们可能已经想过),只包含一些元数据文件。 AWS Java SDK被拆分为多个工件 - 因为它太大,您不想在所有类中使用单个JAR文件,您只想将SDK用于您正在使用的特定服务。

因为您使用的是S3,所以您可能只需要S3 JAR及其依赖项,如Maven repository page for the AWS S3 SDK中所示。请注意,其中一个依赖项是aws-java-sdk-core,这是com.amazonaws.AmazonServiceException类所在的位置。

您还可以在http://central.maven.org/maven2/com/amazonaws/aws-java-sdk-s3/1.11.335/下的POM文件中查看所有依赖项(并导航目录以收集所需的所有文件)。

通常,由于目前常见的复杂依赖结构,建议不要手动下载JAR并将它们逐个放入Eclipse构建路径,而是改为使用Maven。

如果你在eclipse中有一个标准的Java项目,你可以右键单击包浏览器中的项目名称,然后选择&#34;配置&#34; - &gt;&#34;转换为Maven项目&#34;并在打开的对话框中填写一些详细信息(*)。在竞争之后,Eclipse将在项目的根目录中创建一个pom.xml文件 - 打开它,您将获得一个自定义编辑器来设置您的依赖项。单击标有&#34; Dependencies&#34;的选项卡。然后在&#34;添加&#34;您应该能够搜索aws-java-sdk-s3,将其添加到依赖项列表中,然后Maven将自动下载所有必需的JAR文件并自动将它们添加到您的构建路径中。

*)&#34; Group ID&#34;通常是项目的顶级包名称,它应该看起来像一个反向的域名 - 例如我的域名是geek.co.il,所以我的组ID将是il.co.geek; &#34;工件ID&#34;是项目的单个字标识符,例如my-project