简体   繁体   English

无法完成 POST 请求 AWS Lambda 和 API Gateway

[英]Cannot fulfill POST request AWS Lambda and API Gateway

I have the above configuration for an api gateway that uses lambda.我有使用 lambda 的 api 网关的上述配置。 Im attempting to POST a pdf, manipulate it, and then return the new contents to the browser via the /upload route.我试图发布一个 pdf,对其进行操作,然后通过 /upload 路由将新内容返回到浏览器。

    @RequestMapping("/upload")
    public ResponseEntity upload( @RequestParam("files") MultipartFile file) throws IOException {
        String path = "C:\\Users\\nehamaj\\Downloads\\JAVA\\SpringBootFileUpload\\yikes.pdf";
        String path2 =  System.getProperty("java.io.tmpdir");
        file.transferTo(new File(path2+"\\yeahboy2.pdf" )); //transfer the uploaded file to this path AND convert its type to File
        ResponseEntity respEntity = null; //init response entity

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(path2+"\\yeahboy2.pdf"), new PdfWriter(path2+"\\yeahboy.pdf"));
        // open the pdf doc by reading in the newly typed File that was created from the uploaded file
        // write the file to the specified path
        for (int p = 1; p <= pdfDoc.getNumberOfPages(); p++) {
            if (p == 1) {
                PdfPage page = pdfDoc.getPage(p); //get the first page
                Rectangle media = page.getCropBox();
                if (media == null) {
                    media = page.getMediaBox();
                }
                float llx = media.getX() + 0;
                float lly = media.getY() + 250; //do the croping dimensions
                float w = media.getWidth() - 250;
                float h = media.getHeight() - 500;
                // It's important to write explicit Locale settings, because decimal separator differs in
                // different regions and in PDF only dot is respected
                String command = String.format(Locale.ENGLISH,
                        // re operator constructs a rectangle
                        // W operator - sets the clipping path
                        // n operator - starts a new path
                        // q, Q - operators save and restore the graphics state stack
                        "\nq %.2f %.2f %.2f %.2f re W n\nq\n", llx, lly, w, h); //establish the clipping
                // The content, placed on a content stream before, will be rendered before the other cont
                // and, therefore, could be understood as a background (bottom "layer")
                PdfPage pdfPage = pdfDoc.getPage(p);
                new PdfCanvas(pdfPage.newContentStreamBefore(), pdfPage.getResources(), pdfDoc)
                        .writeLiteral(command); //do the clipping on the specified page
                // The content, placed on a content stream after, will be rendered after the other content
                // and, therefore, could be understood as a foreground (top "layer")
//            new PdfCanvas(pdfPage.newContentStreamAfter(), pdfPage.getResources(), pdfDoc)
//                    .writeLiteral("\nQ\nQ\n");
            }

        }
        pdfDoc.close(); //CLOSE THE DOC OR THIS SHITE WILL NOT WORK

        InputStream inputStream = new FileInputStream(path2+"\\yeahboy.pdf"); //read the literal bytes
        // from the file at this location, we wrote to here via the pdf writer
        byte[] out = org.apache.commons.io.IOUtils.toByteArray(inputStream); //make a byte array from the bytes of the input stream
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type","application/pdf");
        respEntity = new ResponseEntity(out,responseHeaders, HttpStatus.OK);
        return respEntity;

    }

I get the following error in the stack trace.我在堆栈跟踪中收到以下错误。 Can someone please give me suggestions as to how to return the pdf to the browser (for viewing)??有人可以就如何将pdf返回到浏览器(以供查看)给我建议吗??

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.NoSuchMethodError: org.apache.commons.io.IOUtils.readFully(Ljava/io/InputStream;[B)V

I still see a lot of people coming up with this exact same MultipartException error.我仍然看到很多人提出了完全相同的 MultipartException 错误。 The apache documentation is confusing and for many the solution can be to use the correct dependency. apache 文档令人困惑,对于许多解决方案,可以使用正确的依赖项。

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM