menu
書いてる野郎
orebike@gmail.com
現在のところ普通にHTTP通信の実装をするならば Jakarta Commons HttpClient を使うのが普通みたいだ
バージョン違いによりインタフェースが異なるので注意 3系と4系じゃ違う
maven
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.2.1</version> </dependency>
DefaultHttpClient httpClient = new DefaultHttpClient(); //POST先をセット HttpPost method = new HttpPost("http://hoge.jp/piyo"); MultipartEntity entity = new MultipartEntity(); File file = new File("/aaa/bbb/ccc/ddd.jpg"); //mimeは適当でくっつける FileBody fileBody = new FileBody(file,"application/octet-stream","UTF-8"); //partをつける。第一引数にはPOSTされた側でファイル受ける名前をつける //通常ブラウザからやる場合の input type="file" のname属性の値を設定 FormBodyPart bodyPart = new FormBodyPart("file", fileBody); //ファイル名をつけとく bodyPart.addField("filename", file.getName()); entity.addPart(bodyPart); method.setEntity(entity); //送信! HttpResponse response = httpClient.execute(method);