流文件下载问题
使用uni.downloadFile下载视频流文件,一直没法下载,把url换成静态的又可以。
一直在改请求方面的代码,但是都不行,后来发现是服务端配置问题。
后台:
1 @ApiOperation(value = "根据疑情Id查看视频", notes = "根据疑情Id查看视频") 2 @PostMapping("/likeVideo") 3 public void likeVideo(@RequestHeader Integer groupId,@RequestHeader Integer userId, DangerIdVo dangerIdVo, HttpServletResponse httpServletResponse) { 4 Integer dangerId = dangerIdVo.getDangerId(); 5 DangerLog dangerLog=dangerLogService.selectByDangerId(groupId,dangerId,userId); 6 if(dangerLog==null){ 7 throw new ServiceException(ServiceExceptionEnum.NOT_JWT); 8 } 9 String video = dangerLog.getVideo(); 10 //响应文件流准备 11 File file = new File(video); 12 FileInputStream fileInputStream = null; 13 //response响应输出流 14 ServletOutputStream servletOutputStream = null; 15 //响应Http信息头准备 16 httpServletResponse.addHeader("Content-Disposition", "attachment;filename=\""+file.getName()+"\""); 17 httpServletResponse.addHeader("Content-Length",file.length()+""); 18 httpServletResponse.setContentType("application/octet-stream"); 19 try{ 20 //输出流信息准备 21 servletOutputStream = httpServletResponse.getOutputStream(); 22 //文件流准备,打开NIO流管道 23 fileInputStream = new FileInputStream(file); 24 //调用方法 25 servletOutput(fileInputStream, servletOutputStream); 26 } catch (FileNotFoundException e) { 27 throw new ServiceException(ServiceExceptionEnum.NOT_VEDIO); 28 }catch (Exception e){ 29 System.out.println("servletOutputStream异常!"); 30 }finally{ 31 try { 32 servletOutputStream.close(); 33 fileInputStream.close(); 34 } catch (IOException e) { 35 System.out.println("文件流关闭异常!"); 36 } 37 } 38 }
前端:
1 uni.showModal({ 2 3 title: "提示", 4 5 content: "是否下载该视频", 6 7 showCancel: true, 8 9 cancelText: '取消', 10 11 confirmText: '下载', 12 13 success: res = >{ 14 15 if (res.confirm) { 16 17 //用户点击确定 18 constdownloadTask = uni.downloadFile({ 19 20 url: encodeURI(`$ { 21 apiUrl 22 }` + 23 24 '/api/downvideo-web/downVideo/likeVideo?dangerId=14081' + 25 26 '&&groupId=' + header().groupId + '&&userId=' + header().userId 27 28 ), 29 30 header: header(), 31 32 success: (data) = >{ 33 34 if (data.statusCode === 200) { 35 36 uni.saveImageToPhotosAlbum({ //需要保存到相册如果是下载文件用uni.saveFile() 37 filePath: data.tempFilePath, 38 //视频的本地临时地址 39 success: function(res) { 40 41 console.log('下载成功') 42 43 uni.showToast({ 44 45 title: '下载完成,请到相册查看', 46 47 position: 'center', 48 49 icon: 'none', 50 51 duration: 2000 52 53 }); 54 55 }, 56 57 fail: () = >{ 58 59 uni.showToast({ 60 61 title: '下载视频失败请重试', 62 63 position: 'center', 64 65 icon: 'none', 66 67 duration: 2000 68 69 }); 70 71 } 72 73 }); 74 75 } 76 77 }, 78 79 fail: (err) = >{ 80 81 uni.hideLoading() 82 83 uni.showToast({ 84 85 title: err, 86 87 position: 'center', 88 89 icon: 'none', 90 91 duration: 2000 92 93 }); 94 95 }, 96 97 complete: () = >{} 98 99 }); 100 101 downloadTask.onProgressUpdate((res) = >{ //下载进度 102 console.log('进度=' + res.progress) 103 104 }); 105 106 } 107 108 } 109 110 })

浙公网安备 33010602011771号