AngularJS CXF JAXRS文件上传

时间:2015-04-16 16:45:21

标签: angularjs file-upload cxf

我正在使用JAXRS方法上传AngularJS客户端的文件。我按照此处找到的示例获取AngularJS代码https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs。在服务器端,找到了该方法,但我遇到了NullPointerException。我将在这里发布堆栈跟踪。

我的CXF JAXRS界面:

@Path("/images")
public interface LinkableImages {

    @POST
    @Path("/upload")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public List<Link> uploadFile(@Multipart(value = "userId", type = "application/json")Long userId, 
            @Multipart(value = "folder", type = "application/json") String foldername, 
            @Multipart(value="file") Attachment attachment) throws Exception;
}

我有以下AngularJS客户端上传文件:

<div data-ng-controller = "uploadCtrl">
     <input type="file" file-model="myFile"/>
     <button data-ng-click="uploadFile()">upload me</button>      
    </div>

HTML文件输入的AngularJS代码:

app.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
        console.log("file about to be uploaded is "+ file);
        var fd = new FormData();

        fd.append('file', file);
        fd.append('userId', 3);
        fd.append('folder', '')
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': 'undefined'}
        })
        .success(function(){
            console.log("done post");
        })
        .error(function(data){
            console.log("failed to post "+data);
        });
    }


    }

}]);

app.controller('uploadCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){

    $scope.uploadFile = function(){
        var file = $scope.myFile;
        console.log('file is ' + JSON.stringify(file) + ' file is '+ file);
        var uploadUrl = "http://localhost:8080/note/soa/linkableImages/images/upload";
        fileUpload.uploadFileToUrl(file, uploadUrl);
    };

}]);

这是服务器日志中的堆栈跟踪:

[2015-04-16 10:30:29,427] DEBUG MultipartFilter 114 doFilterInternal - Request [/note/soa/linkableImages/images/upload] is not a multipart request
[2015-04-16 10:30:29,428]  INFO CsrfHeaderFilter 31 doFilterInternal - setting csrf token in the cookie
[2015-04-16 10:30:29,446] DEBUG ServletController 219 invokeDestination - Service http request on thread: Thread[http-nio-8080-exec-64,5,main]
[2015-04-16 10:30:29,448] DEBUG AbstractHTTPDestination 224 invoke - Create a new message for processing
[2015-04-16 10:30:29,487] DEBUG Headers 380 copyFromRequest - Request Headers: {Accept=[application/json, text/plain, */*], accept-encoding=[gzip, deflate], accept-language=[en-us], connection=[keep-alive], Content-Length=[1113], content-type=[undefined], cookie=[JSESSIONID=9D0598F4A5473149F59C385AC83AF11A; XSRF-TOKEN=d1b24eff-67e3-42f0-8707-7b4d1cbb9f24], dnt=[1], host=[localhost:8080], origin=[http://localhost:8080], referer=[http://localhost:8080/note/user/note], user-agent=[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5], x-xsrf-token=[d1b24eff-67e3-42f0-8707-7b4d1cbb9f24]}
[2015-04-16 10:30:29,512] DEBUG LogUtils 452 doLog - Could not determine bean name for instance of class org.apache.cxf.bus.managers.PhaseManagerImpl.
[2015-04-16 10:30:29,529] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.interceptor.ServiceInvokerInterceptor@5bcb1378 to phase invoke
[2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.interceptor.OutgoingChainInterceptor@716b5a5c to phase post-invoke
[2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.interceptor.OneWayProcessorInterceptor@3ef98830 to phase pre-logical
[2015-04-16 10:30:29,532] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c to phase unmarshal
[2015-04-16 10:30:29,534] DEBUG PhaseInterceptorChain 242 add - Adding interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88 to phase pre-stream
[2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 722 outputChainToLog - Chain org.apache.cxf.phase.PhaseInterceptorChain@5ba6c109 was created. Current flow:
  pre-stream [CertConstraintsInterceptor]
  unmarshal [JAXRSInInterceptor]
  pre-logical [OneWayProcessorInterceptor]
  invoke [ServiceInvokerInterceptor]
  post-invoke [OutgoingChainInterceptor]

[2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 304 doIntercept - Invoking handleMessage on interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88
[2015-04-16 10:30:29,539] DEBUG PhaseInterceptorChain 304 doIntercept - Invoking handleMessage on interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c
[2015-04-16 10:30:29,587] DEBUG JAXRSUtils 296 selectResourceClass - Trying to select a resource class, request path : /images/upload
[2015-04-16 10:30:29,588] DEBUG MediaTypeHeaderProvider 165 handleMediaTypeWithoutSubtype - Converting a malformed media type 'undefined' to '*/*'
[2015-04-16 10:30:29,593] DEBUG JAXRSUtils 405 findTargetMethod - Trying to select a resource operation on the resource class com.sonam.note.webservice.jaxrs.user.UserImages
[2015-04-16 10:30:29,595] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : hello, request path : /upload, method @Path : /hello, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : multipart/form-data,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
[2015-04-16 10:30:29,595] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : getFilenames, request path : /upload, method @Path : /user, HTTP Method : POST, method HTTP Method : GET, ContentType : */*, method @Consumes : */*,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
[2015-04-16 10:30:29,596] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : deleteFile, request path : /upload, method @Path : /user/folder/delete, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : application/x-www-form-urlencoded,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
[2015-04-16 10:30:29,598] DEBUG JAXRSUtils 449 findTargetMethod - Resource operation uploadFile may get selected
[2015-04-16 10:30:29,598] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : multipartObject, request path : /upload, method @Path : /object, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : multipart/form-data,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
[2015-04-16 10:30:29,599] DEBUG JAXRSUtils 592 logNoMatchMessage - No method match, method name : createFolder, request path : /upload, method @Path : /user/folder/create, HTTP Method : POST, method HTTP Method : POST, ContentType : */*, method @Consumes : application/x-www-form-urlencoded,, Accept : application/json,text/plain,*/*,, method @Produces : application/json,.
[2015-04-16 10:30:29,600] DEBUG JAXRSUtils 476 findTargetMethod - Resource operation uploadFile on the resource class com.sonam.note.webservice.jaxrs.user.UserImages has been selected
[2015-04-16 10:30:29,604] DEBUG JAXRSInInterceptor 190 processRequest - Request path is: /images/upload
[2015-04-16 10:30:29,605] DEBUG JAXRSInInterceptor 191 processRequest - Request HTTP method is: POST
[2015-04-16 10:30:29,605] DEBUG JAXRSInInterceptor 192 processRequest - Request contentType is: undefined
[2015-04-16 10:30:29,606] DEBUG JAXRSInInterceptor 193 processRequest - Accept contentType is: application/json, text/plain, */*
[2015-04-16 10:30:29,606] DEBUG JAXRSInInterceptor 195 processRequest - Found operation: uploadFile
[2015-04-16 10:30:29,609] DEBUG MediaTypeHeaderProvider 165 handleMediaTypeWithoutSubtype - Converting a malformed media type 'undefined' to '*/*'
[2015-04-16 10:30:29,674] DEBUG PhaseInterceptorChain 477 unwind - Invoking handleFault on interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@705c279c
[2015-04-16 10:30:29,678] DEBUG PhaseInterceptorChain 477 unwind - Invoking handleFault on interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4dce7e88
[2015-04-16 10:30:29,681]  WARN LogUtils 452 doLog - Interceptor for {http://user.jaxrs.webservice.note.sonam.com/}UserImages has thrown exception, unwinding now
java.lang.NullPointerException
    at org.apache.cxf.attachment.AttachmentUtil.getHeader(AttachmentUtil.java:306)

感谢任何建议

-Sonam

1 个答案:

答案 0 :(得分:0)

也许我有点晚了,但我认为应该是:

headers: {'Content-Type': undefined}

未定义'周围。