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

The*_*der 3 java rest jquery jax-rs jersey

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

阿贾克斯代码:

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) {

    }
});
Run Code Online (Sandbox Code Playgroud)

爪哇代码:

@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);
Run Code Online (Sandbox Code Playgroud)

我已经尝试了一天。我确实设法通过直接提交 的形式来接收文件enctype="multipart/form-data",但我需要在 ajax 中完成。如果我查看我的 tomcat 的日志,它在访问api/rootpath/childpath. 我认为问题是由于与原始内容类型相比收到的内容类型不同。我尝试将其更改MediaType.为“multipart/form-data”等,但失败了。

The*_*der 5

好的,终于知道我的错误了。我希望这个答案对以后希望在 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) {

    }
});
Run Code Online (Sandbox Code Playgroud)

爪哇代码:

@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();
}
Run Code Online (Sandbox Code Playgroud)

  • 你可以只用一个 `byte[]` 作为参数而不是 `InputStream` (2认同)