Create client httpost that send a multipartfile with namefile

Thi is a simple code to create a client that send a multipart file with a specific name this client has got a header with username:password in base64 :

 

                   String filename = “nome del file “;

                    String percorsoFile =”file to download  “;

                    String url = “server https/http”;

                    String returned = null;

                    CloseableHttpClient client = new DefaultHttpClient();

                    HttpPost post = new HttpPost(url);

                    FileBody bin = new FileBody(new File(percorsoFile));

                    StringBody comment = new StringBody(“Filename: ” + filename);

                    MultipartEntity reqEntity = new MultipartEntity();

                    reqEntity.addPart(“fileName”, new StringBody(filename));

                    reqEntity.addPart(“file”, bin);

                    post.setEntity(reqEntity);

                    HttpResponse response = null;

                    post.setURI(new URI(url ));

                    String encoding = DatatypeConverter.printBase64Binary(“username2:password2”.getBytes(“UTF-8”));

                    post.setHeader(“Authorization”, “Basic ” + encoding);

                    response = client.execute(post);

                    HttpEntity entity = response.getEntity();

                    String responseString = EntityUtils.toString(entity, “UTF-8”);

                    System.out.println(responseString);

                    System.out.println(response.toString());

                    Header[] headers = response.getAllHeaders();

                    HashMap<String, String> result = new HashMap<String, String>(headers.length);

                 for (Header header : headers) {

                    System.out.println(header.getName() + ” , “+  header.getValue());

                 }