2017-08-06 7 views
0

私はjavaでsvgエディタ入力を処理するプログラムを作成しています。これは私のJAXRS rest apiにsvgファイルを送信するために使用したコードです。受信したxmlコンテンツは、httpリクエストから残りのAPIに送信すると変更されます

$('#save').click(function (e){ 
    var svg = canvas.toSVG(); 
    fabric.log(svg); 

    $.ajax({ 
     crossDomain: true, 
     url: 'http://localhost:8080/webapi/svg', 
     type: 'POST', 
     data: { 
      svg, 
     }, 
     success: function(){alert('PUT completed');} 
    }); 
    }); 

そして、以下のようにsvgログ。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="800" height="500" viewBox="0 0 800 500" xml:space="preserve"> 
<desc>Created with Fabric.js 1.6.3</desc> 
<defs></defs> 
<line id="branch" x1="-93" y1="84.5" x2="93" y2="-84.5" style="stroke: rgb(0,0,0); stroke-width: 5; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(245 300.5)"/> 
<line id="branch" x1="-67" y1="-84" x2="67" y2="84" style="stroke: rgb(0,0,0); stroke-width: 5; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(308 296)"/> 
<line id="branch" x1="-103" y1="131.5" x2="103" y2="-131.5" style="stroke: rgb(0,0,0); stroke-width: 5; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(360 269.5)"/> 
<line id="branch" x1="0" y1="0" x2="0" y2="0" style="stroke: rgb(0,0,0); stroke-width: 5; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" transform="translate(524 201)"/> 
</svg> 

そして、私はこの静止画を残りのapiに投稿すると、次のようになります。

svg=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22+standalone%3D%22no%22+%3F%3E%0A%3C!DOCTYPE+svg+PUBLIC+%22-%2F%2FW3C%2F%2FDTD+SVG+1.1%2F%2FEN%22+%22http%3A%2F%2Fwww.w3.org%2FGraphics%2FSVG%2F1.1%2FDTD%2Fsvg11.dtd%22%3E%0A%3Csvg+xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22+xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22+version%3D%221.1%22+width%3D%22800%22+height%3D%22500%22+viewBox%3D%220+0+800+500%22+xml%3Aspace%3D%22preserve%22%3E%0A%3Cdesc%3ECreated+with+Fabric.js+1.6.3%3C%2Fdesc%3E%0A%3Cdefs%3E%3C%2Fdefs%3E%0A%3Cline+id%3D%22branch%22+x1%3D%22-93%22+y1%3D%2284.5%22+x2%3D%2293%22+y2%3D%22-84.5%22+style%3D%22stroke%3A+rgb(0%2C0%2C0)%3B+stroke-width%3A+5%3B+stroke-dasharray%3A+none%3B+stroke-linecap%3A+butt%3B+stroke-linejoin%3A+miter%3B+stroke-miterlimit%3A+10%3B+fill%3A+rgb(0%2C0%2C0)%3B+fill-rule%3A+nonzero%3B+opacity%3A+1%3B%22+transform%3D%22translate(245+300.5)%22%2F%3E%0A%3Cline+id%3D%22branch%22+x1%3D%22-67%22+y1%3D%22-84%22+x2%3D%2267%22+y2%3D%2284%22+style%3D%22stroke%3A+rgb(0%2C0%2C0)%3B+stroke-width%3A+5%3B+stroke-dasharray%3A+none%3B+stroke-linecap%3A+butt%3B+stroke-linejoin%3A+miter%3B+stroke-miterlimit%3A+10%3B+fill%3A+rgb(0%2C0%2C0)%3B+fill-rule%3A+nonzero%3B+opacity%3A+1%3B%22+transform%3D%22translate(308+296)%22%2F%3E%0A%3Cline+id%3D%22branch%22+x1%3D%22-103%22+y1%3D%22131.5%22+x2%3D%22103%22+y2%3D%22-131.5%22+style%3D%22stroke%3A+rgb(0%2C0%2C0)%3B+stroke-width%3A+5%3B+stroke-dasharray%3A+none%3B+stroke-linecap%3A+butt%3B+stroke-linejoin%3A+miter%3B+stroke-miterlimit%3A+10%3B+fill%3A+rgb(0%2C0%2C0)%3B+fill-rule%3A+nonzero%3B+opacity%3A+1%3B%22+transform%3D%22translate(360+269.5)%22%2F%3E%0A%3Cline+id%3D%22branch%22+x1%3D%220%22+y1%3D%220%22+x2%3D%220%22+y2%3D%220%22+style%3D%22stroke%3A+rgb(0%2C0%2C0)%3B+stroke-width%3A+5%3B+stroke-dasharray%3A+none%3B+stroke-linecap%3A+butt%3B+stroke-linejoin%3A+miter%3B+stroke-miterlimit%3A+10%3B+fill%3A+rgb(0%2C0%2C0)%3B+fill-rule%3A+nonzero%3B+opacity%3A+1%3B%22+transform%3D%22translate(524+201)%22%2F%3E%0A%3C%2Fsvg%3E 

これは私のrestApiコードです。私は残りのクライアントと試み、ボディ、そのまま残りのAPI印刷全体のXMLコンテンツとして上記のXMLコンテンツとAPIを休ませるPOSTリクエストを送信した場合

@POST 
@Produces(MediaType.TEXT_PLAIN) 
public String showModel(String xmlString) { 
    System.out.println(xmlString); 
    return "Post Method"; 
} 

誰かがこの問題の解決策を知っている場合。私を助けてください。私はxmlをファイルに保存したい。しかし、受信したコンテンツはエンコードされているか何かがある。

+1

java.net.URLDecoder.decode(url、 "UTF-8"); –

+0

これで動作します。ありがとうございました。 –

答えて

0

$.ajax()を使用する場合のデフォルトのコンテンツタイプはapplication/x-www-form-urlencoded; charset=UTF-8です。そのため、データはそのようにエンコードされています。

エンコードしたくない場合は、変更してください。

SVGデータを送信しているので、$.ajax()コールでcontentType: 'image/svg+xml; charset=UTF-8'と指定してください。

ハンドラがSVGデータのみを受け入れるように制限する場合は、ハンドラメソッドを@Consumes("image/svg+xml")に変更する必要があります。

関連する問題