weixin_48484941 2023-07-06 18:22 采纳率: 62.5%
浏览 11
已结题

java html 播放视频后台报错问题

下载图片或视频功能,若单独访问url可以下载视频文件,但是放再html的video src 上,后台反而会报错。请问要怎么样才能在界面播放视频呢

img


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>

  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月16日
  • 已采纳回答 8月8日
  • 创建了问题 7月6日