解决在tinymce编辑器插入视频到正文后不能跳转播放的问题

问题:在其他软件中上传了视频文件,而后将此视频文件插入到正文中,此视频文件可以点击进度条跳转进度;而在知了(出现bug的这个软件)中上传了视频文件,而后将此视频文件插入到正文中。此视频文件无法点击进度条跳转进度。

需求:希望可以在知了中上传视频文件,将文件作为视频插入到正文中,且可以点击进度条进行跳转播放。

解决方法: 

  1. 首先是上传到其他软件的文件能正常播放就代表不是tinymce编辑器和video控件的问题
  2. 在知了中将附件视频添加到正文需要提供下载链接,因此需要排查下载文件方法的问题
  3. 附件的文件点击下载到本地却可以点击进度条跳转进度,就说明下载方法中响应头没有支持浏览器断点续传
  4. 所以需要给请求头加个response.setHeader("Accept-Ranges", "bytes")来支持浏览器断点续传 

完整的下载文件的代码:

    public void downloadFile(String filename, String id, Boolean isImage, HttpServletResponse response) {
        log.info(this.getClass().getName() + "----in----" + "下载文件(downloadFile)" + "----");
        String savePath = getSavePath();
        String orginFilePath = savePath + "/" + id;
        File file = new File(orginFilePath);
    
        if (file.exists()) {
            InputStream inputStream = null;
            BufferedOutputStream outputStream = null;
            try {
                inputStream = new FileInputStream(file);
                byte[] buffer = new byte[1024];
                int byteread;
                try {
                    response.reset();
                    if (isImage) {
                        // 图片直接输出到浏览器
                        String formatType = id.substring(id.lastIndexOf(".") + 1, id.length());
                        response.setContentType("image/" + formatType);
                    } else {

                        response.reset();

                        response.setHeader("Accept-Ranges", "bytes");
                        // 附件执行下载任务
                        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
//                        response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(filename, "UTF-8"));
//                        response.setContentType("application/octet-stream");
                        response.setContentType("video/mp4");

                        // 设置视频文件长度
                        response.setContentLength((int) file.length());
                    }
//                    URL url = new URL("http://filem.free4inno.com/project/downloadFile?id=879");
//                    URLConnection conn = url.openConnection();
//                    inputStream= conn.getInputStream();

                    outputStream = new BufferedOutputStream(response.getOutputStream());
                    while ((byteread = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, byteread);
                    }
                    inputStream.close();
                    outputStream.flush();
                    outputStream.close();
                    log.info(this.getClass().getName() + "----out----" + "文件下载完毕" + "----");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    if (outputStream != null) {
                        outputStream.flush(); // 刷新一下
                        outputStream.close();
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        } else {
            response.reset();
            try {
                response.sendError(404,"文件不存在");
                log.info(this.getClass().getName() + "----out----" + "文件不存在" + "----");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值