How to post an image in Java

There are many API endpoints which expect images or files to be submitted as part of the body. In Java we can do that easily using HTTP client library. Here is the sample code :

try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpPost httppost = new HttpPost(
					"FaceApp/2.0.561 (Linux; Android 6.0)");
			httppost.setHeader("X-FaceApp-DeviceID", "abc4fghr");
			File file = new File("C:\\Downloads\\pics\\Social\\f025cd8b-576e-4941-a4e1-911139754c41.jpg");
			MultipartEntity mpEntity = new MultipartEntity();
			ContentBody cbFile = new FileBody(file, "image/jpeg");
			mpEntity.addPart("Image", cbFile);
			CloseableHttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
		} catch (IOException ex) {
			// Logger.getLogger(VentanaPrincipal.class.getName()).log(Level.SEVERE,
			// null, ex);

Uday Ogra

Connect with me at and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *