文件下载的两种方式(直接通过浏览器下载和下载到指定位置)

最近做产品时有个下载视频的功能,用了两种方法,第一种方法是直接通过浏览器(有两种方式),其中直接通过浏览器下载-----第一种到最后显示失败,如下显示:

直接通过浏览器下载-----第二种方法下载成功,如下显示:

本然感觉两种方法差不多啊,求路过的各位大佬赐教

直接通过浏览器下载-----第一种:

public void downloadVideoById(Integer id,HttpServletResponse response) throws IOException {
        BufferedInputStream bis = null;
        OutputStream os = null;
        BehaviorDemoVideo video = behaviorDemoVideoMapper.findDemoVideoById(id);
        String videoPath = ConstansPropertity.videoPath;//路径
        String videoName = video.getFile();//文件名
        File file = new File(videoPath, videoName);
        if(file.exists()){
            try {
                String showName = video.getName()+video.getFile().substring(video.getFile().lastIndexOf("."));
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/"+video.getFile().substring(video.getFile().lastIndexOf("."))+1);
                response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(showName,"UTF-8"));//下载时浏览器显示的名称
                byte[] buff = new byte[1024];
                os = response.getOutputStream();
                bis = new BufferedInputStream(new FileInputStream(new File(videoPath+videoName)));
                int i = bis.read(buff);
                while (i != -1) {
                    os.write(buff, 0, buff.length);
                    os.flush();
                    i = bis.read(buff);
                }
            } catch (IOException e) {
                logger.error(e.getMessage());
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        logger.error(e.getMessage());
                    }
                }
            }
        }
    }

直接通过浏览器下载-----第二种:

使用spring框架中的FileCopyUtils工具
public void downloadVideoById(Integer id,HttpServletResponse response) throws IOException {
        BehaviorDemoVideo video = behaviorDemoVideoMapper.findDemoVideoById(id);
        String videoPath = ConstansPropertity.videoPath;//路劲
        String videoName = video.getFile();//文件名
        File file = new File(videoPath, videoName);
        if(file.exists()){
            String showName = video.getName()+video.getFile().substring(video.getFile().lastIndexOf("."));
            //获取输入流对象(用于读文件)
            FileInputStream fis = new FileInputStream(new File(videoPath, videoName));
            //动态设置响应类型,根据前台传递文件类型设置响应类型
            response.setContentType("application/"+video.getFile().substring(video.getFile().lastIndexOf("."))+1);
            //设置响应头,attachment表示以附件的形式下载,inline表示在线打开
            response.setHeader("content-disposition","attachment;fileName="+URLEncoder.encode(showName,"UTF-8"));//下载时浏览器显示的名称
            //获取输出流对象(用于写文件)
            ServletOutputStream os = response.getOutputStream();
            //下载文件,使用spring框架中的FileCopyUtils工具
            FileCopyUtils.copy(fis,os);
        }
    }

请各位大佬指教!

第二种(直接下载到本地)

/**
     * 根据id下载演示视频
     * @param id
     * @return
     */
    @Override
    public StateMsg downloadVideoById(Integer id) {
        BehaviorDemoVideo video = behaviorDemoVideoMapper.findDemoVideoById(id);
        String videoPath = ConstansPropertity.videoPath;
        String videoName = video.getFile();
        // 待下载的视频
        File file = new File(videoPath, videoName);
        String showName = null;
        if(file.exists()){
            try {
                showName = video.getName()+video.getFile().substring(video.getFile().lastIndexOf("."));// 截取待下载视频后缀名并拼接新的视频名
                //获取输入流对象(用于读文件)
                FileInputStream in = new FileInputStream(file);
                // 文件保存路劲
                File dir = new File(ConstansPropertity.fileTemp);
                if(! dir.exists()){
                    dir.mkdirs();
                }
                FileOutputStream fos = new FileOutputStream(ConstansPropertity.fileTemp+showName);
                byte[] buffer = new byte[4096];
                int bytesRead = -1;
                while ((bytesRead = in.read(buffer)) != -1) {
                    fos.write(buffer, 0, bytesRead);
                    fos.flush();
                }
                in.close();
                fos.close();
            } catch (FileNotFoundException e) {
               logger.error(e.getMessage());
                return new StateMsg(false, "文件未找到!");
            } catch (IOException e) {
                logger.error(e.getMessage());
                return new StateMsg(false, "下载错误!");
            }
        }
        // new StateMsg(true, video.getName()+video.getFile().substring(video.getFile().lastIndexOf(".")))
        return new StateMsg(true, ConstansPropertity.ServerAddressUrl+Constans.RESOURSE_TEMP+showName);
    }
}

 

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值