JSP文件上传返回空的输入流以进行文件上传
我正在使用AJAX调用进行JSP文件上传,并且能够在HttpservletRequest中击中Java控制器,我会看到收到的Multipart文件,但是当我进行request.getInputStream.getInputStream.readallBytes()
时,我得到了一个空字节数组
JavaScript中的Ajax调用
function saveFileUpload() {
var data = new FormData()
data.append("file", document.getElementById(fileName).files[0])
$.ajax({
type: 'POST',
data: data,
url: pageContext + "/upload",
processData: false,
contentType: false,
success: function(data) {},
error: function(e) {}
});
}
}
在Java控制器中的
@RequestMapping(value = {"/upload"}, method = RequestMethod.POST)
public void fileUpload (
HttpServletRequest request, HttpServletResponse response){
byte[] arr = request.getInputStream().readAllBytes();
System.out.println(arr.length);
}
上述代码prints arr.length
为0。有人可以告诉我这个问题的原因吗?
I am working on a jsp file upload using ajax call and I am able to hit the java controller in the HttpServletRequest I see the multipart file received but when I do request.getInputStream.readAllBytes()
, I get an empty byte array
The ajax call in javascript
function saveFileUpload() {
var data = new FormData()
data.append("file", document.getElementById(fileName).files[0])
$.ajax({
type: 'POST',
data: data,
url: pageContext + "/upload",
processData: false,
contentType: false,
success: function(data) {},
error: function(e) {}
});
}
}
In Java controller
@RequestMapping(value = {"/upload"}, method = RequestMethod.POST)
public void fileUpload (
HttpServletRequest request, HttpServletResponse response){
byte[] arr = request.getInputStream().readAllBytes();
System.out.println(arr.length);
}
The above code prints arr.length
as 0. Can someone tell me the reason for this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您正在使用
springboot
,并且您的应用程序支持multipart/form-data
。版本 3.0 Servlet API本身支持它。JQuery的Ajax:
I assume that you are using
Springboot
and your application supportsmultipart/form-data
. After version 3.0 the Servlet API natively support it.JQuery's Ajax: