使用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 劉錚 閱讀(307) 評論(0) 編輯 收藏 所屬分類: JSP and Servlet