springmvc框架下hdfs下载文件流直接发送httpresponse
2016-08-03 09:25
232 查看
1、首先是通过hdfs上的路径或者inputstream:
2、然后用一个接口调用的这个方法(我自己的需求你们可以省略):
3、controller访问:
4、跟上传文件一样配置dispatchservlet,不过这里我上传文件配置了进度监听,所以是自己的类:
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;
}
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); }
相关文章推荐
- SpringMvc框架Java文件流下载。
- 使用JAVA发送HTTP请求(Http Request),返回HTTP响应(Http Response)内容,代码程序例子及原理说明
- Response下载Content-type:HTTP相应的Content类型(转贴)
- HttpRequest获取文件流,HttpResponse输出文件流
- #.net 中的 HttpWebRequest 和 HttpWebResponse 类发送客户端证书
- HTTP协议处理框架 - 最简单的GET请求拼装和Response解析
- 直接用socket实现HTTP下载
- httpwebresponse 异步: request.BeginGetRequestStream 报错! 无法发送具有此谓词类型的内容正文。
- Winform窗体中发送HTTP请求 手工发送HTTP请求主要是调用 System.Net的HttpWebResponse方法
- 通过设置http的头信息实现服务器不保存文件直接下载
- C#中用HttpWebRequest/HttpWebResponse来发送/接收数据
- httpwebresponse 异步: request.BeginGetRequestStream 报错! 无法发送具有此谓词类型的内容正文。
- tomcat rar 压缩包 http 下载 乱码问题(即直接被浏览器打开) 解决方法
- tomcat rar 压缩包 http 下载 乱码问题(即直接被浏览器打开) 解决方法
- 直接用socket实现HTTP下载
- http路径jsp直接下载文件
- 用httpwebrequest/httpwebresponse向服务器POST发送及接收数据
- 【转】直接用socket实现HTTP下载
- .net 中的 HttpWebRequest 和 HttpWebResponse 类发送客户端证书
- 【RLIB】C++调用HttpRequest 、HttpResponse下载远程HTTP/HTTPS资源