佛山科学技术学院 实 验 报 告 课程名称 Web编程与设计 实验项目 实验二 在线教学网站的开发——上传、下载和用户会话的管理 专业班级 姓 名 学 号 指导教师 成 绩 日 期 一、实验目的 1、了解文件上传和下载的技术原理; 2、掌握会话机制及其实现方式; 3、掌握Servlet程序开发和调试的方法。 二、实验内容 在实验一所开发的在线教学网站系统的基础上,增添文件上传功能、文件下载功能和会话管理功能。 1)文件上传功能 实现将文件从本地上传到服务器固定目录下。 2)文件下载功能 实现从服务器固定目录下载文件到本地。 3)会话管理 当用户再次登录时,直接进入考试页面,不需要再次通过登录界面识别用户。 三、实验步骤及结果分析 (包括程序源代码及注释、程序使用/功能说明、运行结果截图——要在图中体现你的学号名字、结果分析等几部分内容。) 1)文件上传功能 2)文件下载功能 3)会话管理 不知道怎么展示动态过程 源代码 FileUploadServlet package com.kejian.servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; @WebServlet(name = "FileUploadServlet", urlPatterns = { "/fileUpload.do" }) @MultipartConfig(location = "D:\\", fileSizeThreshold = 1024) public class FileUploadServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 返回Web应用程序文档根目录 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); String path = ServletContext().getRealPath(""); Part part = Part("uploadfilename"); String message = ""; if (Size() > 1024 * 1024) { // 上传的文件不能超过1MB大小 part.delete(); message = "文件太大,不能上传!"; } else { path = path + "\\member\\123"; File f = new File(path); if (!f.exists()) { // 若目录不存在,则创建目录 f.mkdirs(); } String header = Header("content-disposition"); // System.out.println("Header = " + header); // System.out.println("增加后path = " + path); String fileName = header.substring(header.lastIndexOf("=") + 2, header.length() - 1); // System.out.println("index = "+ (header.lastIndexOf("\"")-1)); // System.out.println("last = "+ header.length()); System.out.println("filename="+fileName); part.write(path + "\\" + fileName); message = "文件上传成功!"; } request.setAttribute("message", message); RequestDispatcher rd = RequestDispatcher("/fileUpload.jsp"); rd.forward(request, response); } } FileDownloadServlet package com.kejian.servlet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.imageio.stream.FileImageInputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet(name = "FileDownloadServlet", urlPatterns = { "/fileDownload.do" }) @MultipartConfig(location = "D:\\", fileSizeThreshold = 1024) public class FileDownloadServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("image/jpeg; charset=UTF-8"); String path = ServletContext().getRealPath(""); // System.out.println("path = "+path); Part part = Part("downloadfilename"); path = path + "\\member\\123"; // System.out.println("增加后path = "+path); String header = Header("content-disposition"); // System.out.println("header = "+header); String fileName = header.substring(header.lastIndexOf("=") + 2, header.length() - 1); System.out.println("fileName= "+fileName); System.out.println(path + "\\"+fileName); File file = new File(path +"\\" +fileName); FileImageInputStream fs = new FileImageInputStream (file); int streamLength = (int)fs.length(); byte[] image = new byte[streamLength]; fs.read(image,0,streamLength ); fs.close(); response.setHeader("Content-Type","application/octet-stream"); response.setHeader("Content-Disposition","attachment;filename="+fileName); OutputStream().write(image); OutputStream().flush(); OutputStream().close(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } } DealUsernameServlet package com.kejian.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/DealUsernameServlet") public class DealUsernameServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String loginAccount = Parameter("loginAccount"); // System.out.println("传过两次的账号为"+ loginAccount); String loginPassword = Parameter("loginPassword"); String savetimeString = Parameter("savetime"); //最长时间 int saveTime = Integer.parseInt(savetimeString)*24*3600; // System.out.println("保存时间为"+ saveTime); //cookie Cookie loginInfo = new Cookie("loginInfo", loginAccount+","+loginPassword); //设置最长时间 loginInfo.setMaxAge(saveTime); //增加cookie response.addCookie(loginInfo); PrintWriter out = Writer(); // // //得到session对象,false为 如果不存在就不重新创建 // HttpSession session = Session(false); // if(session==null){ // //没有登录成功,跳转到登录页面 // response.ContextPath()+"/Login.jsp"); // } // // //取出会话数据 // String loginAccount = (Attribute("loginAccount"); // if(loginAccount==null){ // //没有登录成功,跳转到登录页面 // response.ContextPath()+"/Login.jsp"); // } // out.println("登陆成功,欢迎您" + loginAccount); response.setHeader("refresh", "1;url="+ContextPath()+"/test.jsp"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } } CheckFilter package com.kejian.servlet; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet Filter implementation class CheckFilter */ @WebFilter("/CheckFilter") public class CheckFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; //获取cookie 网站源码在线 Cookie[] cookies = Cookies(); String name = ""; String passwd = ""; if (cookies != null) { for (Cookie coo : cookies) { if (Name().equals("loginInfo")) { String[] info = Value().split(","); name = info[0]; passwd = info[1]; } } } // 如果在cookie中得到保存的用户名和密码,验证正确后,直接转到success页 if ("admin".equals(name) && "admin".equals(passwd)) { response.sendRedirect("test.jsp"); } else { arg2.doFilter(request, response); } } public void init(FilterConfig arg0) throws ServletException { } } 四、实验体会 代码要多调试才能有满意的结果 |
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论