java输出视频流

@RequestMapping(value = "/imageView1", method = {RequestMethod.GET})
public void imageView1(HttpServletRequest request,  HttpServletResponse httpServletResponse,String fileName){


byte[] bytes = file2byte("e:\\"+fileName+".mp4");


httpServletResponse.setContentType("application/octet-stream");
httpServletResponse.setContentLength(bytes.length);
try {
httpServletResponse.getOutputStream().write(bytes);
} catch (IOException e) {
System.out.println("IO异常----");
}
}






public byte[] file2byte(String filePath) {
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}

猜你喜欢

转载自blog.csdn.net/Fisher_yu01/article/details/80972027