1

PDFファイルをブロブとして保存しました。ブラウザーでPDFとしてレンダリングしようとすると、次のようになります。

%PDF-1.4 %���� 5 0 obj <>stream x��[]s�6}    

これは、私が使用しているコントローラーでの私のアクションです def showDetails = {

    def product = Sample.get(params.id)

    response.contentType = 'application/pdf' 
    response.setHeader('Content-disposition', 'attachment; filename=file.pdf')
    response.getOutputStream().write(product.file)

    response.getOutputStream().flush()

}

私も追加しようresponse.characterEncoding = "utf 8"としましたが、それもうまくいきませんでした。

明確にするために:PDFファイルをブロブとして保存するフォームがあります。オブジェクトがデータベースに保存されると、オブジェクトといくつかのパラメーター (名前、日付など) がアプリに表示されます。名前をクリックすると、div 内の blob ファイルへのリンクが表示されます。

<g:remoteLink id="${s.id}" update="details"  controller="submitSample"action="showDetails" >${s.sampleNameID}</g:remoteLink></td>

このように getOutputStream を getBytes() に変更すると、次のエラーが発生します。

No signature of method: [B.getBytes() is applicable for argument types: () values: []

可能な解決策: getClass()、getAt(groovy.lang.ObjectRange)、getAt(java.lang.Integer)、getAt(groovy.lang.IntRange)、getAt(java.lang.String)、getAt(groovy.lang.Range) )。

単純に product.file を使用すると、出力が文字化けします。

4

2 に答える 2

2

私はそれがどのデータ型であるかはわかりませんが、これがうまくいくはずだproduct.fileと仮定すると...java.io.File

  response.setContentType('application/pdf');
  response.setHeader('Content-disposition', 'Attachment; filename=file.pdf');
  response.getOutputStream().write(product.file.toNewInputStream());
  response.getOutputStream().flush();

ただしproduct.fileブロブの場合は、ブラウザーの応答に送信する前に、ブロブからバイトを取得する必要があります...

 response.setContentType('application/pdf');
 response.setHeader('Content-disposition', 'Attachment; filename=file.pdf');
 response.getOutputStream().write(product.file.getBytes());
 response.getOutputStream().flush();

これはテストされていないコードですが、おわかりいただけると思います。ブロブの toString() を実行しているだけなので、ブラウザー出力に奇妙な文字が出力されているのを見ていると思います。楽しみ。

于 2012-05-29T20:12:36.257 に答える
2

も指定してみてくださいContent-Disposition

response.contentType = 'application/pdf'
response.setHeader('Content-disposition', 'Attachment; filename=file.pdf')
response.getOutputStream().write(product.file)
于 2012-05-29T19:01:41.110 に答える