RAP 操作servlet实现Excel下载
程序员文章站
2022-05-18 11:44:02
...
RAP中操作servlet实现Excel下载
// servlet名 private final String SERVICE_HANDLER_ID="GridExportExcel"; public File exportExcel2Local(final File file){ File newFile=null; try{ RWT.getServiceManager().registerServiceHandler(SERVICE_HANDLER_ID,new IServiceHandler() { public void service() throws IOException, ServletException { HttpServletResponse response=RWT.getResponse(); OutputStream out=response.getOutputStream(); response.setContentType("application/force-download"); response.setHeader("Content-Disposition", "attachment; filename=" + new String(file.getName())); FileInputStream fis = new FileInputStream(file); byte[] bt = new byte[1024]; for (int size = 0; size != -1; size = fis.read(bt)) { out.write(bt, 0, size); out.flush(); } fis.close(); file.delete(); } }); Browser brower=new Browser(Display.getCurrent().getActiveShell(),SWT.NONE); brower.setUrl(getUrl(file.getCanonicalPath())); } catch(Exception ex){ ex.printStackTrace(); MessageDialog.openConfirm(new Shell(),"错误提示","出错了:"+ex.getMessage()); } return newFile; } public String getUrl( String token ) { StringBuffer url = new StringBuffer(); url.append( RWT.getRequest().getContextPath() ); url.append( RWT.getRequest().getServletPath() ); url.append( "?" ); url.append( IServiceHandler.REQUEST_PARAM ).append( "=" ).append( SERVICE_HANDLER_ID ); url.append( "&" ); url.append("file").append( "=" ).append( token ); int relativeIndex = url.lastIndexOf( "/" ); if( relativeIndex > -1 ) { url.delete( 0, relativeIndex + 1 ); } return RWT.getResponse().encodeURL( url.toString() ); }