JAX-RS 2.0 で HTML フォームの実行依頼から multipart/form-data パーツを受信するためのリソースの構成
ファイル・データを送信する HTML フォームは、POST メソッドと "multipart/form-data" アクションを使用して構成されている必要があります。このデータは、IBM Java™ API for RESTful Web Services (JAX-RS) 実装によってデータを受け取る JAX-RS リソース・メソッドが使用する 2 つの方法のいずれかで受信することができます。
このタスクについて
このタスクでは、multipart/form-data を使用および作成するように JAX-RS メソッドを構成するための手順について説明します。以下の例は、HTML フォームを示しています。
<form action="http://www.example.com/" method="POST" enctype="multipart/form-data">
<input type="text" name="fileid" />
<br />
<input type="text" name="description" />
<br />
<input type="file" name="thefile" />
<br />
<input type="submit" name="submit" value="submit"/>
</form>
IBM JAX-RS を実装することによりデータを複数のパーツに分けて受信することができ、必要に応じてそれらのパーツを自身で処理することができます。手順
リソース・メソッドを作成します。 次のリソース・メソッドのいずれかを宣言して、HTTP POST から multipart/form-data コンテンツを受信しエコー出力します。
package com.example.jaxrs;
@POST
@Consumes("multipart/form-data")
@Produces("multipart/form-data")
public Response postFormData(IMultipartBody multipartBody) {
List <IAttachment> attachments = multipartBody.getAllAttachments();
String formElementValue = null;
InputStream stream = null;
for (Iterator<IAttachment> it = attachments.iterator(); it.hasNext();) {
IAttachment attachment = it.next();
if (attachment == null) {
continue;
}
DataHandler dataHandler = attachment.getDataHandler();
stream = dataHandler.getInputStream();
MultivaluedMap<String, String> map = attachment.getHeaders();
String fileName = null;
String formElementName = null;
String[] contentDisposition = map.getFirst("Content-Disposition").split(";");
for (String tempName : contentDisposition) {
String[] names = tempName.split("=");
formElementName = names[1].trim().replaceAll("\"", "");
if ((tempName.trim().startsWith("filename"))) {
fileName = formElementName;
}
}
if (fileName == null) {
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line = null;
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
formElementValue = sb.toString();
System.out.println(formElementName + ":" + formElementValue);
} else {
//handle the file as you want
File tempFile = new File(fileName);
...
}
}
if (stream != null) {
stream.close();
}
return Response.ok("test").build();
}
またはpackage com.example.jaxrs;
@POST
@Consumes("multipart/form-data")
@Produces("multipart/form-data")
public Response postFormData(List<IAttachment>attachments) {
List <IAttachment> attachments = multipartBody.getAllAttachments();
String formElementValue = null;
InputStream stream = null;
for (Iterator<IAttachment> it = attachments.iterator(); it.hasNext();) {
IAttachment attachment = it.next();
if (attachment == null) {
continue;
}
DataHandler dataHandler = attachment.getDataHandler();
stream = dataHandler.getInputStream();
MultivaluedMap<String, String> map = attachment.getHeaders();
String fileName = null;
String formElementName = null;
String[] contentDisposition = map.getFirst("Content-Disposition").split(";");
for (String tempName : contentDisposition) {
String[] names = tempName.split("=");
formElementName = names[1].trim().replaceAll("\"", "");
if ((tempName.trim().startsWith("filename"))) {
fileName = formElementName;
}
}
if (fileName == null) {
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line = null;
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
formElementValue = sb.toString();
System.out.println(formElementName + ":" + formElementValue);
} else {
//handle the file as you want
File tempFile = new File(fileName);
...
}
}
if (stream != null) {
stream.close();
}
return Response.ok("test").build();
}
フォーム POST の実行依頼の発信元は、複数パーツ・メッセージの 1 つ以上のパーツの Content-Transfer-Encoding ヘッダーを生成できます。IBM JAX-RS 実装は、このヘッダーが base64 または quoted-printable のエンコード・タイプの場合、このヘッダーにしたがってパーツのペイロードを自動的にデコードしようとします。タスクの結果
IBM JAX-RS 実装でパーツを分割して自動デコード できるようにし、エンコードされたままのパーツを受信してユーザー自身で処理することにより、multipart/form-data Content-Type のデータを HTTP POST から受信し、エコー出力しました。