REST上载Excel文件:POI库使用Multipart Boundary拒绝该文件

时间:2016-04-21 10:50:33

标签: javascript java angularjs rest apache-poi

我正在尝试使用AngularJS和REST(Jersey)上传excel(.xlsx)文件。多部分边界被添加到REST中接收的文件中,apache POI Library拒绝将其作为无效文件,但有以下异常。这个问题的解决方案是什么?

org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:254)
    at com.bt.ngwfmt.framework.rest.FileUploadHandler.fileUpload(FileUploadHandler.java:128)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

    Truncated. see log file for complete stacktrace
Caused By: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
    at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:203)
    at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:673)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:274)
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:254)
    Truncated. see log file for complete stacktrace 



    When I add @FormDataParam instead of @RequestParam I get the following   exception

    java.lang.NullPointerException
at   com.sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParamete rs(MultiPartReaderClientSide.java:245)
at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:172)
at com.sun.jersey.multipart.impl.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:80)
at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:158)
at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:85)

带边界的输入流是enter image description here

Java代码段:

@POST
@Path("/uploadFile")

@Consumes(MediaType.MULTIPART_FORM_DATA)

@NoCache
public String fileUpload(@RequestParam("file") InputStream inputStream) {

    XSSFWorkbook myWorkBook = null;

    XSSFSheet mySheet;

    try {
        myWorkBook = new XSSFWorkbook(inputStream); //The exception is thrown at this line)
    } catch (IOException e) {
        logger.error("Cant open workbook", e);
        e.printStackTrace();
    }

.....

Angular片段:

    angular.module(appName).controller('fileUploadController',
    [ '$scope', 'fileUpload', function($scope, fileUpload) {

        $scope.uploadFile = function() {
            var file = $scope.myFile;
            console.log('file is ');
            console.dir(file);
            var uploadUrl = "myurl";
            fileUpload.uploadFileToUrl(file, uploadUrl);
        };

    } ]);

angular.module(appName).directive('fileModel', [ '$parse', function($parse) {
return {
    restrict : 'A',
    link : function(scope, element, attrs) {
        var model = $parse(attrs.fileModel);
        var modelSetter = model.assign;

        element.bind('change', function() {
            scope.$apply(function() {
                modelSetter(scope, element[0].files[0]);
            });
        });
    }
};
} ]);

angular
    .module(appName)
    .service(
            'fileUpload',
            [
                    '$http',
                    function($http) {
                        this.uploadFileToUrl = function(file, uploadUrl) {
                            var fd = new FormData();
                            //
                            fd.append('file', file);
                            $http
                                    .post(
                                            uploadUrl,
                                            fd,
                                            {
                                                transformRequest :     angular.identity,
                                                headers : {
                                                    'Content-Type' : "multipart/form-data" //"multipart/form-data"
                                                }
                                            }).success(function() {
                                    }).error(function() {
                                    });
                        }
                    } ]);

问题类似于以下链接中的问题,除了它特定于C#。

https://salesforce.stackexchange.com/questions/42620/uploading-excel-file-as-rest-post-certain-content-types-corrupted-when-saved

1 个答案:

答案 0 :(得分:1)

注释应为FormDataParam而不是RequestParam。如果仍然出现错误,请通过将文件写入临时位置来验证您是否收到该文件。当文件内容为空时,通常会出现此错误,这种情况更可能发生在您的情况下。