我在java中面临的问题是,当我通过多部分的JSON发出请求时,我无法从服务器获得响应。
这是最需要的 JavaScript block :
$.ajaxFileUpload({ url: "/iview",
secureuri:false,
fileElementsId:fileIds,
dataType:entity.config.dataType?entity.config.dataType:"text",
data:{appmode:entity.config.appmode,
json:requestObject,"__RequestType":"ajax"},
success: function (data, status){
for(var ent in fileIds){
entityObject[ent].object = document.getElementById(fileIds[ent]);
entityObject[ent].object.id = fileIds[ent];
}
requestResponse(data);
},
error: function (data, status, e){
Cyberoam.removeOverLay();
Cyberoam.messageBox({message:reportlabels.ConnectionLostMsg,autoClose:true});
},timeout:30*60*1000
}
);
下面是发送响应的 servlet 代码:
PrintWriter out =response.getWriter();
response.setContentType("text/plain");
out.println(new String(("status").toString().getBytes("UTF-8")));
out.close();
所以请帮忙获取 JSON 多部分请求的响应?
提前致谢。
最佳答案
你到底明白它的作用吗?
out.println(new String(("status").toString().getBytes("UTF-8")));
让我们重写它,以便我们可以查看不同的部分。
String s1 = "status";
String s2 = s1.toString();
byte[] bytes = s2.getBytes("UTF-8");
String s2 = new String(bytes);
out.println(s2);
这就是正在发生的事情:
- 您有一个包含“status”的字符串,并对其调用
toString()
。这没有任何作用。 - 然后将字符串转换为字节数组。字节数组将包含以 UTF-8 编码的字符串内容。
- 然后将字节解释回字符串。您没有指定字符编码,因此使用系统的默认字符编码,可能是也可能不是 UTF-8。
- 然后将结果(可能包含也可能不包含“status”,具体取决于系统的默认字符编码)打印到
out
。
这很可能不会达到您想要的效果。你可以这样写:
out.println("status");
如果这就是你想要的。
从 Java 代码中准确找出应该发送什么响应,并编写代码以正确的格式发送响应。
关于java - 通过 JSON 的多部分请求没有得到响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16164612/