2012-03-16 4 views
2

私はGralis 1.3.7を使用しています。私は別のサーバーからPDFファイルを取得してクライアントに返す必要があるコントローラを作成しています。私は、次のような、いくつかの合理的に効率的な方法でこれを実行したいと思います:grailsにプロキシを書く

class DocController { 
    def view = { 
     URL source = new URL("http://server.com?docid=${params.docid}"); 

     response.contentType = 'application/pdf'; 
     // Something like this to set the content length 
     response.setHeader("Content-Length", source.contentLength.toString()); 
     response << source.openStream(); 
    } 
} 

私が午前問題が戻ってくる情報に基づいて私のコントローラの応答のコンテンツ長を設定する方法を考え出すされますsourceから。私はグレイルによって強化されたURLクラスのドキュメントを見つけることができませんでした。

続行するにはどうすればよいですか?

遺伝子

EDITED:へのsetHeaderのパラメータ値を修正

UPDATED 2012年3月16日10:49 PST

UPDATED 2012年3月19日10時45分PST 移転フォローアップ別の質問。

答えて

2

java.net.URLConnectionオブジェクトを使用すると、URLでより詳細な作業を行うことができます。

URLConnection connection = new URL(url).openConnection() 

def url = new URL("http://www.aboutgroovy.com") 
def connection = url.openConnection() 
println connection.responseCode  // ===> 200 
println connection.responseMessage  // ===> OK 
println connection.contentLength  // ===> 4216 
println connection.contentType   // ===> text/html 
println connection.date    // ===> 1191250061000 
println connection.lastModified 

// print headers 
connection.headerFields.each{println it} 

あなたの例のようなものに見えるはずです。

class DocController { 
    def view = { 
     URL source = new URL("http://server.com?docid=${params.docid}"); 
     URLConnection connection = source.openConnection(); 

     response.contentType = 'application/pdf'; 

     // Set the content length 
     response.setHeader("Content-Length", connection.contentLength.toString()); 

     // Get the input stream from the connection 
     response.outputStream << connection.getInputStream(); 
    } 
} 
+0

感謝を!これは近いです。サーバーはPDF形式のストリームで応答し、ブラウザはPDFをレンダリングしていると考えていますが、空白のページしかありません。 PDFレンダラを混乱させる追加の情報が送信されている可能性がありますか? –

+0

コードの種類は機能しますが、送信されるファイルは元のものとは異なります。これは文字エンコードの問題でしょうか? –

+0

私は答えを編集しました。トリックは最後の行にある – pablomolnar

関連する問題