Home » excel » java – How to write a RestService Post that consumes an Excel File?

java – How to write a RestService Post that consumes an Excel File?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have to program a Restful Service that Consumes a Excel File Mapps it into a Class and writes it into the DataBase.

@POST
@Path("/insertDataInDB)
@Consumes(MediaType.???)
public Response insertDataInDB(???) {
   //do Stuff
}

My first idea would be to use a ByteStream and than to interpret the Stream with Apache POI.
But here i got an Exception: “org.jboss.resteasy.spi.UnsupportedMediaTypeException”

@POST
@Path("/insertDataInDB")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void insertDataInDB(@FormDataParam("inputfile")
File inputfile) {
    //do Stuff
}

Has anyone an idea what MediaType to use and what kind of java DataType?

Or has someone a better idea?

How to&Answers:

Just for someone with the same problem, this worked for me.

@POST
@Path("/insertDataInDB")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void insertDataInDB2(@FormDataParam("inputfile")
MultipartFormDataInput inputfile) {

    Map<String, List<InputPart>> uploadForm = inputfile.getFormDataMap();
    List<InputPart> inputParts = uploadForm.get("inputfile");

    //i have only one inputPart
    InputPart inputPart = inputParts.get(0);

    try {
        InputStream inputStream = inputPart.getBody(InputStream.class, null);

        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = workbook.getSheetAt(0);
        System.out.println(sheet.getSheetName());

    } catch (IOException e) {
        e.printStackTrace();
    }
}