三、源代码以及执行结果截图: NewFile.jsp <%@ page import = "java.io.*" %> <%@ page contentType="text/html" %> <%@ page pageEncoding = "utf-8" %> <jsp:useBean id="english" class ="web.EnglishBean" scope="request"/> <style> #tom{ font-family:宋体;font-size:22;color:blue } </style> <HTML> <body id="tom" bgcolor="#ffccff"> <form action="readFile" id="tom" method="post"> 选择一篇课文:<br> <% File f = new File("."); String jspPage=request.getServletPath(); String webDir = request.getContextPath(); jspPage =jspPage.substring(1); webDir = webDir.substring(1); String path = f.getAbsolutePath(); int index = path.indexOf("bin"); String webRoot = request.getRealPath("/"); String filePath = webRoot + "/englishText"; File fileDir=new File(filePath); String name [] = fileDir.list(); %> <select id= tom name="fileName" size =1> <% for(int i=0;i<name.length;i++){ %> <option selected value='<%=name[i]%>'/><%=name[i]%> <% } %> </select> <input type=hidden name='filePath' value='<%=filePath%>' /> <br><input type=submit id=tom value="学习" /><br> <textArea id="tom" rows="15" cols="80"> <%=english.getFileContent()%> </textArea><br> <% if(english.getFileName().length()>0) { %><br> <%=english.getFileAudio()%>:<br> <embed src ='englishAudio/<%=english.getFileAudio()%>' height="56" autostart='false'> </embed> <%} %> </form> </body> </HTML> EnglishBean.java package web; public class EnglishBean { public String fileName=""; public String fileContent=""; public String fileAudio; public void setFileName(String str){ fileName=str; } public String getFileName(){ return fileName; } public void setFileContent(String str){ fileContent=str; } public String getFileContent(){ return fileContent; } public void setFileAudio(String str){ fileAudio = str; } public String getFileAudio(){ return fileAudio; } } ReadFile_Servlet.java package handle; import web.EnglishBean; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class eadFile_Servlet extends HttpServlet{ public void init(ServletConfig config) throws ServletException{ super.init(config); } public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ EnglishBean fileBean=new EnglishBean(); request.setAttribute("english",fileBean); String fileContent=""; request.setCharacterEncoding("utf-8"); String filePath=request.getParameter("filePath"); String fileName=request.getParameter("fileName"); if(filePath==null||fileName==null) return; if(fileName.length()==0||fileName.length()==0) return; fileBean.setFileName(fileName); String audioFileName = fileName.substring(0,fileName.lastIndexOf("."))+".mp3"; fileBean.setFileAudio(audioFileName); try{ File f=new File(filePath,fileName); RandomAccessFile randomAccess = new RandomAccessFile(f,"r"); String s=null; StringBuffer stringbuffer=new StringBuffer(); while ((s=randomAccess.readLine())!=null){ byte b[] = s.getBytes("iso-8859-1"); stringbuffer.append("\n"+new String(b)); } fileContent=new String(stringbuffer); fileBean.setFileContent(fileContent); } catch(Exception exp){ fileContent ="读取失败"+exp.toString(); } RequestDispatcher dispatcher= request.getRequestDispatcher("NewFile.jsp"); dispatcher.forward(request, response); } } web.xml <web-app> <servlet> <servlet-name>readFile</servlet-name> <servlet-class>handle.eadFile_Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>readFile </servlet-name> <url-pattern>/readFile </url-pattern> </servlet-mapping> </web-app> (空间不够,可加附页) 效果图展示 |