Получаю java.io.IOException: unexpected end of stream on http:/ когда пытаюсь загрузить файл из сервера
Привет получаю ошибку (java.io.IOException: unexpected end of stream on http:) при попытке загрузить файл(.txt) из сервера вот код сервера
@RestController
public class DownloadDbController {
@RequestMapping(path = "/download", method = RequestMethod.GET)
public ResponseEntity<Resource> download(String param) throws IOException {
File file = new File("src/main/res/123.txt");
HttpHeaders header = new HttpHeaders();
header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=123.txt");
Path path = Paths.get(file.getAbsolutePath());
ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));
return ResponseEntity.ok()
.headers(header)
.contentLength(file.length())
.contentType(MediaType.TEXT_PLAIN)
.body(resource);
}
}
Апи:
public interface GetDb {
@Streaming
@GET("{url}")
Observable<ResponseBody> downloadDB(@Path("url") String url);
}
вот конфигурация ретрофита:
public class RetrofitService {
private static RetrofitService mInstance;
private static final String BASE_URL = "http://localhost:8080/";
private Retrofit mRetrofit;
private RetrofitService() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.addInterceptor(interceptor);
mRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client.build())
.build();
}
public static RetrofitService getInstance() {
if (mInstance == null) {
mInstance = new RetrofitService();
}
return mInstance;
}
public GetDb getJSONApi() {
return mRetrofit.create(GetDb.class);
}
}
И вот собственно сам вызов и попытка записать в файл
public class SyncDb {
Context mContext;
public SyncDb(Context mContext) {
this.mContext = mContext;
}
public void downloadDB(String path) {
RetrofitService.getInstance()
.getJSONApi()
.downloadDB("download")
.subscribeOn(Schedulers.io())
.subscribe(new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseBody responseBody) {
saveFile(responseBody);
Log.d("abc","onNext");
}
@Override
public void onError(Throwable e) {
Log.d("abc","error");
e.printStackTrace();
}
@Override
public void onComplete() {
Log.d("abc","Completed");
}
});
public void saveFile(
ResponseBody responseBody
) {
File folder =new File(mContext.getFilesDir().toString(), "123.txt");
folder.mkdirs();
File unzipMe = new File(folder, "123.txt");
if (!unzipMe.exists()){
try {
unzipMe.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(unzipMe));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
bos.write(responseBody.bytes());
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}