使用serlvet提供下载

例子如下:
 pulic class CodeReturn extends HttpServlet{

    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException , ServletException {
    response.setContentType("application/jar");
    ServletContext ctx=getServletContext();
    InputStream is=ctx.getResourceAsStream("/bookCode.jar") ;// Returns the resource located at
the named path as an InputStream object.

    int read=0;
    byte[] bytes=new byte[1024];    
    OutputStream os=response.getOuputStream();// Returns a ServletOutputStream suitable for writing binary data in the response,Provides an output stream for sending binary data to the client. A ServletOutputStream object is normally retrieved via the ServletResponse.getOutputStream() method. 
 while((read=is.read(bytes)!=-1){
    os.write(bytes,0,read);
}
    os.flush();
    os.close();
}
}

posted on 2007-11-01 16:57 刘铮 阅读(302) 评论(0)  编辑  收藏 所属分类: JSP and Servlet


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论