简体   繁体   English

Angular excel 文件上传抛出 NotOfficeXmlFileException

[英]Angular excel file upload throws NotOfficeXmlFileException

i want to upload a excel file with Angular and send it via REST API to the backend.我想用 Angular 上传 excel 文件并通过 REST ZDB974238714CA8DE634A7CE1D8 发送到后端。 The file is create in MS Excel as an.xlsx file.该文件在 MS Excel 中创建为 .xlsx 文件。 But when i try to read the excel file i got the following exception at the backend service:但是当我尝试读取 excel 文件时,我在后端服务中遇到了以下异常:

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:这里 Angular Rest 调用:

     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.这将设置正确的内容类型,因此希望能够修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM