我正在尝试发送视频作为对我的Android应用程序的响应。这是服务器端(REST调用)和客户端的代码:
服务器端代码:
return Response.ok().entity(new FileInputStream(new File(fileName))).build();
此处'fileName'是我服务器上视频的路径。
我的Android应用中的客户端代码:
@Override
protected File doInBackground(Void... params) {
String address = <url of the rest service>;
HttpGet httpGet = new HttpGet(address);
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
File myVideo=null;
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
videoRoot.mkdirs();
myVideo = new File(videoRoot, "myVideo.3gp");
FileOutputStream fOut = new FileOutputStream(myVideo);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
myOutWriter.append((char) b);
}
Log.i("VideoResponse", stringBuilder.toString());
myOutWriter.close();
fOut.close();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return myVideo;
}
仅供参考 - 当我在服务器上运行时,视频播放正常。但在Android设备上,它给了我以下错误代码“视频无法播放”。我也匹配了两个视频的校验和,两者都不同。此外,当我将生成的视频从Android设备复制到我的计算机时,它已损坏。所以我发现我的客户端代码是错误的。任何人都可以帮助我。