Q:

HttpPost write body as well as the request parameters

I am using Apache's HttpPost. I am trying to upload files to the web server and it results in following:

POST site/upload?uploadType=chunked&requestId={requestId} HTTP/1.1
Host: 
  
  Accept: application/xml
Authtoken: 
  
  Request Parameters
requestId  The unique identifier for the current upload session.
Request Headers
Host   The host name of the web server.
Accept     The format of the response. Valid values are: application/xml or application/json.
Authtoken  The authentication token received after successfully logging on.
FileEOF    Specifies whether the end of file has reached with the current file chunk. Allowed values are 0 and 1. End of file means value 1.
Request Body
Include the contents of the file chunk in bytes.

What I have come up is this:
HttpPost post = new HttpPost(url);
            post.setHeader("Authtoken", params.get("token"));
            String fileName = file.getName();
            long offset = Long.parseLong(chunkOffset);
            //...
            post.setHeader("FileEOF", eof);
            /*List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("requestId", requestId));
            post.setEntity(new UrlEncodedFormEntity(postParameters));*/

            fileInputStream.skip(offset);
            fileInputStream.read(bytes);
            post.setEntity(new ByteArrayEntity(bytes));

Where do I write the parameters for the HttpPost around here?

A:

You will have to use MultipartEntity and FileBody when dealing with file uploads.
Example:
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("someparameter1", new StringBody("Woody"));
entity.addPart("someparameter2", new StringBody("Woodpecker"));
File fileToSend = new File(filePath);
FileBody fileBody = new FileBody(fileToSend, "application/octet-stream");
entity.addPart("upload_file", fileBody);

httpPost.setEntity(entity);

With MultipartEntityBuilder - try following:
MultipartEntityBuilder builder = MultipartEntityBuilder.create();        
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

final File aFile = new File(fileName);
FileBody fileBody = new FileBody(file);

builder.addPart("file", fileBody);  
builder.addTextBody("requestId", requestId);
final HttpEntity httpEntity = builder.build();

