415尝试在Jax-RS球衣中发送FormData()时的状态

时间:2015-02-23 09:50:29

标签: java jquery rest jersey jax-rs

我正在尝试使用jquery ajax发送附加到FormData的文件。在介绍了一些mozilla和IBM的文档之后,我想出了以下内容。

ajax代码:

var sessionId = $.cookie("referenceId");
var myFormData = { sessionId: sessionId,
                    cipherData: cipherData,   // Encrypted xml data
                    payslip: document.getElementById('payslip').files[0]};
var formData = new FormData();
for (var key in myFormData) {
    console.log(key, myFormData[key]);
    formData.append(key, myFormData[key]);
}
$.ajax({
    url : 'api/rootpath/childpath',
    type : 'POST',
    processData: false,
    contentType: false,    // Here the contentType is set to false, so what should I put at @Consumes in java code
    data : {
        formData: formData
    },
    success : function(data,status) {
        alert('success');
    },
    failure : function(data) {

    }
});

Java代码:

@POST
@Path("/childpath")
@Consumes(MediaType.MULTIPART_FORM_DATA)  // I tried removing it, changing it to various formats, but none worked
public Response createLoan(@FormParam("cipherData") String cipherData,@FormParam("sessionId") String sessionId,
                           @FormParam("payslip") File payslip);

我一直在尝试这一天。我确实设法通过直接提交enctype="multipart/form-data"的形式来接收文件,但我需要在ajax中执行此操作。如果我查看我的tomcat日志,它在访问api/rootpath/childpath时总会给我415状态代码。我认为问题是由于与原始内容类型进行比较时收到的内容类型不同。我尝试将MediaType.更改为“multipart / form-data”等,但它失败了。

3 个答案:

答案 0 :(得分:4)

好的,终于找到了我的错误。我希望这个答案对于希望在JAX-RS中使用ajax上传文件的未来访问者非常有帮助

Ajax代码:

var myFormData = { sessionId: sessionId,
                    cipherData: cipherData,   // encrypted xmlData
                    payslip: document.getElementById('payslip').files[0]};
var formData = new FormData();
for (var key in myFormData) {   // Just to make sure everything set correctly, I would recomment to do like this
    console.log(key, myFormData[key]);
    formData.append(key, myFormData[key]);
}
$.ajax({
    url : 'api/rootpath/childpath',
    type : 'POST',
    data : formData,    // Do not send it as - data: { formData: formData }
    processData: false, // Tell jquery to don't process the data
    contentType: false, // If you do not set it as false, most probably you would get 400 or 415 error
    success : function(data,status) {
        alert('success');
    },
    failure : function(data) {

    }
});

Java代码:

@POST
@Path("/childpath")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response createLoan(
         @FormDataParam("cipherData") String cipherData,  // encrypted xml data
         @FormDataParam("sessionId") String sessionId,   // sessionId (you can also get it through httpHeader)
         @FormDataParam("payslip") InputStream payslipS,  // this is your file
         @FormDataParam("payslip") FormDataContentDisposition payslipD ) {   // this is your file details like file name and file type

// If you need to store the file in DB as blob
byte[] byte = IOUtils.toByteArray(payslipS);   // IOUtils is org.apache.commons.io.IOUtils (you need to add its dependency in pom.xml or build.gradle)
// Now store the byte[] in Blob field of DB
return Response.status(200).entity('success').build();
}

答案 1 :(得分:2)

后端

415在这种情况下不受支持很可能意味着没有可用于处理多部分的提供者

多方支持不是标准化的。您需要添加一个特定于实现的依赖项,并且可能(取决于实现),配置支持,并使用特定于实现的mutlipart注释(并且它不是@FormaParam,这是x-www-form-urlencoded数据),或其他一些mulipart对象。

不同的支持文档和示例

前端

一堆不同的例子可以是found here

答案 2 :(得分:0)

我认为您应该将ajax代码段中的contentType更改为false,因为这是Java常量"multipart/form-data"的值。