在 JAX-RS 2.0 中將資源配置成接收 HTML 表單提交中的 multipart/form-data 組件

必須以 POST 方法和 "multipart/form-data" 動作,來配置傳輸檔案資料的 HTML 表單。藉由實作 IBM Java™ API for RESTful Web Services (JAX-RS) 來接受此資料的 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 實作會根據這個標頭,嘗試自動解碼該組件的有效負載。

結果

您已接收和回應 HTTP POST 中具有 multipart/form-data Content-Type 的資料,您的作法是容許 IBM JAX-RS 實作為您分割和自動解碼組件,並接收仍然編碼的組件以自行處理。


指示主題類型的圖示 作業主題

檔名:twlp_jaxrs_multipart_formdata_from_html.html