Я хочу скачать видео через GET-запрос с помощью Retrofit.
В моем Api я добавил метод, который отправляет GET-запрос в / video / {id} / data, он возвращает retrofit.client. Объект ответа, если запрос выполнен успешно, и соответствующий статус ошибки HTTP в противном случае:
public interface VideoSvcApi {
//...
@Streaming
@GET("/video/{id}/data")
Response getData(@Path("id") long id);
}
И это реализация:
@Controller
public class VideoController {
//...
@Streaming
@RequestMapping(value = "/video/{id}/data", method = RequestMethod.GET)
public @ResponseBody Response getData(@PathVariable("id") long id, HttpServletResponse response) throws IOException{
Video v = getVideo(id, response);
if (v==null) {
return new Response("/video/{id}/data", 404, "The video with id: "+id+" does not existe!", new ArrayList<Header>(), null);
} else {
boolean valid = VideoFileManager.get().hasVideoData(v);
if(valid) {
System.out.println("Valid "); // Valid
File file = new File(v.getLocation());
TypedInput tf = new TypedFile(v.getContentType(), file);
Response res = new Response("/video/{id}/data", 200, "Success", new ArrayList<Header>(), tf);
System.out.println(res.getBody().mimeType()); // video/mp4
System.out.println(res.getBody().in()); // java.io.FileInputStream@1ad6faaf
return res;
} else {
System.out.println("Invalid ");
return new Response("/video/{id}/data", 404, "The video with id: "+id+" is not stored yet!", new ArrayList<Header>(), null);
}
}
}
}
Когда я вызываю метод следующим образом:
Video received; // = new ....
Response response;
try {
response = videoSvc.getData(received.getId());
}catch(Exception e) {
e.printStackTrace();
}
Все распечатки в «if (valid) {...}» напечатаны, но я получаю следующую ошибку:
retrofit.RetrofitError: 500 Ошибка сервера
в retrofit.RetrofitError.httpError (RetrofitError. java: 39) в retrofit.RestAdapter $ RestHandler.invokeRequest (RestAdapter. java: 382) в retrofit.RestAdapter $ RestHandler.invoke (RestAd *. ) в com.sun.proxy. $ Proxy6.getData (Неизвестный источник) в org.magnum.dataup.AutoGradingTest.testAddVideoData (AutoGradingTest. java: 154) в sun.reflect.NativeMethodAccessorImpl.invoke0 (собственный метод sun в. reflect.NativeMethodAccessorImpl.invoke (NativeMethodAc cerorImpl. java: 62) в sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl. java: 43) в java .lang.reflect.Method.invoke (Метод. java: 498) в org.jit .runners.model.FrameworkMethod $ 1.runReflectiveCall (FrameworkMethod. java: 47) в org.junit.internal.runners.model.ReflectiveCallable.run (ReflectiveCallable. java: 12) в org.junitdelrunners. FrameworkMethod.invokeExplosively (FrameworkMethod. java: 44) в org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod. java: 17) в org.junit.runners.ParentRunner.runl (10RunL). *. $ 3.run (ParentRunner. java: 238) в org.junit.runners.ParentRunner $ 1.schedule (ParentRunner. java: 63) в org.junit.runners.ParentRunner.runChildren (ParentRunner. java: 236 ) в org.junit.runners.ParentRunn er.access $ 000 (ParentRunner. java: 53) в org.junit.runners.ParentRunner $ 2.evaluate (ParentRunner. java: 229) в org.junit.runners.ParentRunner.run (ParentRunner. java: 309) в org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (JUnit4TestReference. java: 89) в org.eclipse.jdt.internal.junit.runner.TestExecution.run (TestExecution: *41. 41) в org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (RemoteTestRunner. java: 541) в org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests: Remote. 763) по адресу org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner. java: 463) по адресу org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner *. 209)