title: How to upload a BufferedImage using Jetty HTTP client and MultiPartContentProvider?
body: <p>With Jetty 9.4.21.v20190926 I run a custom servlet (a WAR-file), which is able to generate images like this one:</p>
  
  <p><a href="https://i.stack.imgur.com/oLfv1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oLfv1.png" alt="generated image"></a></p>
  
  <p>by the following code:</p>
  
  <pre><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());
  }
  </code></pre>
  
  <p>This works well and now I would like to add another feature to my servlet: <a href="https://developers.facebook.com/docs/graph-api/photo-uploads/" rel="nofollow noreferrer">uploading the same image by HTTP POST to another website</a> (I would trigger the upload by calling an URL on my servlet by a hourly cronjob).</p>
  
  <p>I understand, that I should use <a href="https://www.eclipse.org/jetty/javadoc/current/org/eclipse/jetty/client/util/MultiPartContentProvider.html" rel="nofollow noreferrer">MultiPartContentProvider</a> and the following code:</p>
  
  <pre><code>MultiPartContentProvider multiPart = new MultiPartContentProvider();
  multiPart.addFilePart("attached_media", "img.png", new PathContentProvider(Paths.get("/tmp/img.png")), null);
  multiPart.close();
  </code></pre>
  
  <p>however I would prefer not to save the generated image as a temporary file.</p>
  
  <p>Instead I would like to use <a href="https://www.eclipse.org/jetty/javadoc/current/org/eclipse/jetty/client/util/BytesContentProvider.html" rel="nofollow noreferrer">BytesContentProvider</a> or maybe <a href="https://www.eclipse.org/jetty/javadoc/current/org/eclipse/jetty/client/util/ByteBufferContentProvider.html" rel="nofollow noreferrer">InputStreamContentProvider</a>… but how to <em>marry</em> them with <code>ImageIO.write()</code> call?</p>