使用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年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论