springcloud 返回流案例
消费者(必须post请求)
public void exportDwzzExcel(@RequestBody String data,HttpServletRequest request, HttpServletResponse servletResponse){
JSONObject json=JSONObject.parseObject(data); String dwzz_id=json.getString("dwzz_id");
Response response1 = zfglService.exportDwzzExcel(servletResponse,dwzz_id); Response.Body body = response1.body(); InputStream fileInputStream = null; OutputStream outStream; try { String fileName = new String(("对外转账详情信息.xlsx").getBytes("utf-8"), "ISO-8859-1"); servletResponse.addHeader("Content-Disposition", "attachment;filename=" + fileName); servletResponse.setContentType("application/vnd.ms-excel;charset=gb2312");
fileInputStream = body.asInputStream(); outStream = servletResponse.getOutputStream();
/*File fosfile = new File("D:\\test2.png"); if(!fosfile.exists()){ fosfile.createNewFile(); } FileOutputStream fos = new FileOutputStream(fosfile);*/
byte[] bytes = new byte[1024]; int len = 0; while ((len = fileInputStream.read(bytes)) != -1) { //fos.write(bytes,0,len); outStream.write(bytes, 0, len); } //fos.close(); fileInputStream.close(); outStream.close(); outStream.flush(); } catch (Exception e) { } }
熔断机制
public Response exportDwzzExcel(HttpServletResponse response,String dwzz_id) { Response response1 = yhywClient.exportDwzzExcel(response,dwzz_id); return response1; }
Feign
@GetMapping("/zfgl/exportDwzzExcel") Response exportDwzzExcel(@RequestParam(value = "response") HttpServletResponse response,@RequestParam(value = "dwzz_id") String dwzz_id);
生产者
public void exportDwzzExcel( HttpServletResponse response,String dwzz_id) throws Exception { /* File file = new File("F:\\小憩\\Map.png"); InputStream fileInputStream = new FileInputStream(file); System.out.println(fileInputStream.available()); OutputStream outStream; try { outStream = response.getOutputStream(); File fosfile = new File("D:\\test.png"); if(!fosfile.exists()){ fosfile.createNewFile(); } FileOutputStream fos = new FileOutputStream(fosfile); byte[] bytes = new byte[1024]; int len = 0; while ((len = fileInputStream.read(bytes)) != -1) { fos.write(bytes,0,len); outStream.write(bytes, 0, len); } fileInputStream.close(); fos.close(); outStream.close(); outStream.flush(); System.out.println(outStream); } catch (IOException e) { e.printStackTrace(); }*/
HSSFWorkbook workbook=null; try { workbook = zfglService.exportDwzzExcel(dwzz_id); try { //workbook将Excel写入到response的输出流中,供页面下载 OutputStream os = response.getOutputStream(); workbook.write(os); if(workbook!=null){ workbook.close(); } if(os!=null){ os.close(); os.flush(); } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }