在 Restlet 中上传带有描述的文件

File Upload with Description in Restlet

我需要使用 restlet上传一个文件 以及一些额外的数据。所以我创建了一个示例 html 页面,如下所示。

<html>
<body>
    <h1>*****Upload File with RESTFul WebService*****</h1>
    <form action="http://localhost:8080/test/api/streams/sample.json" method="post" enctype="multipart/form-data">


        <fieldset>
            <legend>Upload File</legend>

            <input type="file" name="fileToUpload"/><br />  
            <br /><br />
            Party ID<input type="text" name="mybody"  /><br />


            <input type="submit" name="Upload" id="Upload" value="Upload" />
        </fieldset>
    </form>
</body>

我需要从 输入字段 中读取值以及文件 data.Now 可以读取文件 content.how 我可以从中获取值吗该输入框在同一个 api 调用中。

@Post
public Representation accept(Representation entity) throws Exception {
Representation result = 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 based on the Restlet
    // FileUpload extension that will parse Restlet requests and
    // generates FileItems.
    RestletFileUpload upload = new RestletFileUpload(factory);

    // 3/ Request is parsed by the handler which generates a
    // list of FileItems
    FileItemIterator fileIterator = upload.getItemIterator(entity);

    // Process only the uploaded item called "fileToUpload"
    // and return back
    boolean found = false;
    while (fileIterator.hasNext() && !found) {
      FileItemStream fi = fileIterator.next();
      Extractor extractor = new Extractor(getContext());

      if (fi.getFieldName().equals("fileToUpload")) {
        found = true;
        // consume the stream immediately, otherwise the stream
        // will be closed.
        StringBuilder sb = new StringBuilder("media type: ");
        sb.append(fi.getContentType()).append("\n");
        sb.append("file name : ");
        sb.append(fi.getName()).append("\n");
        BufferedReader br = new BufferedReader(new InputStreamReader(fi.openStream()));
        String line = null;
        while ((line = br.readLine()) != null) {
          sb.append(line);
        }
        sb.append("\n");
        result = new StringRepresentation(sb.toString(), MediaType.TEXT_PLAIN);
      }
    }
  } else {
    setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
  }
  System.out.println("result==" + result);
}


return result;

}

首先您需要了解您的请求内容。如果您查看发送的请求(firebug 或其他内容),您将看到以下内容:

-----------------------------2003194375274723921294130757
Content-Disposition: form-data; name="fileToUpload";
filename="mysql.sql" Content-Type: application/sql 

<<YOUR FILE CONTENT>>
-----------------------------2003194375274723921294130757
Content-Disposition: form-data; name="mybody"

my value
-----------------------------2003194375274723921294130757
Content-Disposition: form-data; name="Upload"

Upload
-----------------------------2003194375274723921294130757--

如您所见,您的请求分为几个部分。这意味着您可以在服务器资源中迭代这些部分。在您提供的代码中,您只查找名称为 fileToUpload 的条目,找到后,您将打破循环。

您可以更新代码以执行完整循环并检查输入字段的值 mybody,如下所述:

while (fileIterator.hasNext()) {
    FileItemStream fi = fileIterator.next();

    if ("mybody".equals(fi.getFieldName())) {
        BufferedReader br = new BufferedReader(
                 new InputStreamReader(fi.openStream()));
        String fieldValue = null;
        if ((line = br.readLine()) != null) {
            fieldValue = line;
        }
    } else if ("fileToUpload".equals(fi.getFieldName())) {
        (...)
    }
    (...)
}

希望对您有所帮助, 蒂埃里