2014-09-19 59 views
0

我正在撰寫一個概念證明應用程序,該應用程序由Sencha Touch/Cordova混合應用程序組成,該應用程序訪問手機相機API,拍攝照片並將其上傳到Spring服務器。我的手機應用程序干擾正常。我已經設法拍攝了這張照片,但我有問題發佈到Spring後端。當我調試服務器時,我可以看到應用程序正確地點擊Web服務,但圖像字節[]似乎是空白的。我使用Android logcat在客戶端進行了調試,並且圖像肯定是從客戶端發送來的。我認爲問題在於我的web服務正在接收發布的圖像。從科爾多瓦發佈照片到Spring Web服務

我的基本客戶端代碼如下:

function onPhotoUriSuccess(imageUriToUpload){ 
    var url=encodeURI("http://192.168.2.4:8080/app/api/rest//uploadImage/"); 

    var params = new Object(); 
    params.your_param_name = "something"; //you can send additional info with the file 

    var options = new FileUploadOptions(); 
    options.fileKey = "the_name_of_the_image_field"; //depends on the api 
    options.fileName = imageUriToUpload.substr(imageUriToUpload.lastIndexOf('/')+1); 
    options.mimeType = "image/jpeg"; 
    options.params = params; 
    options.chunkedMode = true; //this is important to send both data and files 

    var headers={'Authorization':'TOKEN_BASED_AUTHENTICATION '+ localStorage.getItem('Token')}; 
    options.headers = headers; 

    var ft = new FileTransfer(); 
    console.log("Donovan"); 
    console.log("image uri" + imageUriToUpload) 
    ft.upload(imageUriToUpload, url, win, fail, options, true); 

} 

function win(r) { 
    console.log("Code = " + r.responseCode.toString()+"\n"); 
    console.log("Response = " + r.response.toString()+"\n"); 
    console.log("Sent = " + r.bytesSent.toString()+"\n"); 
    alert("Code Slayer!!!"); 
} 

function fail(error) { 
    alert("An error has occurred: Code = " + error.code); 
} 

Ext.define('PhotoBomb.view.Main', { 
    extend: 'Ext.Panel', 
    xtype: 'main', 
    requires: [ 
     'Ext.Button', 
     'Ext.Img' 
    ], 
    config: { 
     layout: { 
      type:"vbox", 
      pack:"center", 
      align:"center" 
     }, 
     items: [ 
      { 
       xtype: "image", 
       src: "http://placehold.it/200x200", 
       width: 200, 
       height: 200 
      }, 
      { 
       xtype: "button", 
       text: "Photo", 
       handler: function() 
       { 
        console.log('Handler for launching camera'); 
        navigator.camera.getPicture(onPhotoUriSuccess, onFail, 
         { 
          quality: 40, 
          destinationType:Camera.DestinationType.FILE_URI, 
          encodingType : navigator.camera.EncodingType.JPEG, 
          targetWidth: 500, 
          targetHeight: 500, 
          saveToPhotoAlbum: 1 
         }); 

        function onFail(){ 
         Ext.Msg.alert(message); 
         console.log(message); 
        } 

        function refreshImages() 
        { 
         console.log('Image Gallery refreshed'); 
        } 

       } 
      } 
     ] 
    } 
}); 

我的基本春天web服務如下:

Path("/uploadImage/") 
@Produces("application/json") 
@Consumes("*/*") 
public interface ImagePostWebService { 
    /** 
    * A Web service that handles tokens 
    */ 
     @POST 
     String postPicture(InputStream streame); 
} 

而且實現

@Override 
    public String postPicture(InputStream image) { 
     byte [] images; 
     try { 
      images = IOUtils.toByteArray(image); 
     } 
     catch (Exception e) { 
      return "Mother Trucker"; 
     } 

     boolean empty = true; 
     for (byte b : images) { 
      if (b != 0) { 
       empty = false; 
      } 
     } 
     if (empty) { 
      return "empty"; 
     } 

       log.info("image upload webservice hit"); 
     log.info(Arrays.toString(images)); 

     Employee employee = contextBeanService.getLoggedOnEmployee(); 
     UploadRequestDTO uploadRequest = new UploadRequestDTO(employee.getId(), DocumentType.CLIENT_MUGSHOT, 
       "new.jpg", "image/jpeg"); 
     uploadRequest.setExtension("jpg"); 
     try { 
     documentCRUDMappingService.createDocument(DocumentType.CLIENT_MUGSHOT, uploadRequest, images, 
       employee.getId()); 
     } catch (IOException ioe) { 

     } 

     return "done"; 
    } 

當我運行應用程序,調試服務器端,我可以看到它打到web服務,當我打印出byte []接收它如下:[115,111,109,101,116,104,105,110,103],這不是我期望從三星S4mini拍照手機拍攝的圖像,我想我錯過了關於上傳從科爾多瓦到我的春季web服務的圖像。任何幫助,將不勝感激 !

回答

0

我的問題的根源是我做了一個錯誤的假設,即我正在使用Spring作爲web服務,實際上我使用的是Java web服務。一旦我發現這一點,我的問題就很簡單了。事實上我錯過了一個表單參數,所以我的web服務從來沒有真正期待過一個圖像。對我的Web服務接口的修復如下:

import javax.ws.rs.*; 

@Path("/uploadImage/") 
@Produces("application/json") 
@Consumes("*/*") 
public interface ImagePostWebService { 
    /** 
    * A Web service that handles tokens 
    */ 
     @POST 
     String postPicture(@FormParam("image") String image); 
}