RESTlet: how to handle multipart / form-data requests?

How do you catch @Post input variables when it is a multipart / form-data request?

For a regular mail request, I would do:

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

But since this is a multipart / form-data request, the parent outputs are null

I am new to Java, so be careful :)

PS: I'm not interested in processing incoming files, just text fields.

+3
source share
3 answers

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, so it’s pretty complete:

 @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 will return to transforming it into something more general, but that is what I have.

+7
source

to pack it on one line in your Restore resource class:

Iterator it = new RestletFileUpload (new DiskFileItemFactory ()). parseRequest (getRequest ()). iterator ();

Then in your loop, through your objects, you can check whether they are files or not, using the: isFormField () method.

Testing if fileItem is a formField ... does it make sense ?;)
but it works.

Good luck.

+1
source

To answer my own question, this is currently not possible in version 1.2

0
source

Source: https://habr.com/ru/post/1496307/


All Articles