出题---扩展上传word文档并显示内容

本文介绍了一个基于Struts2框架实现的文件上传及解析系统。该系统能从Word文档中读取不同类型的试题(如单选题、多选题等),并将其转化为结构化的数据供后续使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

哐哐,前几天写了个文件上传,虽然上传上去了并且显示出来了,但是好像意义不太大,以为显示的内容是没任何格式的,乱七八糟,在丽丽姐姐的要求下,俺还是写了个比较完整版的出题小系统.首先看一下word文档中试卷格式:

T 标题 C语言程序设计第三次课堂测试 }} {{1<B>单选题 1. 下面函数调用语句含有实参的个数是 。 func( (exp1, exp2), (exp3, exp4,exp5) ) ; (A)1 (B)2 (C)4 (D)5 }} {{1<B>单选题 2、写出以下程序的运行结果 。 fun( int a , int b ) { if (a>b) return( a+b ) ; else return(a-b); } main( ) { int x=3, y=8, z=6, r; r= fun( fun(x , y), 2*z ); printf(“\n r= %5d\n ”, r ); } (A)-16 (B)-17 (C)16 (D)17 }} {{1<C>单选题 3、以下程序的正确运行结果是 。 # include “stdio.h” void num( ) { extern int x, y ; int a=15, b=10; x= a-b; y= a+b; } int x, y; main( ) { int a= 7, b=5; x= a +b; y= a-b; num( ); printf( “%d, %d \n”, x, y ); } (A)12,2 (B)不确定 (C)5,25 (D)1,12 }} {{ 2<A,B,D>多选题 1.下列关系表达式中结果为真的是 。 (A) 0!=1 (B)2 < = 8 (C)( a = 2*2 ) = =2 (D)y = ( 2+2 ) = = 4 }} {{2<A,B,C> 多选题 2.设:int x=3 , y=4 , z=5 ; 则下面表达式中值为 1的是 。<br/> (A) x && y (B) x<=y (C) x || y+z && y-z (D) ! ( ( x<y ) && !z || 1 ) }} {{3< 0>填充题 当a=3; b=2; c=1; 时,表达式 f=a>b>c 的值是___________. }} {{3< 1 >填充题 设a,b,c 均为int 型变量, 且 a=6, b=4, c=2, 则表达式 !( a - b) +c -1 && b+c/2 的值是_________. }} {{ 4< (ch>=’A’ && ch<=’Z’ ) ,(ch=ch-32) >多项填充题 以下程序的功能是:输入一个字符,如果它是一个大写字母,则将其转换为小写字母,若它是一个大写字母,则将其转换为小写字母,其他字符不变。请填空。 main( ) { char ch; scanf( “%c”, &ch ); if ( 【1】 ) ch = ch + 32; else if( ch>=’a’ && ch<=’z’ ) 【2】; printf( “%c”,ch); } }} {{ 5<T>是非题 1.这是是非题. }} {{ 5<T>是非题 }}


分析一下:一共五个类型的题,1-单选,2-多选,3-单项填空,4-多项填空,5-是非题.样式是: "题型标号<答案>题型说明题干(选项)".

通过上面的word上传文档已经可以得到word里面的内容,至于怎么处理,就是按照这样的需求做了,其实,更多运用的是正则表达式的方式去处理这个string.

主要思路:

(1)先将string用'{{'分开,完成一个题一个题分开了,放到List里面.

(2)遍历上面的List将每个题分成[答案][题干]([选项])

(3)分开后将这些对应的内容分别放到一个List<String[]>中,其中,list存放每个题,String[]存放题的不同部分,0-答案,1-题干,(2-A选项),(3-B选项),(4-C选项),(5-D选项)

(4)返回前台这个List<String[]>

(5)前台用遍历方式和EL表达式展示出来.不用用struts标签,因为格式会被屏蔽掉.

(6)要注意:word文档中的回车是'\n',但是显示在html中就要是'<br/>',所以,记得将\n替换掉.

最后是相关的程序了:

前台上传文件index.jsp:(和上一次的上传word文档并显示内容相同)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% 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>Demo of upload files</title> <script type="text/javascript"> function checkup(){ //提示信息 var div=document.getElementById("message"); if(document.form1.upload.value.length==0){ div.innerHTML="请选择要上传的文件"; document.form1.upload.focus(); return false; } return true; } </script> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style> *{ font-size:14px;} </style> </head> <body> <form action="UploadAction.action" name="form1" method="post" enctype="multipart/form-data"> <table> <tr> <td ></td> <td ><span id="message" style="font-size:16px;color:red;" > </span></td> </tr> <tr> <td style="line-height:30px;">选择上传的文件:</td> <td> <input type="file" id="uploadname" name="upload"></td> </tr> <tr> <td style="line-height:30px;"><input type="submit" name="button" id="button" value="提交" onClick="return checkup();"></td> <td><input type="reset" name="button2" id="button2" value="重置"></td> </tr> </table> </form> </body> </html>

web.xml:(主要是配置struts)

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>

struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package namespace="/" name="default" extends="struts-default"> <action name="UploadAction" class="com.up.action.UploadAction" > <!-- 将savePath设置为"/upload" --> <param name="savePath">/upload</param> <result name="show">/showc.jsp</result> <result name="error">/index.jsp</result> </action> </package> </struts>



比较重要的UploadAction.java:

package com.up.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import org.apache.struts2.ServletActionContext; import org.textmining.text.extraction.WordExtractor; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { List<String[]> listcontent1 = new ArrayList<String[]>();//最终单选 List<String[]> listcontent2 = new ArrayList<String[]>();//最终多选题 List<String[]> listcontent3 = new ArrayList<String[]>();//最终填空 List<String[]> listcontent4 = new ArrayList<String[]>();//最终多项填空 List<String[]> listcontent5 = new ArrayList<String[]>();//最终是非题 File upload; String uploadContentType; String uploadFileName; String str; private String savePath; public String execute() throws Exception{ /** * 文件上传 */ String doc = getFilename(getUploadFileName()); String filename =getTime() + "." + doc; //文件上传 //getSavepath()是获取struts.xml中传过来的值 FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + filename); FileInputStream fis = new FileInputStream(getUpload()); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer , 0 , len); } int ch = 0; String path = ServletActionContext.getServletContext().getRealPath("/upload"); try{ FileInputStream in = new FileInputStream (path + "\\" + filename); WordExtractor extractor = new WordExtractor(); str = extractor.extractText(in); // str = str.replaceAll("\n", "<br/>"); List<String> lists = new ArrayList<String>();//所有的 List<String[]> list1 = new ArrayList<String[]>();//单选 List<String[]> list2 = new ArrayList<String[]>();//多选 List<String[]> list3 = new ArrayList<String[]>();//填空 List<String[]> list4 = new ArrayList<String[]>();//多项填空 List<String[]> list5 = new ArrayList<String[]>();//是非题 String[] strs = str.split("\\{\\{"); for(String s : strs){ lists.add(s); } for(int i=0;i<lists.size();i++){ String s = lists.get(i); if(s.contains("1<")){ String[] ss = s.split("^1.*单选题{1}quot;); list1.add(ss); } if(s.contains("2<")){ String[] ss = s.split("^2.*多选题{1}quot;); list2.add(ss); } if(s.contains("3<")){ String[] ss = s.split("^3.*填充题{1}quot;); list3.add(ss); } if(s.contains("4<")){ String[] ss = s.split("^4.*多项填充题{1}quot;); list4.add(ss); } if(s.contains("5<")){ String[] ss = s.split("^5.*是非题{1}quot;); list5.add(ss); } } /** * 单选 */ for(int i = 0;i<list1.size();i++){ for(int j = 0;j<list1.get(i).length;j++){ String[] ti1 = new String[6]; //ti[0]答案,ti[1]题目,ti[2]A,ti[3]B,ti[4]C,ti[5]D String t = list1.get(i)[j]; int h = list1.get(i)[j].indexOf("<", 0); int k = list1.get(i)[j].indexOf(">",0); ti1[0] = list1.get(i)[j].substring(h+1, k);//在<>之间获取答案,然后去掉第一行 int m = t.indexOf("\n", 0);//获取第一行的索引,为了下面去掉第一行做准备 t = t.substring(m);//去掉第一行. t = t.replaceAll("\n", "<br/>"); t= t.replaceAll("\\}\\}", ""); String[] sti = t.split("[((]+[ABCD]+[))]"); if(sti.length==5){ for(int n = 0;n<sti.length;n++){ ti1[n+1] = sti[n].replaceAll("\n", "<br/>"); } listcontent1.add(ti1); }else{ System.out.println("文件格式错误"); } } } /** * 多选 */ for(int i = 0;i<list2.size();i++){ for(int j = 0;j<list2.get(i).length;j++){ String[] ti2 = new String[6]; //ti[0]答案,ti[1]题目,ti[2]A,ti[3]B,ti[4]C,ti[5]D String t = list2.get(i)[j]; int h = list2.get(i)[j].indexOf("<", 0); int k = list2.get(i)[j].indexOf(">",0); ti2[0] = list2.get(i)[j].substring(h+1, k);//在<>之间获取答案,然后去掉第一行 int m = t.indexOf("\n", 0);//获取第一行的索引,为了下面去掉第一行做准备 t = t.substring(m);//去掉第一行. t = t.replaceAll("\n", "<br/>"); t= t.replaceAll("\\}\\}", ""); String[] sti = t.split("[((]+[ABCD]+[))]"); if(sti.length==5){ for(int n = 0;n<sti.length;n++){ ti2[n+1] = sti[n].replaceAll("\n", "<br/>"); } listcontent2.add(ti2); }else{ System.out.println("文件格式错误"); } } } /** * 填空 */ for(int i = 0;i<list3.size();i++){ for(int j = 0;j<list3.get(i).length;j++){ String[] ti3 = new String[6]; //ti[0]答案,ti[1]题目,ti[2]A,ti[3]B,ti[4]C,ti[5]D String t = list3.get(i)[j]; int h = list3.get(i)[j].indexOf("<", 0); // int m = t.indexOf("\n", 0);//获取第一行的索引,为了下面去掉第一行做准备 // String temp = t.substring(1, m); // int k = 0; // for(int x=temp.length()-1;x > 0 ;x--){ // char[] c = temp.toCharArray(); // if(c[x]=='>') { // k = x; // break; // } // } int k = list3.get(i)[j].indexOf(">",0); ti3[0] = list3.get(i)[j].substring(h+1, k);//在<>之间获取答案,然后去掉第一行 int m = t.indexOf("\n", 0);//获取第一行的索引,为了下面去掉第一行做准备 t = t.substring(m);//去掉第一行. t = t.replaceAll("\n", "<br/>"); t= t.replaceAll("\\}\\}", ""); String[] sti = t.split("[((]+[ABCD]+[))]"); if(sti.length==1){ for(int n = 0;n<sti.length;n++){ ti3[n+1] = sti[n].replaceAll("\n", "<br/>"); } listcontent3.add(ti3); }else{ System.out.println("文件格式错误"); } } } /** * 多项填空 */ for(int i = 0;i<list4.size();i++){ for(int j = 0;j<list4.get(i).length;j++){ String[] ti4 = new String[6]; //ti[0]答案,ti[1]题目,ti[2]A,ti[3]B,ti[4]C,ti[5]D String t = list4.get(i)[j]; int h = list4.get(i)[j].indexOf("<", 0); int m = t.indexOf("\n", 0);//获取第一行的索引,为了下面去掉第一行做准备 String temp = t.substring(1, m); int k = 0; for(int x=temp.length()-1;x > 0 ;x--){ char[] c = temp.toCharArray(); if(c[x]=='>') { k = x; break; } } ti4[0] = list4.get(i)[j].substring(h+1, k);//在<>之间获取答案,然后去掉第一行 t = t.substring(m);//去掉第一行. t = t.replaceAll("\n", "<br/>"); t= t.replaceAll("\\}\\}", ""); String[] sti = t.split("[((]+[ABCD]+[))]"); if(sti.length==1){ for(int n = 0;n<sti.length;n++){ ti4[n+1] = sti[n].replaceAll("\n", "<br/>"); } listcontent4.add(ti4); }else{ System.out.println("文件格式错误"); } } } /** * 是非题 */ for(int i = 0;i<list5.size();i++){ for(int j = 0;j<list5.get(i).length;j++){ String[] ti5 = new String[6]; //ti[0]答案,ti[1]题目,ti[2]A,ti[3]B,ti[4]C,ti[5]D String t = list5.get(i)[j]; int h = list5.get(i)[j].indexOf("<", 0); int k = list5.get(i)[j].indexOf(">",0); ti5[0] = list5.get(i)[j].substring(h+1, k);//在<>之间获取答案,然后去掉第一行 int m = t.indexOf("\n", 0);//获取第一行的索引,为了下面去掉第一行做准备 t = t.substring(m);//去掉第一行. t = t.replaceAll("\n", "<br/>"); t= t.replaceAll("\\}\\}", ""); String[] sti = t.split("[((]+[ABCD]+[))]"); if(sti.length==1){ for(int n = 0;n<sti.length;n++){ ti5[n+1] = sti[n].replaceAll("\n", "<br/>"); } listcontent5.add(ti5); }else{ System.out.println("文件格式错误"); } } } }catch(Exception e){ e.printStackTrace(); } return "show"; } String getTime(){ /** * 获取时间来定义文件名称 * @return String as the name of the file */ SimpleDateFormat formatter_f = new SimpleDateFormat("yyyyMMddHHmmss"); Date now = new Date(); String time = formatter_f.format(now); return time; } String getTrueTime(){ /** * 获取当前时间 * @return String time */ SimpleDateFormat formatter_f = new SimpleDateFormat("yyyy-MM-dd"); Date now = new Date(); String time = formatter_f.format(now); return time; } String getFilename(String name){ /** * 获取文件名的后缀 * @return String */ int i = name.lastIndexOf(".")+1; return name.substring(i,name.length()); } //接受依赖注入的方法 public void setSavePath(String value) { this.savePath = value; } @SuppressWarnings("deprecation") private String getSavePath() throws Exception { return ServletActionContext.getRequest().getRealPath(savePath); } public void setUpload(File upload) { this.upload = upload; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public File getUpload() { return (this.upload); } public String getUploadContentType() { return (this.uploadContentType); } public String getUploadFileName() { return (this.uploadFileName); } public List<String[]> getListcontent1() { return listcontent1; } public void setListcontent1(List<String[]> listcontent1) { this.listcontent1 = listcontent1; } public List<String[]> getListcontent2() { return listcontent2; } public void setListcontent2(List<String[]> listcontent2) { this.listcontent2 = listcontent2; } public List<String[]> getListcontent3() { return listcontent3; } public void setListcontent3(List<String[]> listcontent3) { this.listcontent3 = listcontent3; } public List<String[]> getListcontent4() { return listcontent4; } public void setListcontent4(List<String[]> listcontent4) { this.listcontent4 = listcontent4; } public List<String[]> getListcontent5() { return listcontent5; } public void setListcontent5(List<String[]> listcontent5) { this.listcontent5 = listcontent5; } }


最后是显示showc.jsp:(其实显示也挺重要的,因为用到了循环)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% 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>Demo of upload files</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style> *{ font-size:14px;font-weight:bold;} </style> </head> <body> <FORM action="tree.jsp" method=post name=form> 一.单项选择.<br/><br/> <s:iterator value="#attr.listcontent1" id="strs"> 答案:${strs[0]} ${strs[1]} <INPUT type="radio" name="R" value="A">A.${strs[2]} <INPUT type="radio" name="R" value="B">B.${strs[3]} <INPUT type="radio" name="R" value="C">C.${strs[4]} <INPUT type="radio" name="R" value="D">D.${strs[5]} </s:iterator> 二.多项选择.<br/><br/> <s:iterator value="#attr.listcontent2" id="strs"> 答案:${strs[0]} ${strs[1]} <INPUT type="checkbox" name="R" value="A">A.${strs[2]} <INPUT type="checkbox" name="R" value="B">B.${strs[3]} <INPUT type="checkbox" name="R" value="C">C.${strs[4]} <INPUT type="checkbox" name="R" value="D">D.${strs[5]} </s:iterator> 三.填空.<br/><br/> <s:iterator value="#attr.listcontent3" id="strs"> 答案:${strs[0]} <br/>请填写答案:<input type="text"/>${strs[1]} </s:iterator> 四.多项填空.<br/><br/> <s:iterator value="#attr.listcontent4" id="strs"> 答案:${strs[0]} <br/>请填写答案:<input type="text"/> ${strs[1]} </s:iterator> 五.是非题.<br/><br/> <s:iterator value="#attr.listcontent5" id="strs"> 答案:${strs[0]} 请选择答案:<INPUT type="radio" name="R" value="Y">A.是 <INPUT type="radio" name="R" value="N">B.否 ${strs[1]} </s:iterator> </FORM> </body> </html>


这样,基本上一个比较好一点的上传和读取功能就先做好了,嘻嘻,记录记录~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值