クライアント側 (JavaScript) からサーバー側の Jaxrs 実装に img を送信する必要があります。
クライアント側:
function sendRequest()
{
var url = 'http://localhost:8080/MobilePOC/restService/uploadImage';
$("body").append('<canvas id="theCanvas" style="display:none" width="300px"
height="300px"></canvas>');
var canvas = document.getElementById('theCanvas');
var context = canvas.getContext('2d');
var imageObj = new Image();
imageObj.src = "myjpg.jpeg";
context.drawImage(imageObj, 0, 0, 300, 300);
$.post(url, {'image':canvas.toDataURL("image/jpeg"), 'url':'caption'},
function(file){
//Callback code
alert("done");
});
}
そして、私が使用しているJAXRSのサーバー側では:
それは機能しますが、base64文字列を画像に戻すと機能しないという問題があります。元の画像ではなく、空白の画像を作成しています。
@POST
@Path("/uploadImage")
@Consumes(MediaType.WILDCARD)
public Response upload(String image) {
System.out.println("In upload:"+image);
Base64 decoder = new Base64();
try {
byte[] imgBytes = decoder.decode(image);
FileOutputStream osf;
osf = new FileOutputStream(new File("C:/yourImage.jpg"));
osf.write(imgBytes);
osf.flush();
osf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// store input somewhere
return Response.ok().build();
}
エンコードされた文字列のいくつかの文字を置き換えようとしましたが、成功しませんでした。