springmvc框架下hdfs下载文件流直接发送httpresponse
来源:互联网 发布:数据库系统的三级模式 编辑:程序博客网 时间:2024/06/05 05:19
1、首先是通过hdfs上的路径或者inputstream:
public InputStream downLoadFile(final String video_unique, String hdfsPath) throws IOException {FileSystem fileSystem = FileSystem.get(conf);InputStream inputStream = fileSystem.open(new Path(hdfsPath));return inputStream;}
2、然后用一个接口调用的这个方法(我自己的需求你们可以省略):
public InputStream doVideoDownload(String video_unique) throws IOException {HDFSUtils hdfsUtils = new HDFSUtils();String hdfsPath = getVideoInfoFieldByVideoUnique(video_unique).getUpload_url() + "/" + getVideoInfoFieldByVideoUnique(video_unique).getVideo_name();InputStream inputStream = hdfsUtils.downLoadFile(video_unique, hdfsPath);return inputStream;}
3、controller访问:
@RequestMapping(value = "/fileDownload", method = { RequestMethod.POST, RequestMethod.GET }) @ResponseBody public Object fileDownloadDo(String video_unique, HttpServletResponse response) throws IllegalStateException, IOException { response.setCharacterEncoding("utf-8");response.setContentType("multipart/form-data"); InputStream inputStream = null; inputStream = iVideoCloudService.doVideoDownload(video_unique); OutputStream os = response.getOutputStream();byte[] b = new byte[4096];int length;while ((length = inputStream.read(b)) > 0) {os.write(b, 0, length);}os.close();inputStream.close(); return null; }
4、跟上传文件一样配置dispatchservlet,不过这里我上传文件配置了进度监听,所以是自己的类:
<!-- SpringMVC上传文件时,为了获取上传进度,装载自定义MultipartResolver处理器 --><bean id="multipartResolver" class="com.eshore.storage.utils.CustomMultipartResolver"> <property name="maxUploadSize" value="10000000000"/> <property name="maxInMemorySize" value="4096"/> <property name="defaultEncoding" value="UTF-8"></property> </bean>
5、当然,也可以吧文件读写的程序放在接口实现里,相应的controller也修改并简化:
public Object doVideoDownload(String video_unique, OutputStream outputStream) throws IOException {HDFSUtils hdfsUtils = new HDFSUtils();String hdfsPath = getVideoInfoFieldByVideoUnique(video_unique).getUpload_url() + "/" + getVideoInfoFieldByVideoUnique(video_unique).getVideo_name();InputStream inputStream = hdfsUtils.downLoadFile(video_unique, hdfsPath);byte[] b = new byte[4096];int length;while ((length = inputStream.read(b)) > 0) {outputStream.write(b, 0, length);}outputStream.close();inputStream.close();return null;}
@RequestMapping(value = "/fileDownload", method = { RequestMethod.POST, RequestMethod.GET }) @ResponseBody public Object fileDownloadDo(String video_unique, HttpServletResponse response) throws IllegalStateException, IOException { response.setCharacterEncoding("utf-8");response.setContentType("multipart/form-data"); OutputStream outputStream = response.getOutputStream(); return iVideoCloudService.doVideoDownload(video_unique, outputStream); }
1 0
- springmvc框架下hdfs下载文件流直接发送httpresponse
- 基于springMvc框架下的文件下载
- springMVC + hadoop + httpclient 文件上传请求直接写入hdfs
- Springmvc 生成并且下载文件--直接在浏览器下载。
- SpringMVC下的下载文件实现
- SpringMVC下打包文件的下载
- 客户端下载HDFS文件
- springmvc框架下使用java mail发送邮件
- ssh框架之springmvc文件下载功能代码
- SpringMVC框架实现文件、资源的上传和下载
- 在SpringMVC框架中实现文件上传和下载
- SpringMVC下http和https路径下的文件下载
- 直接下载文件
- 直接下载文件
- spring 直接下载文件
- C# 直接下载文件
- java直接下载文件
- 直接打开下载文件
- 证书过期:The certificate used to sign "###"...
- tcp三次握手,Wireshark实践
- 深入浅出 RecyclerView
- 立足GitHub学编程:13个不容错过的Java项目
- Android源码编译环境的配置以及源码编译(find7)
- springmvc框架下hdfs下载文件流直接发送httpresponse
- WebRTC中peerconnectclient&server 简析【转】
- Spring事务管理
- hdoj3018 Ant Trip 欧拉回路
- Java API之输入输出流
- c语言常用检查清单
- [技巧]用WinRAR解压7z.001格式的文件
- android studio 的project structure选项打不开怎么办?
- Amcharts属性的含义