i want to upload a excel file with Angular and send it via REST API to the backend. The file is create in MS Excel as an.xlsx file. But when i try to read the excel file i got the following exception at the backend service:
org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException: No valid entries or contents found, this is not a valid OOXML (Office Open XML) file
at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.getNextEntry(ZipArchiveThresholdInputStream.java:156)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:94)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:132)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:312)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:59)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:289)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:285)
at
Caused by: java.util.zip.ZipException: Unexpected record signature: 0X2D2D2D2D
at org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.getNextZipEntry(ZipArchiveInputStream.java:296)
at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.getNextEntry(ZipArchiveThresholdInputStream.java:152)
... 86 more
Here the Angular Rest call:
onFileSelect(event): void {
const file: File = event.target.files[0];
if (file) {
const formData = new FormData();
formData.append("file", file);
this.http.post('/rest/upload', formData)
.map((response: Response) => {
let body = response.json();
return body || [];
})
.catch((error: Response) => {
return this.handleError(error);
});
}
}
And here the backend code:
@RequestMapping(value = "/rest/upload", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
@ResponseBody
public ResponseEntity<List<BaselineJson>> uploadExcel(@FormDataParam("file") InputStream excelFile) {
XSSFWorkbook workbook = null;
try {
workbook = new XSSFWorkbook(excelFile);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
....
Any help?
Please try the following headers in your call:
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
this.http.post('/rest/upload', formData, options)....
This will set the right content type, so hopefully will fix it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.