视频播放测试
程序员文章站
2024-01-16 19:11:16
...
//视频播放测试
public String text(HttpServletRequest re, HttpServletResponse rs) {
CloseableHttpClient httpClient = null;
try {
SSLContext sslContext = SSLContextBuilder.create().useProtocol(SSLConnectionSocketFactory.SSL)
.loadTrustMaterial((x, y) -> true).build();
RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).setSSLContext(sslContext)
.setSSLHostnameVerifier((x, y) -> true).build();
} catch (Exception e) {
e.printStackTrace();
}
String flag = "0";
HttpGet httpGet = new HttpGet("*******");
httpGet.setHeader(new BasicHeader("Cookie", "******"));
// response 对象
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream in=entity.getContent();
int count = 0;
while (count == 0) {
count = Integer.parseInt(""+entity.getContentLength());//in.available();
}
byte[] bytes = new byte[count];
int readCount = 0; // 已经成功读取的字节的个数
while (readCount <= count) {
if(readCount == count)break;
readCount += in.read(bytes, readCount, count - readCount);
}
OutputStream out=null;
rs.setContentType("video/mp4");
out = rs.getOutputStream();
out.write(bytes);
out.flush();
out.close();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (null != httpGet) {
httpGet.releaseConnection();
}
if (null != response) {
try {
response.close();
} catch (IOException e) {
}
}
}
return flag;
}
上一篇: 关于PHP公钥加密的有关问题