2016-10-14 22 views
0

Retrofit 1.9とSpring Serverで動作するAndroidアプリケーションがありました。 Androidアプリケーションはファイルを完全にダウンロードできます。 Retrofit 2.0に移行しようとしています。Retrofit 2.0とSpring Serverを使用してファイルをダウンロードする

レトロフィット1.9コード

コントローラサーバー:

@RequestMapping(value = GET_MAP , method = RequestMethod.GET) 
public void getMap(@PathVariable("filename")String filename 
      ,HttpServletResponse response) throws IOException { 
     Files.copy(filename + ".zip", response.getOutputStream()); 
} 

AndroidのインタフェースAPI

@Streaming 
@GET(GET_MAP) 
public Response getMap(@Path("filename") long id); 

ので、ダウンロードするために移行するためのグーグルでの後ファイルin Retrofit 2.0私はAndroidのインターフェイスApiのCall<ResponseBody>を使用する必要があります。

OPTION 1コントローラサーバーコード

@RequestMapping(value = GET_MAP , method = RequestMethod.GET) 
public void getMap(@PathVariable("filename")String filename 
      ,HttpServletResponse response) throws IOException { 
     Files.copy(filename + ".zip", response.getOutputStream()); 
} 

OPTION 2コントローラサーバーコード

@RequestMapping(value = GET_MAP , method = RequestMethod.GET) 
public @ResponseBody HttpServletResponse getMap(@PathVariable("filename")String filename 
      ,HttpServletResponse response) throws IOException { 
     Files.copy(filename + ".zip", response.getOutputStream()); 
return response; 
} 

AndroidのインタフェースAPI

@Streaming 
@GET(GET_MAP) 
public Call<ResponseBody> getMap(@Path("filename") long id); 
:私はこのような何かを試してみました

しかし、これらの2つのオプションを使用して応答長は私を与え-1:

response.body().contentLength() = -1

私はコントローラのメソッドを移行する必要がどのように?

答えて

0

サーバー上の応答のためのいくつかの構成を試した後、これは私のために働いている:

1からnew File

2としてファイルを取得する - ResponseBodyとしてファイルを渡すためにorg.springframework.core.io.FileSystemResource.FileSystemResource ライブラリを使用します。

@RequestMapping(value = GET_MAP , method = RequestMethod.GET) 
public @ResponseBody Resource getMap(@PathVariable("filename")String filename 
      ,HttpServletResponse response) throws IOException { 
     File file = new File(filename + ".zip"); 
     return new FileSystemResource(file); 
    } 
関連する問題