JAX-RS 2.0의 HTML 양식 제출에서 멀티파트/양식-데이터 파트를 수신하도록 자원 구성
HTML은 POST 메소드 및 "멀티파트/양식-데이터" 조치를 사용하여 전송 파일 데이터를 구성하도록 형성됩니다. JAX-RS(Java™ API for RESTful Web Services) 구현을 통해 데이터를 허용하는 JAX-RS 자원 메소드를 사용한 두 가지 방법 중 하나로 이 데이터를 수신할 수 있습니다.
이 태스크 정보
이 태스크는 멀티파트/양식-데이터를 사용하고 생성하도록 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에서 멀티파트/양식-데이터 컨텐츠를 수신하고
반향시켜야 합니다.
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 제출의 제안자는
멀티파트 메시지의 하나 이상의 파트에 대한 Content-Transfer-Encoding 헤더를
생성할 수 있습니다. IBM JAX-RS 구현은 헤더가 base64
또는 quoted-printable 인코딩 유형인 경우 이 헤더에
따라 파트의 페이로드를 자동으로 디코딩하려고
합니다.결과
IBM JAX-RS 구현이 파트를 분할하고 자동 디코딩할 수 있도록 하고 자체 처리할 인코딩된 파트를 수신하여 멀티파트/양식-데이터 컨텐츠-유형의 HTTP POST에서 데이터를 수신하고 반향시켰습니다.