gradle依賴
//hadoop
compile("org.apache.hadoop:hadoop-client:3.3.1")
HdfsUtil類
public class HdfsUtil
{
private FileSystem fileSystem;
public HdfsUtil()
{
init();
}
private void init()
{
Configuration configuration = new Configuration();
try
{
fileSystem = FileSystem.get(new URI("hdfs://IP:PORT"), configuration, "op");
}
catch (IOException e)
{
log.error(e.getMessage());
log.error("HDFS連接異常");
}
catch (InterruptedException e)
{
log.error(e.getMessage());
log.error("HDFS中斷異常");
}
catch (URISyntaxException e)
{
log.error(e.getMessage());
log.error("URI語法異常");
}
}
}
讀取二進(jìn)制文件方法類
public byte[] downloadFile(String path)
{
byte[] b = null;
try
{
//獲取文件
FSDataInputStream fsDataInputStream = fileSystem.open(new Path(path));
BufferedInputStream bufferedInputStream = new BufferedInputStream(fsDataInputStream);
//設(shè)置byte長(zhǎng)度
int len = bufferedInputStream.available();
b = new byte[len];
//賦值
bufferedInputStream.read(b);
bufferedInputStream.close();
fsDataInputStream.close();
fileSystem.close();
}
catch (IOException e)
{
log.error(e.getMessage());
log.error("獲取HDFS文件失敗");
}
return b;
}
提供下載接口
@GetMapping("URL")
public ResponseEntity<Object> downFileHdfs(@PathVariable String fileName)
{
HdfsUtil hdfsUtil = new HdfsUtil();
byte[] content = hdfsUtil.downloadFile("path"+fileName);
if (content == null)
{
return null;
}
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", String.format("attachment;filename=%s",fileName));
headers.add("Cache-Control", "no-cache,no-store,must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
ResponseEntity<Object> responseEntity = ResponseEntity.ok()
.headers(headers)
//設(shè)置長(zhǎng)度
.contentLength(content.length)
.contentType(MediaType.parseMediaType("application/octet-stream"))
//因?yàn)榉椒ㄊ欠盒?,所以直接傳? .body(content);
return responseEntity;
}