415尝试在Jax-RS球衣中发送FormData()时的状态 [英] 415 Status when trying to send FormData() in Jax-RS jersey

查看:160
本文介绍了415尝试在Jax-RS球衣中发送FormData()时的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send files which is appended to FormData using jquery ajax. After referring some of mozilla and IBM's documents, I've came up with following.

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等,但失败了。

I've been trying this for a day. I do manage to receive the file by directly submitting the form of enctype="multipart/form-data", but I need to do it in ajax. If I look at my tomcat's log, it always gives me 415 status code when accessing api/rootpath/childpath. I think the problem is due to different content type received when comparing with original content type. I tried changing the MediaType. to "multipart/form-data" etc, but it failed.

推荐答案

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

Ok, finally figured out my mistake. I hope this answer will be greatly helpful for future visitos who wish to upload files using ajax in JAX-RS

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();
}

这篇关于415尝试在Jax-RS球衣中发送FormData()时的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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