2016-04-21 12 views
-2

我正嘗試使用AngularJS和REST(澤西島)上傳excel(.xlsx)文件。多部分邊界被添加到REST收到的文件中,apache POI Library拒絕作爲無效文件,但有以下例外情況。這個問題的解決方案是什麼?REST上傳Excel文件:使用多部分邊界拒絕文件的POI庫

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) 

與邊界的InputStream爲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.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() { 
            }); 
         } 
        } ]); 

的問題是simil除了它特定於C#之外的其他鏈接。

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

+3

您應該可能將您的java代碼添加到您的問題 – 2016-04-21 10:53:29

+0

這裏是一個鏈接,顯示如何使用Web服務上傳文件...希望它可以幫助... http://howtodoinjava.com/jersey/jersey-file-upload - 例子/ – Abhishek

+1

@RC。嗨,我已經添加了所有的細節。這是我在stackoverflow上的第一個問題,對不起。 – user3187479

回答

1

註釋應該是FormDataParam而不是RequestParam。如果仍然出現錯誤,請通過將其寫入臨時位置來驗證您是否收到該文件。當文件內容爲空時,您通常會遇到此錯誤,而這種情況在您的情況下更有可能。

+0

我正在獲取邊界後的數據,您可以在圖像中看到它。我將文件在本地進行了批處理,並且驗證了邊界內的數據是相同的。 – user3187479

+0

在'fileUpload'方法中,您是否將文件寫入臨時位置,並且能夠無誤地打開文件?關於FormDataParam的錯誤,您使用的是哪一個Jersey版本?我看到https://java.net/jira/browse/JERSEY-1658。 – Adi

+0

我正在使用以下Jersey球罐。 jersey-bundle-1.19.jar jersey-core-1.19.jar jersey-multipart-1.19.jar – user3187479