我正嘗試使用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)
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#之外的其他鏈接。
您應該可能將您的java代碼添加到您的問題 – 2016-04-21 10:53:29
這裏是一個鏈接,顯示如何使用Web服務上傳文件...希望它可以幫助... http://howtodoinjava.com/jersey/jersey-file-upload - 例子/ – Abhishek
@RC。嗨,我已經添加了所有的細節。這是我在stackoverflow上的第一個問題,對不起。 – user3187479