Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

437
Views
Jsp File upload returns empty Input Stream for file upload

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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) {}
    });
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!