将图像上传到Servlet,然后转发到Amazon s3

时间:2016-07-19 14:29:58

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

我在处理上传到服务器的图片时遇到问题。在线查看,在我看来,我需要首先从apache库创建一个DiskFileItemFactory对象,然后创建一个ServletFileUpload对象。从那里我循环遍历所有表单项,直到我点击文件。但是,当我formItems.iterator()时,compliler抱怨

- Iterator is a raw type. References to generic type Iterator<E> should be 
     parameterized
    - The method iterator() is undefined for the type List

我将如何解决此错误,有人可以帮助我了解我做错了什么?我是Java Servlets的新手,但我熟悉Java。这是我下面的代码。谢谢!

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //doGet(request, response);

        PrintWriter out = response.getWriter();

        AWSCredentials credentials = new BasicAWSCredentials("XXXX", "XXXX");

        AmazonS3 s3Client = new AmazonS3Client(credentials);

        try {

        out.println("Hello, THIS WORKED!!<br/>");

             boolean isMultipart = ServletFileUpload.isMultipartContent(request);


             if (isMultipart) {
                    out.println("Multipart is true");

                      response.setHeader("Access-Control-Allow-Origin", "*");
                        response.setHeader("Access-Control-Allow-Methods", "POST");
                        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
                        response.setHeader("Access-Control-Max-Age", "86400");

                        DiskFileItemFactory factory = new DiskFileItemFactory();
                        factory.setSizeThreshold(THRESHOLD_SIZE);

                        ServletFileUpload upload = new ServletFileUpload(factory);
                        upload.setFileSizeMax(MAX_FILE_SIZE);
                        upload.setSizeMax(MAX_REQUEST_SIZE);


                        List formItems = (List) upload.parseRequest(request);

/////COMPILER ERROR. It complains that the Iterator is a raw type. How Do I learn how to fix this? 
                        Iterator iter = formItems.iterator();



                        // iterates over form's fields to get UUID Value
                        while (iter.hasNext()) {
                            FileItem item = (FileItem) iter.next();
                            ObjectMetadata om = new ObjectMetadata();
                            om.setContentLength(item.getSize());

                            try {



                            } catch (AmazonServiceException ase) { 
                                out.println(ase.toString());

                            } catch (AmazonClientException ace) {


                                out.println(ace.toString());
                            }


                            s3Client.putObject("images", "TestImg.jpg", item.getInputStream(), om);
                            if (item.isFormField()) {

                            out.println(item.getString());

                            }
                            // processes only fields that are not form fields
                            if (!item.isFormField()) {
                               // itemFile = item;
                            }
                        }


//                      
//                  }
//                  

             }


        } catch(Exception e) {



            out.print(e.toString());

        }



    }

1 个答案:

答案 0 :(得分:0)

我认为你需要退后一步,尝试逐步解决这个问题。暂时忘掉所有AWS的内容,因为您甚至没有在代码中与AWS交互。在您担心将文件复制到S3之前,您需要首先让文件上传工作。要做到这一点,你需要弄清楚编译器告诉你什么。

让我们看看这两行:

List formItems = (List) upload.parseRequest(request);
Iterator iter = formItems.iterator();

看几行,我们发现uploadServletFileUpload的一个实例。我们转到ServletFileUpload.parseRequest() public List<FileItem> parseRequest(HttpServletRequest request) throws FileUploadException 。文档显示了此方法签名:

List

注意它不仅仅返回一个List<FileItem>对象,它实际上返回一个(List)对象。当你把它投射到FileItem时,你扔掉了那些信息。因此,让我们更改代码以使用Iterator,在我们使用时,为什么不使用the documentation而不是像这样的旧学校List<FileItem> formItems = upload.parseRequest(request); for (FileItem item : formItems) { ObjectMetadata om = new ObjectMetadata(); om.setContentLength(item.getSize());

{{1}}