[JAX-RS] - file upload
multipart/form-data로 파일 업로드 하자.
서버
interface
@POST
@Path("/TEST02")
@Consumes("multipart/form-data")
void TEST02(MultipartBody body) throws BizException;
implement
public void TEST02(MultipartBody body) throws BizException {
File file = null;
try {
file = FileUtil.makeFile(body.getAttachment("testFile").getDataHandler().getInputStream(),
"D:/upload", body.getAttachmentObject("fileName", String.class) );
} catch (IOException e) {
e.printStackTrace();
}
}
inputStrem을 파일로 떨궈주면 땡..
클라이언트
public static void excute_post1(String url) throws ClientProtocolException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("content", "multipart/form-data");
httppost.addHeader("Accept", "application/json; charset=UTF-8" );
FileBody filebody = new FileBody(new File("D:\\test.txt"));
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("testFile", filebody);
reqEntity.addPart("fileName", new StringBody("aaaa.txt"));
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
httppost.abort();
httpclient.getConnectionManager().shutdown();
}