此处是“教学课件”的下载,因此导航栏中教学课件的代码如下:
<ul id="sliderNav"> <li class="sliderTag"><h3>教学资源</h3></li> <li class="sliderTag"><a href="../../TeachResourceServlet?flag=jiaoxuedagang" target="main">教学大纲</a></li> <li class="sliderTag"><a href="../../TeachResourceServlet?flag=jiaoxuekejian" target="main">教学课件</a></li> <li class="sliderTag"><a href="p_jiaoXueLuXiang.jsp" target="main">教学录像</a></li> <li class="sliderTag"><a href="p_moNiShiTi.jsp" target="main">模拟试题</a></li> </ul>
TeachResourceServlet中的doPost()关键代码如下:
response.setContentType("text/html; charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); String flag=request.getParameter("flag"); List<FileUploadBean> flist=new ArrayList<FileUploadBean>(); FileUploadDao fd=new FileUploadDaoImpl();
if(flag.equals("jiaoxuekejian")){ flist=fd.load(); request.setAttribute("filelist",flist); request.getRequestDispatcher("pages/jiaoXueZiYuan/p_jiaoXueKeJian.jsp").forward(request, response); }
public List<FileUploadBean> load() { sql="select * from uploadfile"; try{ conn=DbUtil.getConn(); st=DbUtil.getStatement(conn); rs=DbUtil.getResultSet(st, sql); while(rs.next()){ FileUploadBean fd=new FileUploadBean(); fd.setId(rs.getInt("id")); fd.setFilename(rs.getString("filename")); fd.setUfiledescr(rs.getString("ufiledescr")); fd.setUperson(rs.getString("uperson")); fd.setUtime(rs.getString("utime")); list.add(fd); } }catch(Exception e){ e.printStackTrace(); } finally{ DbUtil.closeRs(rs); DbUtil.closeStmt(st); DbUtil.closeConn(conn); } return list; }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="com.xuan.vo.*,java.net.*;" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>教学课件</title> <link rel="stylesheet" href="css/jq.css" type="text/css" media="print, projection, screen" /> <link rel="stylesheet" href="css/style.css" type="text/css" media="print, projection, screen" /> <script type="text/javascript" src="js/jquery-1.1.3.js"></script> <script type="text/javascript" src="js/jquery.tablesorter.js"></script> <script type="text/javascript" src="js/jquery.tablesorter.pager.js"></script> <script type="text/javascript"> $(document).ready(function(){ //jQuery插件:Tablesorter 2.0 - 加jQuery分页插件控制表格分页显示 $(".table-04").tablesorter({widthFixed: true}).tablesorterPager({container: $("#pager")}); }); </script> </head> <body> <div id="main"> <table cellspacing="1" class="tablesorter table-04"> <caption><h3>通知公告</h3></caption> <thead> <tr><th>文件</th><th>发布人</th><th>发布时间</th><th>文件描述</th><th>操作</th></tr> </thead> <tfoot> <tr><th>文件</th><th>发布人</th><th>发布时间</th><th>文件描述</th><th>操作</th></tr> </tfoot> <tbody> <% List flist=(List)request.getAttribute("filelist"); FileUploadBean fb=new FileUploadBean(); %> <% for(int i=0;i<flist.size();i++){ fb=(FileUploadBean)flist.get(i); int id=fb.getId(); out.print("<tr><td>"+fb.getFilename()+"</td><td>"+fb.getUperson()+"</td><td>"+fb.getUtime()+"</td><td>"+fb.getUfiledescr()+"</td><td><a href=TeachResourceServlet?flag=kejiandownload&id="+id+">下载</a></td></tr>"); } %> </tbody> </table> <div id="pager" class="pager"> <form> <img src="images/first.png" class="first"/> <img src="images/prev.png" class="prev"/> <input type="text" class="pagedisplay"/> <img src="images/next.png" class="next"/> <img src="images/last.png" class="last"/> <select class="pagesize"> <option selected="selected" value="10">10</option> <option value="20">20</option> <option value="30">30</option> </select> </form> </div> </div> </body> </html>
if(flag.equals("kejiandownload")){ String idstr=request.getParameter("id"); int id=Integer.parseInt(idstr); flist=fd.loadById(id); String filename=flist.get(0).getFilename(); response.reset();//(加上就不会报Illegal...异常) response.setContentType("application/octet-stream"); /*String fileName=new String(request.getParameter("filename").getBytes("iso8859-1"));*/ File file = new File(getServletContext().getRealPath("/") + "upload/"+ filename); InputStream is = new FileInputStream(file); OutputStream os = response.getOutputStream(); response.addHeader("Content-Disposition", "attachment;filename =" + new String(file.getName().getBytes("utf-8"),"iso8859-1")); response.addHeader("Content-Length", new Long(file.length()).toString()); response.setCharacterEncoding("utf-8"); int a = 0; while((a = is.read()) != -1){ os.write(a); } is.close(); os.close(); }