java RESTlet:如何处理多部分/表单数据请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/996819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RESTlet: How to process multipart/form-data requests?
提问by Alex
How do you catch incoming @Post variables when it is a multipart/form-data request?
当它是一个 multipart/form-data 请求时,你如何捕获传入的 @Post 变量?
For a regular Post request I would do:
对于常规的 Post 请求,我会这样做:
@Post
public void postExample(Representation entity) throws Exception{
Form form = new Form(entity);
System.out.println(form.getFirstValue("something"));
}
But because it is a multipart/form-data request the above outputs null
但是因为它是一个 multipart/form-data 请求,所以上面的输出为null
I'm a Java newbie so be gentle :)
我是 Java 新手所以要温柔:)
PS: I'm not interested in processing the incoming files, just the text fields.
PS:我对处理传入的文件不感兴趣,只对文本字段感兴趣。
回答by Carles Barrobés
This is a paste from one of my methods (Restlet 2.0). Here I have a form that includes one file upload plus other fields, therefore it is rather complete:
这是我的一种方法(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;
}
I'll end up refactoring it to something more generic, but this is what I have by now.
我最终会将它重构为更通用的东西,但这就是我现在所拥有的。
回答by Alex
to pack it on one line, in you Restlet Resource class :
将其打包在一行中,在您的 Restlet Resource 类中:
Iterator it = new RestletFileUpload(new DiskFileItemFactory()).parseRequest(getRequest()).iterator();
Iterator it = new RestletFileUpload(new DiskFileItemFactory()).parseRequest(getRequest()).iterator();
Then in your loop through your items, you can test whether they are or not fileItems with the method: isFormField().
然后在您的项目循环中,您可以使用以下方法测试它们是否是 fileItems:isFormField()。
Testing if a fileItem is a formField... makes sens ? ;)
but it works.
测试 fileItem 是否是 formField... 使 sens ?;)
但它的工作原理。
Good luck.
祝你好运。
回答by Alex
To answer my own question, this is not currently feasible in version 1.2
要回答我自己的问题,这在 1.2 版中目前不可行

