我现在想从hdfs上获取文件直接将获取到的文件返回给前端,请问要怎么实现? 文件不能存放到本地路径
java环境
9条回答 默认 最新
- 是派小星呀 2023-01-06 10:42关注
String fileName = "xxx"; response.reset(); response.setContentType("application/x-msdownload"); try { response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO-8859-1")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try ( BufferedInputStream bis = new BufferedInputStream("xxx"); // 输出流 BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()) ) { byte[] buff = new byte[1024]; int len = 0; while ((len = bis.read(buff)) > 0) { bos.write(buff, 0, len); } } catch (IOException e) { e.printStackTrace(); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报