2009-06-15 124 views
3

當它是多部分/表單數據請求時,您如何捕獲傳入的@Post變量?RESTlet:如何處理多部分/表單數據請求?

對於常規發佈採購信息,我會做:

@Post 
public void postExample(Representation entity) throws Exception{ 
    Form form = new Form(entity); 
    System.out.println(form.getFirstValue("something")); 
} 

但因爲它是一個多/表單數據請求就上述輸出

我是一個Java新手,所以要溫柔:)

PS:我不感興趣處理傳入的文件,只是文本字段。

+0

你可以將表單數據添加爲html嗎? – Clint 2009-06-18 05:07:49

回答

0

要回答我的問題,這是不是目前的版本是可行的1.2

1

收拾它在同一行,在你的Restlet資源類:

迭代它=新RestletFileUpload(新DiskFileItemFactory()) .parseRequest(Request()方法)的迭代器();

然後在您的項目循環中,您可以使用isFormField()方法測試它們是否爲fileItems。

測試fileItem是否爲formField ...使sens? ;)
但它的工作。

祝你好運。

7

這是來自我的一個方法(Restlet 2.0)的粘貼。在這裏,我有一個包含一個文件上傳再加上其他領域的一種形式,因此它是相當完整:

@Post 
public Representation createTransaction(Representation entity) { 
    Representation rep = null; 
    if (entity != null) { 
     if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) { 
      // 1/ Create a factory for disk-based file items 
      DiskFileItemFactory factory = new DiskFileItemFactory(); 
      factory.setSizeThreshold(1000240); 

      // 2/ Create a new file upload handler 
      RestletFileUpload upload = new RestletFileUpload(factory); 
      List<FileItem> items; 
      try { 
       // 3/ Request is parsed by the handler which generates a list of FileItems 
       items = upload.parseRequest(getRequest()); 

       Map<String, String> props = new HashMap<String, String>(); 
       File file = null; 
       String filename = null; 

       for (final Iterator<FileItem> it = items.iterator(); it.hasNext();) { 
        FileItem fi = it.next(); 
        String name = fi.getName(); 
        if (name == null) { 
         props.put(fi.getFieldName(), new String(fi.get(), "UTF-8")); 
        } else { 
         String tempDir = System.getProperty("java.io.tmpdir"); 
         file = new File(tempDir + File.separator + "file.txt"); 
         filename = name; 
         fi.getInputStream(); 
         fi.write(file); 
        } 
       } 

       // [...] my processing code 

       String redirectUrl = ...; // address of newly created resource 
       getResponse().redirectSeeOther(redirectUrl); 
      } catch (Exception e) { 
       // The message of all thrown exception is sent back to 
       // client as simple plain text 
       getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 
       e.printStackTrace(); 
       rep = new StringRepresentation(e.getMessage(), MediaType.TEXT_PLAIN); 
      } 
     } else { 
      // other format != multipart form data 
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 
      rep = new StringRepresentation("Multipart/form-data required", MediaType.TEXT_PLAIN); 
     } 
    } else { 
     // POST request with no entity. 
     getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 
     rep = new StringRepresentation("Error", MediaType.TEXT_PLAIN); 
    } 

    return rep; 
} 

我將最終它重構到更多的東西通用的,但是這是我到現在。

+0

但是您是否需要Post請求的註釋,比如@Post(「form」)或者其他?我得到一個405返回做一些非常相似的東西 – adhanlon 2010-12-01 22:23:01

相關問題