开发者

Is it possible to post a variable length form with a file to a restful web service?

I am trying to create a restful web service that will email data from a form. The form can have a variable number of fields and will include a file. I tried using a MultivaluedMap to hold the form entries due to the variable number of fields but if I add a FormParam for the file, the MultivaluedMap no longer gets the entries.

Here is the code I was trying to make:

@POST
@Path("/formEmailerAttachment/{formGuid}")
public Response emailFormAttachment(@PathParam("Guid") long configId, MultivaluedMap<String, String> values, @FormParam("file") File file){
    Response.ResponseBuilder builder = null;
    Form form = new Form();
    form.setValues(value开发者_开发问答s);
    try{
        if (values.isEmpty())
            throw new NullPointerException();
        builder = Response.ok();
        Iterator iterator = form.getValues().entrySet().iterator();
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append("<h3>Success</h3><br>file = " + file.getName());
        while (iterator.hasNext()){
            Map.Entry pairs = (Map.Entry)iterator.next();
            sBuilder.append(pairs.getKey() + " = " + pairs.getValue() + "<br>");
        }
        builder.entity(sBuilder.toString());
    } catch(Exception e) {
        builder = Response.serverError();
        builder.entity("<h3>Some Error Occured</h3><br>" + e.getMessage());
    }

    return builder.build();
}

The code results in a NullPointerError. Is it possible to do something along these lines or would I have to make a separate service to handle the file?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜