gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:44:22 27 4
gpt4 key购买 nike

我正在尝试使用 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”等,但失败了。

最佳答案

好吧,终于弄清楚我的错误了。我希望这个答案对以后希望在 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();
}

关于java - 尝试在 Jax-RS Jersey 中发送 FormData() 时的 415 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28670886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com