springmvc框架下hdfs下载文件流直接发送httpresponse

47 篇文章 0 订阅
38 篇文章 0 订阅

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);
    }



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值