下载图片或视频功能,若单独访问url可以下载视频文件,但是放再html的video src 上,后台反而会报错。请问要怎么样才能在界面播放视频呢
public void getFile(HttpServletRequest request, HttpServletResponse response, String fileName) {
InputStream is = null;
OutputStream os = null;
try {
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
if(VIDEOSUFFIX.contains(suffix)){
response.setContentType("video/avi");
fileName = fileName.replace(suffix,"avi");
}else if(IMAGESUFFIX.contains(suffix)){
response.setContentType("image//"+suffix);
}else{
throw new RuntimeException("文件类型错误");
}
File file = new File(uploadFilePath + File.separator + "runs" + File.separator + fileName);
response.addHeader("Content-Length", "" + file.length());
is = new FileInputStream(file);
os = response.getOutputStream();
IOUtils.copy(is, os);
} catch (Exception e) {
log.error("获取文件失败", e);
} finally {
if (null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>上传图片或视频到后台并预览</title>
</head>
<body>
<h1>上传图片或视频到后台并预览</h1>
<div id="previewContainer">
<video src="../getFile?fileName=b054799c-340f-48f3-b987-db93cd8ccb04.mp4" controls="" style="max-width: 500px; max-height: 500px;"></video>
</div>
</body>
</html>