tapestry5.0 下载本文

评论 (0)

? 分类: Tapestry/Wicket/JSF2

?

2008-05-09 缩略显示

Tapestry4实现文件导出功能

关键字: 文件导出

@InjectObject(\public abstract HttpServletResponse getHttpServletResponse();

String filePath=null,fileName=null; try{ filePath=getDocFilePath(); fileName = getTitle() +\ getHttpServletResponse().setContentType( \ filePath =

ReadProperty.getInstance().getType(\ File file = new File(filePath); if (file.exists()) { try { FileInputStream fileIn = new FileInputStream(file); long l = file.length(); byte[] b = new byte[1024]; int k = 0; String str = new String(fileName.getBytes(\ getHttpServletResponse().setHeader( \ \\ while ((long) k < l) { int j = fileIn.read(b, 0, 1024); k += j;

getHttpServletResponse().getOutputStream() .write(b, 0, j); } getHttpServletResponse().getOutputStream().flush(); fileIn.close(); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println(\该文件不存在。\ } }catch(Exception e){ e.printStackTrace(); } }

附:servlet/jsp 获取绝对路径和相对路径 servlet中获得项目绝对路径 String

filePath=this.getServletConfig().getServletContext().getRealPath(\

根目录所对应的绝对路径:request.getServletPath();

文件的绝对路

径 :request.getSession().getServletContext().getRealPath(request.getRequestURI())

当前web应用的绝对路

径 :servletConfig.getServletContext().getRealPath(\

ServletContext对象获得几种方式:

Javax.servlet.http.HttpSession.getServletContext()

Javax.servlet.jsp.PageContext.getServletContext()

Javax.servlet.ServletConfig.getServletContext()

文件的绝对路