现在的位置: 首页 > 综合 > 正文

写入文件以及解决文件名中文乱码问题

2018年05月05日 ⁄ 综合 ⁄ 共 1598字 ⁄ 字号 评论关闭

public ModelAndView expSequences(String id, HttpServletRequest request, HttpServletResponse response){
  logger.debug("导出序列号");
  response.setContentType("text/plain");
  BufferedOutputStream outStream = null;
  StringBuffer stringBuffer = new StringBuffer();
  ServletOutputStream serStream = null;
  String tab = "\t";  
        String enter = "\r\n";
        List<Passageploy> list = new ArrayList();
        Passageploy p = new Passageploy();
        int i = id.indexOf(",");
  if(i>0){
   String name="";
   try {
    name = new String(id.substring(i+1).getBytes("gb2312"),"iso8859-1");
   } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
   }
   if(name!=""){
    response.setHeader("Content-Disposition", "attachment;filename="+name+".xls");
    list=passageManager.expSequences(id.substring(0, i));
    p.setPaspName(id.substring(i+1));
    if(list!= null){
     try {
      serStream = response.getOutputStream();
      outStream = new BufferedOutputStream(serStream);
      stringBuffer.append("通道名称");
      stringBuffer.append(tab);
      stringBuffer.append("序列号");
      stringBuffer.append(tab);
      stringBuffer.append(enter);
      for(Passageploy pasp:list){
       stringBuffer.append(pasp.getPaspName());
       stringBuffer.append(tab);
       stringBuffer.append(pasp.getSeqId());
       stringBuffer.append(tab);
       stringBuffer.append(enter); 
      }
      outStream.write(stringBuffer.toString().getBytes("GB2312"));
      outStream.flush();
      outStream.close();
     } catch (IOException e) {
      logger.debug("导出序列号失败");
      e.printStackTrace();
     }
    }
   }
   
   insertPassageployMageLog(request,p,"导出-与通道策略相关联的序列号");
  }
  return null;
 
 }

抱歉!评论已关闭.