下载文件返web端流

下载数据
@ApiOperation(value = "水面csv数据下载" ,response = MeteorWaterData.class,responseContainer = "List") @RequestMapping(value = "Water/{usaf}/data/csv", method = RequestMethod.GET) public void all2(HttpServletRequest request,HttpServletResponse response, @ApiParam("气象站USAF编号") @PathVariable(value = "usaf") String usaf, @ApiParam("查询起始时间") @RequestParam(value = "start_date", defaultValue = "0", required = false) long startDate, @ApiParam("查询结束时间") @RequestParam(value = "end_date", defaultValue = "0", required = false) long endDate, @ApiParam("路径") @RequestParam(value = "path", required = false) String path ){ try { response.setContentType("text/csv;charset=utf-8"); response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); List<MeteorWaterData> meteorGroundDataListCsv = meteorWaterDataNewService.getPagedSoundingCsvData(startDate, endDate, usaf); //返回前段 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("Warte.csv", "GB2312")); PrintWriter out = response.getWriter(); //标题头 out.write("sm_usaf,sm_date,sm_temperature,sm_surface_temperature,sm_rh ,sm_wind_dir,sm_wind_speed," + "sm_mixing_height,sm_below_rate, sm_above_rate ,sm_wave_period ,sm_wave_height"); out.write("\n"); for(MeteorWaterData date : meteorGroundDataListCsv){ out.write(date.getSmUsaf()+","+ date.getSmDate()+","+ date.getSmTemperature()+","+ date.getSmSurfaceTemperature()+","+ date.getSmRh()+","+ date.getSmWindDir()+","+ date.getSmWindSpeed()+","+ date.getSmMixingHeight()+","+ date.getSmBelowRate()+","+ date.getSmAboveRate()+","+ date.getSmWavePeriod()+","+ date.getSmWaveHeight()+"\n"); } out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); }

 返回文件

 @ApiOperation(value = "地面csv数据导出"
          ,response = MeteorGroundData.class,responseContainer = "List")
  @RequestMapping(value = "Ground/{usaf}/data/csv", method = RequestMethod.GET)
  public void all(HttpServletRequest request,HttpServletResponse response,
    @ApiParam("气象站USAF编号")
    @PathVariable(value = "usaf") String usaf,
    @ApiParam("查询起始时间")
    @RequestParam(value = "start_date", defaultValue = "0", required = false) long startDate,
    @ApiParam("查询结束时间")
    @RequestParam(value = "end_date", defaultValue = "0", required = false) long endDate,
    @ApiParam("路径")
    @RequestParam(value = "path", required = false) String  path
  ){
	
	  //换行符
      final String NEW_LINE = "\n";
      //文件名称D:	est\2017-06-28\ground.csv
      String fileName = null;
      if(path == null || path==" "){
    	  fileName = "地址+ground.csv";
      }else{
    	  fileName = path+"\\ground.csv";
      }
     
      System.out.println(fileName);
      try {
    	  response.setContentType("text/html;charset=utf-8");
    	  response.setCharacterEncoding("utf-8");
    	  request.setCharacterEncoding("utf-8");
    	  List<MeteorGroundData> meteorGroundDataListCsv = meteorGroundDataNewService.getPagedGroundCsvData(startDate, endDate, usaf);
          //标题头
          String title = " 区站ID,区站号/观测平台标识 (字符), 年 月 日 时次, 气压 , 海平面气压 ,最高气压,"
          		       + " 最低气压, 最大风速 ,极大风速 ,极大风速的风向(角度),10分钟平均风向(角度),10分钟平均风速,"
          		       + "最大风速的风向(角度),温度/气温  ,最高气温,最低气温,相对湿度 ,水汽压 , 最小相对湿度, 降水量";
           
          StringBuilder csvStr = new StringBuilder();
          csvStr.append(title).append(NEW_LINE);
          for(MeteorGroundData csvData : meteorGroundDataListCsv){
              csvStr.append(csvData.getId()).append(",")
              .append(csvData.getDmUsaf()).append(",")
              .append(csvData.getDmDate()).append(",")
              .append(csvData.getDmQiya()).append(",")
              .append(csvData.getDmHpmQiya()).append(",")
              .append(csvData.getDmMaxQiya()).append(",")
              .append(csvData.getDmMinQiya()).append(",")
              .append(csvData.getDmMaxFengsu()).append(",")
              .append(csvData.getDmMaximumFengsu()).append(",")
              .append(csvData.getDmMaximumWindJiaodu()).append(",")
              .append(csvData.getDmMeanWindJiaodu()).append(",")
              .append(csvData.getDmMeanFengsu()).append(",")
              .append(csvData.getDmMaxWindJiaodu()).append(",")
              .append(csvData.getDmTemp()).append(",")
              .append(csvData.getDmMaxTemp()).append(",")
              .append(csvData.getDmMinTemp()).append(",")
              .append(csvData.getDmRelativeShidu()).append(",")
              .append(csvData.getDmWaterQiya()).append(",")
              .append(csvData.getDmMinRelativeShidu()).append(",")
              .append(csvData.getDmRainfall()).append(NEW_LINE);
          }
           //返回oi
          //写文件
          Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), "GB2312"));
          writer.write(csvStr.toString());
          writer.flush();
          writer.close();
          response.getWriter().print(writer);
      } catch (Exception e) {
          e.printStackTrace();
      }
     
      
  }

 

posted @ 2017-06-30 17:08  八角虫  阅读(212)  评论(0编辑  收藏  举报