RESTlet: How to process multipart/form-data requests?

前端 未结 3 1216
青春惊慌失措
青春惊慌失措 2020-12-18 11:38

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

For a regular Post request I would do:

@Post
public void postExam         


        
3条回答
  •  没有蜡笔的小新
    2020-12-18 11:48

    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:

    @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 items;
                try {
                    // 3/ Request is parsed by the handler which generates a list of FileItems
                    items = upload.parseRequest(getRequest());
    
                    Map props = new HashMap();
                    File file = null;
                    String filename = null;
    
                    for (final Iterator 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.

提交回复
热议问题