薄雾倾城

导航

如何在dorado中自定义导出

原理:利用servlet,IO流,实现数据的传输。

1.dorado中创建RequestCommand,其中path指向servlet

2.创建导出servlet,并自定义导出方法

public class LoadFile extends HttpServlet {
 
  @Override
  public void init() {
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     String fileName = "******.txt";

   String str = "****************";//导出内容
     ServletOutputStream sos = null;
     try {//设置文件名编码格式
        fileName = java.net.URLEncoder.encode(fileName, "utf-8");
     } catch (UnsupportedEncodingException e1) {
        throw new RuntimeException("不支持的编码格式!!!");
     }

     try{
        response.reset();//必须加,不然保存不了临时文件
            response.setContentType("application/octet-stream");//设置文件内容格式,防止浏览器自动打开
            response.addHeader("Content-Disposition", "attachment; filename=" + fileName);//设置下载的文件名
        //输出流
        sos = response.getOutputStream();
        sos.write(str.getBytes());
     } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
     } finally{
        sos.flush();
        sos.close();
     }
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
    doGet(req, resp);
  }
}

3.在web.xml中配置servlet

 <servlet>
    <servlet-name>LoadFile</servlet-name>
    <servlet-class>com.fenet.insurance.mm.invoicemanage.action.LoadFile</servlet-class>

</servlet>

<servlet-mapping>
    <servlet-name>LoadFile</servlet-name>
    <url-pattern>/LoadFile</url-pattern>
 </servlet-mapping>

posted on 2011-10-14 11:17  若菲  阅读(1079)  评论(1编辑  收藏  举报