I am facing an issue while attempting to upload an excel (.xlsx) file using AngularJS and REST (Jersey). The multipart boundary is being added to the file received in REST, causing the apache POI Library to reject it as an invalid file with the following exception. Is there a solution to this problem?
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)
The inputstream with boundary can be found https://i.sstatic.net/8Sk2B.jpg
Java Snippet:
@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("Can't open workbook", e);
e.printStackTrace();
}
.....
Angular snippet:
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() {
});
}
} ]);
This issue is somewhat similar to one discussed in the following link, though specific to C#.