Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] How to upload a BufferedImage using Jetty HTTP client and MultiPartContentProvider?

Good afternoon,

With Jetty 9.4.21.v20190926 I run a custom servlet (a WAR-file), which is able to generate images like this one: https://slova.de/ws/puzzle2?mid=669600

by the following code:

    @Override
    protected void doGet(HttpServletRequest httpReq, HttpServletResponse httpResp) throws ServletException, IOException {
        BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        // ...drawing code skipped here...
        g.dispose();
        httpResp.setStatus(HttpServletResponse.SC_OK);
        httpResp.setContentType("image/png");
        ImageIO.write(image, "png", httpResp.getOutputStream());
    }

This works well and now I would like to add another feature to my servlet: uploading the same image by HTTP POST to another website.

I understand, that I should use MultiPartContentProvider and the following code:

    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    multiPart.addFilePart("attached_media", "img.png", new PathContentProvider(Paths.get("/tmp/img.png")), null);
    multiPart.close();

however I do not want to save the generated image as a temporary file.

Instead I would like to use ByteBufferContentProvider or maybe InputStreamContentProvider… but how to marry them with ImageIO.write call?

Thank you
Alex


Back to the top