您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 佛山科学技术学院-javaweb上传、下载和用户会话的管理-实验报告
佛山科学技术学院实验报告课程名称Web编程与设计实验项目实验二在线教学网站的开发——上传、下载和用户会话的管理专业班级姓名学号指导教师成绩日期一、实验目的1、了解文件上传和下载的技术原理;2、掌握会话机制及其实现方式;3、掌握Servlet程序开发和调试的方法。二、实验内容在实验一所开发的在线教学网站系统的基础上,增添文件上传功能、文件下载功能和会话管理功能。1)文件上传功能实现将文件从本地上传到服务器固定目录下。2)文件下载功能实现从服务器固定目录下载文件到本地。3)会话管理当用户再次登录时,直接进入考试页面,不需要再次通过登录界面识别用户。三、实验步骤及结果分析(包括程序源代码及注释、程序使用/功能说明、运行结果截图——要在图中体现你的学号名字、结果分析等几部分内容。)1)文件上传功能2)文件下载功能3)会话管理不知道怎么展示动态过程源代码FileUploadServletpackagecom.kejian.servlet;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;importjavax.servlet.annotation.*;@WebServlet(name=FileUploadServlet,urlPatterns={/fileUpload.do})@MultipartConfig(location=D:\\,fileSizeThreshold=1024)publicclassFileUploadServletextendsHttpServlet{publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//返回Web应用程序文档根目录request.setCharacterEncoding(utf-8);response.setContentType(text/html;charset=utf-8);response.setCharacterEncoding(utf-8);Stringpath=this.getServletContext().getRealPath();Partpart=request.getPart(uploadfilename);Stringmessage=;if(part.getSize()1024*1024){//上传的文件不能超过1MB大小part.delete();message=文件太大,不能上传!;}else{path=path+\\member\\123;Filef=newFile(path);if(!f.exists()){//若目录不存在,则创建目录f.mkdirs();}Stringheader=part.getHeader(content-disposition);//System.out.println(Header=+header);//System.out.println(增加后path=+path);StringfileName=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);RequestDispatcherrd=request.getRequestDispatcher(/fileUpload.jsp);rd.forward(request,response);}}FileDownloadServletpackagecom.kejian.servlet;importjava.io.File;importjava.io.IOException;importjava.io.PrintWriter;importjavax.imageio.stream.FileImageInputStream;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.annotation.MultipartConfig;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.Part;@WebServlet(name=FileDownloadServlet,urlPatterns={/fileDownload.do})@MultipartConfig(location=D:\\,fileSizeThreshold=1024)publicclassFileDownloadServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding(UTF-8);response.setCharacterEncoding(UTF-8);response.setContentType(image/jpeg;charset=UTF-8);Stringpath=this.getServletContext().getRealPath();//System.out.println(path=+path);Partpart=request.getPart(downloadfilename);path=path+\\member\\123;//System.out.println(增加后path=+path);Stringheader=part.getHeader(content-disposition);//System.out.println(header=+header);StringfileName=header.substring(header.lastIndexOf(=)+2,header.length()-1);System.out.println(fileName=+fileName);System.out.println(path+\\+fileName);Filefile=newFile(path+\\+fileName);FileImageInputStreamfs=newFileImageInputStream(file);intstreamLength=(int)fs.length();byte[]image=newbyte[streamLength];fs.read(image,0,streamLength);fs.close();response.setHeader(Content-Type,application/octet-stream);response.setHeader(Content-Disposition,attachment;filename=+fileName);response.getOutputStream().write(image);response.getOutputStream().flush();response.getOutputStream().close();}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}}DealUsernameServletpackagecom.kejian.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;@WebServlet(/DealUsernameServlet)publicclassDealUsernameServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding(UTF-8);response.setCharacterEncoding(UTF-8);response.setContentType(text/html;charset=UTF-8);StringloginAccount=request.getParameter(loginAccount);//System.out.println(传过两次的账号为+loginAccount);StringloginPassword=request.getParameter(loginPassword);StringsavetimeString=request.getParameter(savetime);//最长时间intsaveTime=Integer.parseInt(savetimeString)*24*3600;//System.out.println(保存时间为+saveTime);//cookieCookieloginInfo=newCookie(loginInfo,loginAccount+,+loginPassword);//设置最长时间loginInfo.setMaxAge(saveTime);//增加cookieresponse.addCookie(loginInfo);PrintWriterout=response.getWriter();//////得到session对象,false为如果不存在就不重新创建//HttpSessionsession=request.getSession(false);//if(session==null){////没有登录成功,跳转到登录页面//response.sendRedirect(request.getContextPath()+/Login.jsp);//}//////
本文标题:佛山科学技术学院-javaweb上传、下载和用户会话的管理-实验报告
链接地址:https://www.777doc.com/doc-6113568 .html