2017-09-03 17 views
0

レガシーサービスから中間層のSpring Webサービスを介してファイルをダウンロードしたいと考えています。問題は、現在、ファイル自体ではなく、ファイルの内容を返すことです。Spring Web:Springサービスを介したサービスからのファイルのダウンロード

私は以前にFileSystemResourceを使っていましたが、私はSpringにリダイレクトしてサーバー自体にファイルを作成しないようにしたいので、これをしたくありません。ここで

はメソッドです:

@Override 
public byte[] downloadReport(String type, String code) throws Exception { 
    final String usernamePassword = jasperReportsServerUsername + ":" + jasperReportsServerPassword; 
    final String credentialsEncrypted = Base64.getEncoder().encodeToString((usernamePassword).getBytes("UTF-8")); 
    final HttpHeaders httpHeaders = new HttpHeaders(); 
    httpHeaders.add("Accept", MediaType.APPLICATION_JSON_VALUE); 
    httpHeaders.add("Authorization", "Basic " + credentialsEncrypted); 
    httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM)); 
    final HttpEntity httpEntity = new HttpEntity(httpHeaders); 
    final String fullUrl = downloadUrl + type + "?code=" + code; 

    return restTemplate.exchange(fullUrl, HttpMethod.GET, httpEntity, byte[].class, "1").getBody(); 
} 
+0

*現在のところ、問題はファイル自体ではなくファイルの内容を返すことです。*:そのmeaは何ですか? n?あなたは何をしていますか、何が起こると思いますか?代わりに何が起こりますか? –

+0

別のWebサービスにあるファイルをダウンロードしようとしています。私はファイルをダウンロードしたい。私はファイルの生の内容を表示しています。 –

+0

これは、「何が起こるか」の部分に答えるものです。あなたは何が起こると思いますか? –

答えて

0

は、私は私の*コントローラクラスでこの注釈パラメータがありませんでしたが判明:

produces = MediaType.APPLICATION_OCTET_STREAM_VALUE 

コントローラの全体の方法は次のようになります。

@RequestMapping(value = "/download/{type}/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) 
    public ResponseEntity<?> downloadReport(@PathVariable String type, @PathVariable String id) throws Exception { 
     return new ResponseEntity<>(reportService.downloadReport(type, id), HttpStatus.OK); 
    } 
関連する問題