使用HDFS文件系统在线浏览视频音频
JAVA API 读取hdfs系统文件
支持音频与视频
public void preview(String fpath, HttpServletRequest req, HttpServletResponse resp) throws IOException {
if (fpath == null)
return;
String filename = HADOOP_URL + fpath;
Configuration config = new Configuration();
FileSystem fs = null;
FSDataInputStream in = null;
try {
fs = FileSystem.get(URI.create(filename), config);
in = fs.open(new Path(filename));
} catch (IOException e) {
e.printStackTrace();
}
final long fileLen = fs.getFileStatus(new Path(filename)).getLen();
String range = req.getHeader("Range");
resp.setHeader("Content-type", "video/mp3");
OutputStream out = resp.getOutputStream();
if (range == null) {
filename = fpath.substring(fpath.lastIndexOf("/") + 1);
resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
resp.setContentType("application/octet-stream");
resp.setContentLength((int) fileLen);
IOUtils.copyBytes(in, out, fileLen, false);
} else {
long start = Integer.valueOf(range.substring(range.indexOf("=") + 1, range.indexOf("-")));
long count = fileLen - start;
long end;
if (range.endsWith("-"))
end = fileLen - 1;
else
end = Integer.valueOf(range.substring(range.indexOf("-") + 1));
String ContentRange = "bytes " + String.valueOf(start) + "-" + end + "/" + String.valueOf(fileLen);
resp.setStatus(206);
resp.setContentType("video/mpeg3");
resp.setHeader("Content-Range", ContentRange);
in.seek(start);
try {
IOUtils.copyBytes(in, out, count, false);
} catch (Exception e) {
throw e;
}
}
in.close();
out.close();
}
private String HADOOP_URL;
HDFS_PATH hdfs文件系