Configuración de un recurso para recibir partes de datos de diversos formularios o de diversas partes del envío de un formulario HTML en JAX-RS 2.0
Los formularios HTML que transmiten datos de archivo se deben configurar con el método POST y la acción "datos de diversos formularios o de diversas partes". Estos datos se pueden recibir de una de las dos formas mediante el método de recurso JAX-RS que los acepta con la implementación de IBM Java™ API for RESTful Web Services (JAX-RS).
Acerca de esta tarea
Esta tarea proporciona instrucciones para
configurar un método JAX-RS para utilizar y generar datos de
diversos formularios o de diversas partes. El ejemplo siguiente
ilustra un formulario 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>
Puede implementar IBM JAX-RS para recibir
los datos en partes, de forma que pueda procesar estas partes usted
mismo, si es necesario.Procedimiento
Cree un método de recurso. Debe declarar uno de los métodos de recurso siguientes para
recibir y repetir contenido de datos de diversos formularios o de
diversas partes de 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 {
//manejar el archivo como desee
File tempFile = new File(fileName);
...
}
}
if (stream != null) {
stream.close();
}
return Response.ok("test").build();
}
O bien,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 {
//manejar el archivo como desee
File tempFile = new File(fileName);
...
}
}
if (stream != null) {
stream.close();
}
return Response.ok("test").build();
}
El originador del envío del formulario POST
puede generar una cabecera Content-Transfer-Encoding para una o
varias partes del mensaje de varias partes. La implementación de IBM
JAX-RS intenta descifrar automáticamente la carga útil de la parte de
acuerdo con esta cabecera cuando la cabecera es del tipo de
codificación base64 o
quoted-printable.Resultados
Ha recibido y repetido datos de un HTTP POST con tipo de contenido de datos de diversos formularios o de diversas partes, lo cual permite a la implementación de IBM JAX-RS dividir y descodificar automáticamente las partes para usted y se reciben las partes que siguen codificadas para que las procese usted mismo.