How to upload a BufferedImage using Jetty HTTP client and MultiPartContentProvider?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to upload a BufferedImage using Jetty HTTP client and MultiPartContentProvider?

Alexander Farber
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


_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/jetty-users