2015-04-16 3 views
1

Я работаю над методом JAXRS для загрузки файла с помощью клиента AngularJS. Я следил за образцом, найденным здесь для кода AngularJS https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs. На стороне сервера метод найден, но я запускаю NullPointerException. Я отправлю трассировку стека в конце здесь. ИнтерфейсЗагрузка файла AngularJS CXF JAXRS

Мои 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> 

AngularJS код для ввода HTML файла:

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 [email protected] to phase invoke 
[2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding interceptor [email protected] to phase post-invoke 
[2015-04-16 10:30:29,531] DEBUG PhaseInterceptorChain 242 add - Adding interceptor [email protected] to phase pre-logical 
[2015-04-16 10:30:29,532] DEBUG PhaseInterceptorChain 242 add - Adding interceptor [email protected] to phase unmarshal 
[2015-04-16 10:30:29,534] DEBUG PhaseInterceptorChain 242 add - Adding interceptor [email protected]88 to phase pre-stream 
[2015-04-16 10:30:29,536] DEBUG PhaseInterceptorChain 722 outputChainToLog - Chain [email protected] 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 [email protected]88 
[2015-04-16 10:30:29,539] DEBUG PhaseInterceptorChain 304 doIntercept - Invoking handleMessage on interceptor [email protected] 
[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 [email protected] 
[2015-04-16 10:30:29,678] DEBUG PhaseInterceptorChain 477 unwind - Invoking handleFault on interceptor [email protected]88 
[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

ответ

0

Может быть, я немного поздно, но я думаю, что это должно быть:

headers: {'Content-Type': undefined} 

без ' вокруг неопределенными.

+0

Я попробую это и загляну. спасибо Рори – Sonam