首页 > 编程知识 正文

java实现文件下载的两种方式,springboot输出视频流

时间:2023-05-05 00:50:47 阅读:130179 作者:502

Springboot流媒体下载和预览文件1 .流媒体下载@ request mapping (value='/precede )、 method=request method.get (publicresponseentityinputstreamresourcetest 02 (string path ) throwsexception(/以下两行),在hdfs配置连接中} publicresponseentityinputstreamresourceprefile (字符串路径) throws exception (配置=新配置); file system=file system.get (new uri (HDFS _ path ),configuration,username ); sdatainputstreamin=file system.open (new path (path ); string filename=path.substring (path.lastindexof ('/' )1); //读取任何类型的文件都可以获得流的try { byte [ ] test bytes=new byte [ in.available (]; httpheadersheaders=newhttpheaders (; headers.add(cache-control )、) no-cache、no-store、must-revalidate ); headers.add (内容-位置),string.format ) attachment; fileName='%s',filename ); headers.add('pragma )、' no-cache ); headers.add('expires ','0' ); headers.add (内容语言,) UTF-8 ); //最终,此语句将文件内容转换为return ResponseEntity.ok ).headers(headers ).contentlength (test bytes.length ).contenttype ) } finally { } log.info (file is null ' filename ); 返回空值; }要点:

//设置附件类型为:附件头. add (内容分布)、string.format ) )附件; fileName='%s',filename ); //设置是application/octet-stream:未知类型,当然, 可以设置为特定类型的response entity.ok (.headers (headers ).contentlength ) testbytes.lleaders ).content type (介质类型) 1 .流预览文件(txt和图像) publicresponseentityinputstreamresourceprefile (string path ) throws exception (配置=sdatainputstreamin=file system.open (new path (path ); string filename=path.substring (path.lastindexof ('/' )1); //读取任何类型的文件都可以获得流的try { byte [ ] test bytes=new byte [ in.available (]; httpheadersheaders=newhttpheaders (; headers.add(cache-control )、) no-cache、no-store、must-revalidate ); headers.add (内容位置)、string.format )、inline; fileName='%s',filename ); headers.add('pragma )、' no-cache ); headers.add('expires ','0' ); headers.add (内容语言,) UTF-8 ); //最终,此语句必须指定文件类型才能以流形式输出文件内容! returnresponseentity.ok (.headers ).contentlength(testbytes.length ).contenttype ) media type.parse media } catch } finally { } log.info (file is null ' filename ); 返回空值; (重要信息(如果设置为在inline中嵌入文件,则支持的文件类型为txt和图像headers.add (' content-disposition ',string.format )、inline; fileName='%s',filename ); //特定文件类型returnresponseentity.ok (.headers (headers ).contentlength ) testbytes.length ).contenttype (介质类型) ) 获取内容类型(字符串文件名)//文件名后缀string file extension=filename.substring ) ) filename.las string if ('.BMP (if ).gif ).equalsignorecase (文件扩展) ) { return 'image/gif; }if ().JPEG ).equalsignorecase (文件扩展)|.jpg ).equalsignorecase (文件扩展)|).png ).equaaaase (if ) ).txt ).equalsignorecase (文件扩展) ) { return 'text/plain; (if ).VSD ).equalsignorecase (文件扩展) ) { return 'application/vnd.visio; (if ).PPT ).equalsignorecase (文件扩展)|PPTX ).equalsignorecase (文件扩展) ) return (应用) application ) if ().doc ).equalsignorecase (文件扩展) (docx ).equalsignorecase (文件扩展) ) return (应用程序) if }if ().doc ).equals ignore case (文件扩展) { return 'application/msword; (if ).docx ).equalsignorecase (文件扩展) ) return ' application/vnd.open XML formats-office document.word ding }返回'应用程序/octet-stream '; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。