Javascript Form Post with Jersey MultiFormData - Null Pointer Exception - Stack Overflow

I'm trying to post some form data using javascript against a Jersey Resource. Here is the javascri

I'm trying to post some form data using javascript against a Jersey Resource. Here is the javascript:

            var form = document.getElementById('form');
            var formdata = new FormData(form);

            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("xmlTextBox").innerHTML=xmlhttp.responseText;
                }
              }
            xmlhttp.open("POST", "PostXml", true);
            xmlhttp.setRequestHeader('Content-Type', 'multipart/form-data');
            xmlhttp.setRequestHeader("Content-length", formdata.length);
            xmlhttp.setRequestHeader("Connection", "close");
            xmlhttp.send(formdata);

The Jersey Resource look like this:

@Path("/Resource")
public class MyClass {

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_XML)
public String postXML(@FormDataParam("param1") String param1, 
@FormDataParam("param2") String param2){

return "test";

}

The real version includes more params and full code, but the annotations are the same. This produces the following exception when running through tomcat:

java.lang.NullPointerException
    at .sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParameters(MultiPartReaderClientSide.java:227)
    at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:154)
    at .sun.jersey.multipart.impl.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:80)
    at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:144)
    at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:82)
    at .sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488)
    at .sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:552)

From looking at the source that produced the exception it looks like the param isn't ing through:

224     for (final String parameterName : parameters) {
225         String parameterValue = mediaType.getParameters().get(parameterName);
226
227         if (parameterValue.startsWith("\"")) {
228             parameterValue = parameterValue.substring(1, parameterValue.length() - 1);
229             unquotedParams.put(parameterName, parameterValue);
230         }
231     }

I;ve used firebug to put a trace on and the name / values are ing through differently when using javascript pared to a straight HTML post. In the trace for the HTML post the content type is returned in an upload stream:

Request Headers From Upload Stream
Content-Length  1756
Content-Type    multipart/form-data; boundary=---------------------------1523409566516443041527622966

But the javascript seems to be just a standard post or something? Any ideas how I replicate the multiformdata post in javascript??

Any ideas as it looks like i'm passing things through OK? I've also tried this using a normal HTML form post and it works fine, so must be related to the javascript.

I'm trying to post some form data using javascript against a Jersey Resource. Here is the javascript:

            var form = document.getElementById('form');
            var formdata = new FormData(form);

            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("xmlTextBox").innerHTML=xmlhttp.responseText;
                }
              }
            xmlhttp.open("POST", "PostXml", true);
            xmlhttp.setRequestHeader('Content-Type', 'multipart/form-data');
            xmlhttp.setRequestHeader("Content-length", formdata.length);
            xmlhttp.setRequestHeader("Connection", "close");
            xmlhttp.send(formdata);

The Jersey Resource look like this:

@Path("/Resource")
public class MyClass {

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_XML)
public String postXML(@FormDataParam("param1") String param1, 
@FormDataParam("param2") String param2){

return "test";

}

The real version includes more params and full code, but the annotations are the same. This produces the following exception when running through tomcat:

java.lang.NullPointerException
    at .sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParameters(MultiPartReaderClientSide.java:227)
    at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:154)
    at .sun.jersey.multipart.impl.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:80)
    at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:144)
    at .sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:82)
    at .sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488)
    at .sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:552)

From looking at the source that produced the exception it looks like the param isn't ing through:

224     for (final String parameterName : parameters) {
225         String parameterValue = mediaType.getParameters().get(parameterName);
226
227         if (parameterValue.startsWith("\"")) {
228             parameterValue = parameterValue.substring(1, parameterValue.length() - 1);
229             unquotedParams.put(parameterName, parameterValue);
230         }
231     }

I;ve used firebug to put a trace on and the name / values are ing through differently when using javascript pared to a straight HTML post. In the trace for the HTML post the content type is returned in an upload stream:

Request Headers From Upload Stream
Content-Length  1756
Content-Type    multipart/form-data; boundary=---------------------------1523409566516443041527622966

But the javascript seems to be just a standard post or something? Any ideas how I replicate the multiformdata post in javascript??

Any ideas as it looks like i'm passing things through OK? I've also tried this using a normal HTML form post and it works fine, so must be related to the javascript.

Share Improve this question asked May 13, 2013 at 21:46 thomascrabsthomascrabs 731 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Remove those setRequestHeader

var form = document.getElementById('form');
var formdata = new FormData(form);

var xmlhttp=new XMLHttpRequest();
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("xmlTextBox").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST", "PostXml", true);
xmlhttp.send(formdata);

http://jsfiddle/8NWB7/ working
http://jsfiddle/8NWB7/1/ not working

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745351992a4623888.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信