配置资源以接收来自 JAX-RS 2.0 中的 HTML 表单提交的 multipart/form-data 部分

必须使用 POST 方法和 multipart/form-data 操作配置传输文件数据的 HTML 表单。JAX-RS 资源方法可以通过这两种方式中的其中一种接收此数据,该资源方法使用 IBM Java™ API for RESTful Web Services (JAX-RS) 实现接受此数据。

关于此任务

此任务提供有关配置 JAX-RS 方法以使用和生成 multipart/form-data 部分的指示信息。以下示例展示 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 提交的发起方可以为多部分消息的一个或多个部分生成 Content-Transfer-Encoding 头。当头编码类型为 base64quoted-printable 时,IBM JAX-RS 实现将尝试根据此头自动解码部分的有效内容。

结果

您已通过允许 IBM JAX-RS 实现为您拆分和自动解码各个部分,并通过接收仍然处于编码状态的部分以自行处理,来接收和回传来自具有 multipart/form-data Content-Type 的 HTTP POST 的数据。


指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_jaxrs2
文件名:twbs_jaxrs2.0_multipart_formdata_from_html.html