GWT 文件下载

public class Template implements EntryPoint {

    private String BASE_URL = GWT.getModuleBaseURL();

 

public void onModuleLoad() {

  ......

  displayTemplate()

}

 

    /**
     * Display template.
     */
    private void displayTemplate(){
         templateService.sendToTemplateServer(new AsyncCallback<List<TemplateModel>>(){
            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.toString());
            }
            @Override
            public void onSuccess(List<TemplateModel> templateModels) {
                for (TemplateModel templateModel : templateModels) {
                     final String url = BASE_URL

                                            +"readfile?templateName="+templateModel.getName().toString();
                    HTML template = new HTML("<a href='"+url+"'>"

                                             +templateModel.getName()+"</a>"+"<br/>");

                    //把模板添加到已存在的Panl中
                    app.getTreePanel().add(template);
                }
                app.getNavigationPanel().add(app.getTreePanel(),"Template");

            }
          });
    }   

 

servlet 中进行处理

public class OpenPageFile extends HttpServlet{

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        if ("".equals(req.getParameter("pageId"))) {
            throw new NullPointerException("page id shoult not null.");
        }
        int pageId = Integer.parseInt(req.getParameter("pageId"));       
        Page page = new Page();
        page.setId(pageId);
        IContextMenuService serivce = (IContextMenuService)SpringBeanFactory.getBean("contextMenuService");
        Page pageObject =     serivce.getPageById(page.getId());
        String pageContentName = pageObject.getPageContentName();
        if (pageContentName!= null && !"".equals(pageContentName)) {
            if (pageContentName.endsWith("xls")) {
                resp.setContentType("application/vnd.ms-excel");
            }else if (pageContentName.endsWith("xlsx")) {
                resp.setContentType("application/vnd.ms-excel");
            }else if (pageContentName.endsWith("pdf")) {
                resp.setContentType("application/pdf");
            }else if (pageContentName.endsWith("doc")) {
                resp.setContentType("application/msword");
            }else if (pageContentName.endsWith("docx")) {
                resp.setContentType("application/msword");
            }
        }else{
            resp.setContentType("application/unknown");
        }
       
        ServletOutputStream op = resp.getOutputStream();
        byte[] bytes = new byte[1024];
        if (null ==pageObject.getPageContent()) {
            String msg = "page file not exist";
            bytes = msg.getBytes();
        }else{
            bytes = pageObject.getPageContent();
        }
        op.write(bytes);
        op.close();
        resp.flushBuffer();
    }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值