2016-04-17 4 views
0

ファイル名に変数を使用してダウンロードしたファイルの名前を設定すると、変数が表示されません。しかし、ファイル名に変数を使用しない場合は、必要に応じて名前を設定します。この方法でsetHeaderはファイル名を変更しません

response().setHeader("Content-disposition", "attachment; filename=testName.pdf");、ダウンロードしたファイルの名前は、私は、変数とそれを使用するには、3つの異なる方法を試してみましたtestName.pdf

です。

response().setHeader("Content-disposition", "attachment; filename="+ fileName.toString() +".pdf");

または

..... "attachment; filename="+ fileName +".pdf"); 

または

..... "attachment; filename="+ fileName.toString() +".pdf"); 

全コード:

public static Result download(String id) throws IOException { 
     Get g = new Get(Bytes.toBytes(id)); 
     g.addColumn(Bytes.toBytes("content"), Bytes.toBytes("raw")); 
     g.addColumn(Bytes.toBytes("book"), Bytes.toBytes("title")); 

     HTable hTable = new HTable(hConn.config, "books"); 
     org.apache.hadoop.hbase.client.Result result = hTable.get(g); 

     if (result.containsColumn(Bytes.toBytes("content"), Bytes.toBytes("raw"))){ 
      byte[] rawBook = result.getNoVersionMap().get(Bytes.toBytes("content")).get(Bytes.toBytes("raw")); 
      byte[] fileName = result.getNoVersionMap().get(Bytes.toBytes("book")).get(Bytes.toBytes("title")); 
      response().setContentType("application/octet-stream"); 
      response().setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\".pdf"); 
      return ok(rawBook); 
     } 
     return notFound(); 
    } 

だから、これはJavaのプレイフレームワークからです。データベースはHBaseです。私はbooksと呼ばれる1つのテーブルを持っており、それは2つのファミリーcontentbookを持っています。 contentにはpdf(バイト単位)の内容が含まれ、bookにはpdf(タイトル、ページ番号、著者など)のプロパティが含まれています。 Row Keycontentbookの両方が同じです。

変数を使用してファイル名を設定する別の方法がありますか、または何か不足していますか?

答えて

2

この問題は、byte[]Stringに変換しているように見えます。そのアレイ上のシンプルなtoString()は、[[email protected]のようなものにつながります。これはAFAIKがfilenameとして受け入れられていません。エンコードは常に重要ですが、あなたはUnsupportedEncodingExceptionをキャッチする必要があります。このコンストラクタを使用していること

String fn = new String(filename, "UTF-8"); 

注:

は次のようにStringbyte[]を変換してみます。

あなたが UnsupportedEncodingExceptionをキャッチすることなく、follwoingを使用できるJava 8で

String fn = new String(filename, java.nio.charset.StandardCharsets.UTF_8); 
+0

はいあなたは正しかったです。私は[B @を得ていた...ありがとうございました。 –

関連する問題