@Pabloの発言に加えて、すでにApache Commons HTTPクライアントライブラリを使用している場合は、MultipartEntity
オブジェクトを使用してマルチパートリクエストのフォーマットを処理できます。
MultipartEntity reqEntity = new MultipartEntity();
// add your ContentBody fields as normal...
// Now, pull out the contents of everything you've added and set it as the payload
ByteArrayOutputStream bos = new ByteArrayOutputStream((int)reqEntity.getContentLength());
reqEntity.writeTo(bos);
oAuthReq.addPayload(bos.toByteArray());
// Finally, set the Content-type header (with the boundary marker):
Header contentType = reqEntity.getContentType();
oAuthReq.addHeader(contentType.getName(), contentType.getValue());
// Sign and send like normal:
service.signRequest(new Token(oAuthToken, oAuthSecret), oAuthReq);
Response oauthResp = oAuthReq.send();
もちろん、これには欠点があります。コンテンツ本体全体byte[]
をメモリ内に読み込む必要があるため、巨大なファイルを送信する場合、これは最適ではない可能性があります。ただし、小さなアップロードの場合、これでうまくいきました。