Download file method

1, from hdfs download the file stream, the main principle is to open the file generated byte array output stream, and then call the springResponseEntity

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity;
    @Override
    public ResponseEntity<byte[]> download(DownloadRequest downloadRequest) throws IOException {  List<ExportTaskVo> list = exportDAO.findExportTaskVoByUserTask3(  downloadRequest.getUserId(),  downloadRequest.getTaskId(),  downloadRequest.getTaskStartTS()  );   ByteArrayOutputStream bos = new ByteArrayOutputStream();  HttpHeaders headers = new HttpHeaders();  // 文件的属性,也就是文件叫什么吧  headers.setContentDispositionFormData("attachment", "blank");  headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);  if (list != null && !list.isEmpty()) {  ExportTaskVo taskVo = list.get(0);  HDFSUtils.initHDFS(hdfsURI, hdfsUser);  if (StringUtils.isEmpty(taskVo.getFileName())) {  logger.error("filePath 为空");  return null;  }  HDFSUtils.httpDownLoad(taskVo.getFileName(), bos);  String filePath = taskVo.getFileName();  String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);  logger.info("fileName:" + fileName);  headers.setContentDispositionFormData("attachment", fileName);  }
       ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bos.toByteArray(), headers, HttpStatus.OK);  bos.close();  return responseEntity;

Guess you like

Origin www.cnblogs.com/gouhaiping/p/12098427.html