您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > java web 实验8
实验报告课程名称Web程序设计实验题目实验八JSP中文件操作日期2017-6-10学生学号姓名班级实验目的:掌握JSP中怎样操作文件实验条件:电脑一台、能上网查阅资料。实验8-1代码:writeConent.jsp%@pagecontentType=text/html;charset=gb2312%jsp:useBeanid=fileBeanclass=mybean.data.Shiyan1_Beanscope=request/HTMLbodybgcolor=yellowfontsize=2formaction=writeFilemethod=post请选择一个目录:Selectname=fileDirOptionvalue=C:/1000C:/1000Optionvalue=D:/2000D:/2000Optionvalue=D:/1000D:/1000/Selectbr输入保存文件的名字:inputtype=textname=fileNamebr输入文件的内容:BRTextAreaname=fileContentRows=5Cols=38/TextAreabrinputtype=submitvalue=提交/formb文件的位置:jsp:getPropertyname=fileBeanproperty=filePath/,文件的名字:jsp:getPropertyname=fileBeanproperty=fileName/,文件的长度:jsp:getPropertyname=fileBeanproperty=fileLength/字节。/font/body/HTMLShiyan1_servlet.javapackagemyservlet.control;importmybean.data.Shiyan1_Bean;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassShiyan1_ServletextendsHttpServlet{publicvoidinit(ServletConfigconfig)throwsServletException{super.init(config);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding(gb2312);Shiyan1_BeanfileBean=newShiyan1_Bean();//创建Javabean对象request.setAttribute(fileBean,fileBean);StringfilePath=request.getParameter(fileDir);StringfileName=request.getParameter(fileName);StringfileContent=request.getParameter(fileContent);Filef=newFile(filePath,fileName);fileBean.setFileLength(fileContent.length());fileBean.setFilePath(filePath);//将数据存储在fileBean中fileBean.setFileName(fileName);try{FileOutputStreamoutput=newFileOutputStream(f);bytebb[]=fileContent.getBytes();output.write(bb,0,bb.length);output.close();}catch(IOExceptionexp){fileBean.setFileName(写文件失败+exp.toString());}RequestDispatcherdispatcher=request.getRequestDispatcher(writeContent.jsp);dispatcher.forward(request,response);}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}}Shiyan1_Bean.javapackagemybean.data;publicclassShiyan1_Bean{StringfilePath,fileName,fileContent;longfileLength;publicvoidsetFilePath(Stringstr){filePath=str;}publicStringgetFilePath(){returnfilePath;}publicvoidsetFileName(Stringstr){fileName=str;}publicStringgetFileName(){returnfileName;}publicvoidsetFileLength(longlen){fileLength=len;}publiclonggetFileLength(){returnfileLength;}}实验结果:实验8-2代码:readFile.jsp%@pagecontentType=text/html;charset=gb2312%HTMLbodybgcolor=cyanfontsize=2jsp:useBeanid=fileBeanclass=mybean.data.Shiyan2_Beanscope=request/formaction=readFileByLinemethod=postname=form输入文件的路径(如:d:/2000):inputtype=textname=filePathsize=12br输入文件的名字(如:Hello.java):inputtype=textname=fileNamesize=9brinputtype=submitvalue=读取name=submit/formbbr读入的内容:brbr%=fileBean.getContent()%/b/font/body/HTMLShiyan2_Bean.javapackagemybean.data;publicclassShiyan2_Bean{StringBuffercontent;//读入的内容publicvoidsetContent(StringBufferstr){content=str;}publicStringBuffergetContent(){returncontent;}}Shiyan2_Servlet.javapackagemyservlet.control;importmybean.data.Shiyan2_Bean;importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassShiyan2_ServletextendsHttpServlet{publicvoidinit(ServletConfigconfig)throwsServletException{super.init(config);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding(gb2312);Shiyan2_BeanfileBean=newShiyan2_Bean();//创建Javabean对象request.setAttribute(fileBean,fileBean);StringfilePath=request.getParameter(filePath);StringfileName=request.getParameter(fileName);booleanboo=filePath!=null&&filePath.length()0;boo=fileName!=null&&fileName.length()0;StringBufferbuffer=newStringBuffer();if(boo){RandomAccessFilein=null;try{in=newRandomAccessFile(filePath+/+fileName,rw);longlength=in.length();longposition=0;in.seek(position);while(positionlength){Stringstr=in.readLine();byteb[]=str.getBytes(iso-8859-1);str=newString(b);position=in.getFilePointer();buffer.append(br+str);}fileBean.setContent(buffer);}catch(IOExceptionexp){}}RequestDispatcherdispatcher=request.getRequestDispatcher(readFile.jsp);dispatcher.forward(request,response);}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}}实验结果:实验总结(结论或问题分析):这次的实验主要是对文件的操作,用jsp对文件进行读写,相对比较容易操作。实验成绩任课教师签名
本文标题:java web 实验8
链接地址:https://www.777doc.com/doc-4471840 .html