Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

439
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda