JSP文件上传返回空的输入流以进行文件上传

发布于 2025-01-21 11:01:55 字数 958 浏览 0 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红衣飘飘貌似仙 2025-01-28 11:01:55

我假设您正在使用springboot,并且您的应用程序支持multipart/form-data。版本 3.0 Servlet API本身支持它。

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void fileUpload(@RequestParam("file") MultipartFile file) throws IOException 
{
    if (!file.isEmpty()) 
   {
        byte[] bytes = file.getBytes();
        // and so on
    }
}

JQuery的Ajax:

function saveFileUpload() {
    let 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) {}
    });
}

I assume that you are using Springboot and your application supports multipart/form-data. After version 3.0 the Servlet API natively support it.

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void fileUpload(@RequestParam("file") MultipartFile file) throws IOException 
{
    if (!file.isEmpty()) 
   {
        byte[] bytes = file.getBytes();
        // and so on
    }
}

JQuery's Ajax:

function saveFileUpload() {
    let 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) {}
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文