2017-08-27 4 views
0

、視聴不可の画像生成:ここは、私が試してみて、Javaで書かれた安らかなWebサービスに画像を受信するには、以下のソリューションを使用してい

@POST 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
@Produces(MediaType.TEXT_PLAIN) 
public String getFile(@FormDataParam("pic") InputStream file, 
     @QueryParam("uid") String uid) { 

    try { 
     storeFile(file, uid); 
    } catch (IOException ex) { 
     Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex); 
     return "failed"; 
    } 
    return "success"; 
} 

private void storeFile(InputStream input, String uid) throws IOException { 
    String absPath = PATH_TO_FILES + uid + ".jpg"; 
    try { 
     OutputStream out = new FileOutputStream(new File(absPath)); 
     int read = 0; 
     byte[] bytes = new byte[1024]; 

     out = new FileOutputStream(new File(absPath)); 
     while ((read = input.read(bytes)) != -1) { 
      out.write(bytes, 0, read); 
     } 
     out.flush(); 
     out.close(); 
    } catch (IOException e) { 

     e.printStackTrace(); 
    } 
} 

クライアントコードをされた(ジャバスクリプト)を:

$scope.fileSelect = function (files) { 

var file = files[0]; 
    console.log("File loaded"); 
    console.log(files); 
    console.log('uid = ' + $scope.uid + ' user = ' + $scope.user); 
    var formData = new FormData(); 
    formData.append('pic', file); 
    var requestBody = {"token": $scope.token}; 

    var req = { 
     method: 'POST', 
     url: 'http://192.168.0.9/resources/UploadPicture?uid=' + $scope.uid, 
     headers: { 
      'Content-Type': undefined 
     }, 
     data: formData 
    }; 
    console.log(FormData); 
    $http(req).then(function(response){ 
     console.log(response); 
    }, function(error){ 
     console.log(error); 
    }); 


}; 

このコードは、表示されないファイルを生成します。予期しているファイルは画像です。 は、だから私は2つの質問だ:Webサービスが応答呼び出されるたび

  1. をリターンです、それはハードディスクに完全にフラッシュ画像イマイチのように思えます。しばらくすると、私はそれを編集することができます。イメージが実際にディスクにフラッシュされたときにクライアントに応答する方法はありますか?

  2. 入力ストリームがディスクに書き込まれたときに表示可能な画像を生成するにはどうすればよいですか? --edit--

iが++メモ帳で画像を編集した場合、いくつかのファイルをいじった後、私は実現して、フォームの境界のためにbegginingタグと終了タグを脱いだ、画像は再び閲覧可能です:

Produced File

は、フォームの境界が画像データに干渉を停止するために方法はありますか?

@POST 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
@Produces(MediaType.TEXT_PLAIN) 
public String getFile(@Context HttpServletRequest request, @QueryParam("uid") String uid) { 


    ServletFileUpload upload = new ServletFileUpload(); 
    try { 
     FileItemIterator iter = upload.getItemIterator(request); 
     while (iter.hasNext()) { 
      FileItemStream item = iter.next(); 
      String name = item.getFieldName(); 
      InputStream stream = item.openStream(); 
      if (item.isFormField()) { 
       System.out.println("Form field " + name + " with value " 
         + Streams.asString(stream) + " detected."); 
      } else { 
       System.out.println("File field " + name + " with file name " 
         + item.getName() + " detected."); 
       // Process the input stream 
       storeFile(stream, uid); 
      } 
     } 

     return "success"; 

    } catch (FileUploadException ex) { 
     Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IOException ex) { 
     Logger.getLogger(UploadImage.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    return "failed."; 
} 
+0

がどのようにファイルをアップロードされています –

+0

クライアントコードを追加しました – SanzioSan

答えて

0

は、私は、Apacheコモンズのファイルアップロードを使用して解決策を見つけましたか?クライアントプログラムを表示できますか?
関連する問題