2014-09-19 37 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); 
}