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

172
Visualizações
File Upload Java Spring

I am trying to upload a file in chunks in Java.
My chunk upload function:

@Async
private Future<String> sendChunk(byte[] chunk, int start, int end, int numberOfChunks, String name, String sessionId) throws IOException {

    LinkedMultiValueMap<String, Object> requestParams = new LinkedMultiValueMap<>();
    requestParams.add("data", new String(java.util.Base64.getEncoder().encode(chunk), "UTF-8"));
    requestParams.add("start", start);
    requestParams.add("end", end);
    requestParams.add("numberOfChunks", numberOfChunks);
    requestParams.add("fileName", name);
    requestParams.add("session", sessionId);

    HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestParams);
    UploadResponse uploadResponse = restTemplate.exchange(fileServiceUrl, HttpMethod.POST, requestEntity, new ParameterizedTypeReference<UploadResponse>(){}).getBody();
    return new AsyncResult<>(uploadResponse.getSessionId());
}

This is how the File-Service-API looks like:

@RequestMapping(value = "/simpleUploader", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<UploadResponse> simpleUploader(
    @RequestParam("data") String data,
    @RequestParam("session") String sessionId,
    @RequestParam("start") int start,
    @RequestParam("end") int end,
    @RequestParam("numberOfChunks") int numberOfChunks,
    @RequestParam("fileName") String fileName) throws IOException {
    ....
}

Now, if I try to upload a Chunk the File-Service responds with a 400 - Bad Request.
Stack-Trace:

org.springframework.web.client.HttpClientErrorException: 400 null at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:526)

It will be rejected from Spring even before it gets to the API-Function.
I am already uploading the same way, but in Javascript. From Javascript everything is working as it should.
What am I missing?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The only problem I had with my code was, that "sessionId" will be null the first time I am calling the File-Service API.
When I set a value of a Map Entry to null, the whole Entry doesn't get passed. I didnt knew that.
My fix was to add a defaultValue to @RequestParam("session") String sessionId so that the method declaration looks like that:

@RequestMapping(value = "/simpleUploader", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<UploadResponse> simpleUploader(
    ...
    @RequestParam(value = "session", defaultValue = "") String sessionId,
    ... ) throws IOException {
    ....
}
about 4 years ago · Santiago Trujillo Relatório

0

The @Async has two limitations.

  • it must be applied to public methods only.
  • self-invocation – calling the async method from within the same class – won’t work

http://www.baeldung.com/spring-async

Try changing private to public

@Async public Future<String> sendChunk(byte[] chunk, int start, int end, int numberOfChunks, String name, String sessionId) throws IOException {

about 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