无法得到泽西的回应

时间:2012-12-01 15:07:35

标签: jquery jersey

我正在使用
* jquery 1.7.1
*使用ajax的multipart / form-data的jquery表单 *泽西岛1.8。
以下是我的HTML代码:

    <html>
    <head>
     <!-- <script src="jquery-1.4.2.js" type="text/javascript" charset="utf-8"></script> -->
     <script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
       <script src="js/main.js" type="text/javascript"></script>
     <script src="js/jquery.form.js" type="text/javascript"></script>
    </head>
    <body>
    <div id="output">
    </div>
    <form encType="multipart/form-data" method="POST" id="userForm">
    <label>Image file : </label>
    <input type="file" id="file1" name="file1"/>
    <input type="submit" id="btnCreate" name="btnCreate"/>
   </form>
   </body>
   </html>

以下是我的javascript代码:

  jQuery.support.cors = true;
  var rootURL = "http://localhost:8084/UserDemo/webresources/user";
  $(document).ready(function() { 
        var options = { 
              target:        "#output",   // target element(s) to be updated with server response 
             beforeSubmit:  showRequest,  // pre-submit callback 
             success:       showResponse,  // post-submit callback 
     error: errorResponse,
      url : rootURL,
      dataType :  "json"
     }; 
     $('#userForm').ajaxForm(options); 
 }); 
    function errorResponse(responseText,statusText,xhr,$form)
     {
        alert("responseText : " + responseText +" : statusText : " + statusText);
     }
     // pre-submit callback 
   function showRequest(formData, jqForm, options) { 

      var queryString = $.param(formData); 
       alert('About to submit: \n\n' + queryString); 
         return true; 
     } 

    // post-submit callback 
    function showResponse(responseText, statusText, xhr, $form)  { 

         alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    '\n\nThe output div should have already been updated with the responseText.'); 
    }

这是我的球衣网络服务代码段

        @POST
        @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML,MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_OCTET_STREAM})
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.MULTIPART_FORM_DATA })
            //public User create(@FormParam("file") InputStream  user) {
            public User create(User  user1) {
               //User user = (User)user1;
          System.out.println("creating user");
                  return new User();
            //return dao.create(user);
       }

在服务器日志中,我收到以下消息:

    Dec 01, 2012 8:12:16 PM com.sun.jersey.api.container.filter.LoggingFilter filter
    INFO: 7 * Server in-bound request
    7 > POST http://localhost:8084/UserDemo/webresources/user
     7 > accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*
    7 > dnt: 1
     7 > accept-language: en-us
    7 > content-type: multipart/form-data;  boundary=---------------------------7dc1e4101d01ba
    7 > accept-encoding: gzip, deflate
   7 > user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; FDM)
  7 > host: localhost:8084
  7 > content-length: 10122
    7 > connection: Keep-Alive
     7 > cache-control: no-cache
    7 > 

     Dec 01, 2012 8:12:16 PM com.sun.jersey.spi.container.ContainerRequest getEntity
      SEVERE: A message body reader for Java class com.qfour.User, and Java type class com.qfour.User, and MIME media type multipart/form-data;    boundary=---------------------------7dc1e4101d01ba was not found.
     The registered message body readers compatible with the MIME media type are:
     */* ->
     com.sun.jersey.core.impl.provider.entity.FormProvider
     com.sun.jersey.core.impl.provider.entity.StringProvider
     com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
     com.sun.jersey.core.impl.provider.entity.FileProvider
     com.sun.jersey.core.impl.provider.entity.InputStreamProvider
     com.sun.jersey.core.impl.provider.entity.DataSourceProvider
     com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
     com.sun.jersey.core.impl.provider.entity.ReaderProvider
     com.sun.jersey.core.impl.provider.entity.DocumentProvider
     com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
     com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
     com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
     com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
     com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
     com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
     com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
     com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General  
     com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
     com.sun.jersey.core.impl.provider.entity.EntityHolderReader  
     com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
     com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
     com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy 

   Dec 01, 2012 8:12:16 PM com.sun.jersey.api.container.filter.LoggingFilter$Adapter  finish
     INFO: 7 * Server out-bound response
     7 < 415
     7 < 

请帮助我找出问题的根本原因

提前致谢

塞特希

1 个答案:

答案 0 :(得分:1)

因为您使用的是“multipart / form-data”内容类型,所以应该使用@FormDataParam来获取表单数据:

@POST
@Consumes("multipart/form-data")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.MULTIPART_FORM_DATA })
public User create(@FormDataParam("file") InputStream  user) { 
      //analyse your user binary data    
      System.out.println("creating user");
      return new User();
}