因为客户机的访问不到政务云存储的ip, 所以决定使用后台返回视频和图片流到前端
注意: 如果上边返回的流会导致图片不清晰或者马赛克, 请使用下边注释的代码返回流
前端视频用的是ckplayer
/**
* 返回视频流和图片流
* @param response
* @param imgPath
*/
@RequestMapping("/getViewImg1")
public void execute1(HttpServletResponse response,@RequestParam(value="imgPath") String imgPath){
//由于数据库存的是绝对路径,之前的老数据只能这样转换了
imgPath=imgPath.replace("http://zkyt-yc.oss-cn-ningxia-a-internal.aliyuncs.com/", "http://zkyt-yc.oss-cn-yc-yczw-d01-a.yc-ops.nxcloud.com.cn/");
System.out.println("路径-"+imgPath);
try {
if(imgPath.indexOf("http")>-1) {
URL url = null;
InputStream input = null;
try{
url = new URL(imgPath);
HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
httpUrl.getInputStream();
input = httpUrl.getInputStream();
}catch (Exception e) {
e.printStackTrace();
return;
}
response.setContentType(url.openConnection().getContentType());
ServletOutputStream out=response.getOutputStream();
try {
byte[] buf = new byte[2048];
while(input.read(buf)>=0){
out.write(buf);
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(input!=null){
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 如果边返回的流会导致图片不清晰或者马赛克, 请使用下边注释的代码返回流
/*
URL urlimg = new URL(imgPath);
//创建链接对象
URLConnection urlConnection = urlimg.openConnection();
//设置超时
urlConnection.setConnectTimeout(1000);
urlConnection.setReadTimeout(5000);
urlConnection.connect();
//获取流
InputStream inputStream = urlConnection.getInputStream();
// 判读是mp4格式还是jpg格式
String format=imgPath.substring(imgPath.lastIndexOf(".")+1);
if(".mp4".equals(format)) {
response.setContentType("video/mp4"); // 设置返回的文件类型
response.addHeader("Content-Type", "audio/mp4;charset=UTF-8");
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
}else if(".mp3".equals(format)) {
response.addHeader("Content-Type", "audio/mpeg;charset=UTF-8");
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
}else if (".jpg".equals(format)) {
//读取图片
BufferedImage bufferedImage = ImageIO.read(inputStream);
if (bufferedImage!=null){
//打印图片
ImageIO.write(bufferedImage,format,response.getOutputStream());// 将文件流放入response中
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}*/
} catch (Exception e) {
// TODO: handle exception
}
}
前端视频代码
?
?
|