Java Spring boot convert Multipart file to a file
In this article, we will learn how can we convert a Multipart file into a file. Sometimes, we need to convert a Multipart file to a file for some reason, In my case, when I was trying to covert xl data to a model list that time I needed to convert a multipart file to a file because in the Poiji package it doesn't support Multipart file format.
Now we will show with controller how can we convert a Multipart file into a file.
CODE:
/**
* @aurthor fahad
* @date 08-06-2022
* @description insert data from xl file.
* @param fileName
* @param contractId
* @return
*/
@PostMapping("/importDataFromXlfile")
@ResponseBody
public CommonResult<String> importFromXl(@RequestParam MultipartFile fileName, @RequestParam Integer contractId) {
try {
//this part is convert code you can ignore other code
File convFile = new File(fileName.getOriginalFilename());
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(fileName.getBytes());
fos.close();
List<PmsContractArrival> invoices = Poiji.fromExcel(convFile, PmsContractArrival.class);
for (PmsContractArrival pca : invoices) {
PmsContractArrival pmsContractArrival = new PmsContractArrival();
pmsContractArrival.setContractId(contractId);
pmsContractArrival.setCabinetNumber(pca.getCabinetNumber());
pmsContractArrival.setFurQuantity(pca.getFurQuantity());
pmsContractArrival.setBoardQuantity(pca.getBoardQuantity());
pmsContractArrival.setGrossWeight(pca.getGrossWeight());
pmsContractArrival.setFurWeight(pca.getFurWeight());
if (pca.getHalalString() != "" || pca.getHalalString() != null) {
pmsContractArrival.setHalal(true);
} else {
pmsContractArrival.setHalal(false);
}
if (!pmsContractArrivalService.save(pmsContractArrival)) {
return CommonResult.failed("failed");
}
}
return CommonResult.success("success");
} catch (Exception e) {
return CommonResult.failed("failed");
}
}
Note: This is my own project's code, you can just use convert code into your project and ignore other code. If you want a full tutorial you can let me know in the comment section. I will write an article with full code. This is just about converting.
No comments