1

Is it possible with spring boot and for example apache poi to get POST request json format with excel file inside? for example :

POST api/testrequest/ 
Content-Type: application/json  //(maybe another type?)
{
    "searchKey": "test1",
    "searchValue": file.excel
}

And fetch it to Object?

Now I did something like this :

Controller method :

@PostMapping(
      value = "excelentity",
      consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE})
  public String getExcelAndParseItToEntity(@RequestBody ExcelTemplate file) {
    String fileName = file.getFile().getOriginalFilename();
    log.info(fileName);
    
    return "test case";
  }

And Java Object :

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ExcelTemplate {
    private MultipartFile file;
    private String name;
}

But it doesn't work

4
  • Do you want the content of the excel file there? I would suggest to encode it using something like base64, then just read it from that json like any other string. The client of course has to create the json request accordingly. Commented Jul 21, 2022 at 8:53
  • Yes, i need opportunity to content excel file in json. without encoding/decoding to base64 if it is possible. Commented Jul 21, 2022 at 9:04
  • It's technically not possible without encoding, because json is text and excel is a binary format. But see the example I posted, encoding/decoding it is really simple Commented Jul 21, 2022 at 9:07
  • 1
    Thank you for your answer, i thought it is possible Commented Jul 21, 2022 at 9:09

1 Answer 1

3

You can't include it directly but you can encode it as string.

The client sending that json request to your spring boot application can encode the file to base64 and include the resulting string in the json as text.

Something like that:

byte[] fileContent = readExcelFile(file); // Use InputStreams to read all bytes of the excel

String encodedFile = Base64.getEncoder().encodeToString(fileContent);

doRequest(encodedFile); // Do request, set 'encodedFile' as value of 'searchValue' in json

Your json would then look something like that:

{
    "searchKey": "test1",
    "searchValue": "SGVsbG8gU3RhY2tPdmVyZmxvdyE=..."
}

In your spring boot application simply decode it to bytes again and save it as file or use it directly with a ByteArrayInputStream.

var searchValue = getFromJson(json); // get the value from your json / dto

byte[] decodedBytes = Base64.getDecoder().decode(searchValue);

// Save to a file then use it
saveToFile(decodedBytes); 
// Or
// Use it directly as InputStream without saving it to file
var inputStream = new ByteArrayInputStream(decodedBytes); 

See this baeldung tutorial for more information on how to use Base64: https://www.baeldung.com/java-base64-encode-and-decode
And this one for the ByteArrayInputStream: https://www.baeldung.com/convert-byte-array-to-input-stream

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.