RESTlet:如何处理multipart / form-data请求? [英] RESTlet: How to process multipart/form-data requests?

查看:356
本文介绍了RESTlet:如何处理multipart / form-data请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当它是一个多部分/表单数据请求时,如何捕获传入的@Post变量?

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

对于常规的Post请求,我会这样做:

For a regular Post request I would do:

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

但是因为它是一个multipart / form-data请求,所以输出 null

But because it is a multipart/form-data request the above outputs null

我是一个Java新手,所以要温柔:)

I'm a Java newbie so be gentle :)

PS:我对处理传入的文件,只对文本字段不感兴趣。

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

推荐答案

这是我的一种方法的粘贴( Restlet 2.0)。在这里,我有一个包含一个文件上传和其他字段的表单,因此它相当完整:

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<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.

这篇关于RESTlet:如何处理multipart / form-data请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆