无法完成POST请求AWS Lambda和API网关

时间:2020-02-14 19:26:34

标签: java spring amazon-web-services aws-lambda aws-api-gateway

我具有使用lambda的api网关的上述配置。我试图发布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;

    }

我在堆栈跟踪中收到以下错误。有人可以给我关于如何将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

1 个答案:

答案 0 :(得分:1)

我仍然看到很多人都提出了与此完全相同的MultipartException错误。 apache文档令人困惑,对于许多解决方案,可以使用正确的依赖项。

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