public void doFile(){
UploadFile file = getFile("getfile");
File examFile = file.getFile();
GetQuestionFile ss = new GetQuestionFile();
boolean stat = ss.readTxtFile(examFile.toString());
if(stat){
renderEasyUISuccess("导入成功!");
} else {
renderEasyUIError("导入失败!");
}
}
------------------------------------------------------------------------
public boolean readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String line = null;
long tkzyId = 0L; //专业名称
long tkqustionTypeId=0L; //题目id
String tkquestion =null; //提干
List<String> tkoption = null; //选项
String tkanswer =null; //答案
String tkexplain = null; //答案解析
long questionId = 0L; //题目Id
while((line = bufferedReader.readLine()) != null){
//处理txt --begin
if (isBlankline(line)) {
//跳过空行
} else if (isHandline1(line)) {
String[] zymcs = StrKit.splitEx(line, "、");
String zymc = zymcs[1];
ResClassifyModel resModel = ResClassifyModel.dao.findFirst("SELECT * from ResClassify where name=?",zymc);
tkzyId=resModel.getId();
} else if (isHandline2(line)) {
String[] questions = StrKit.splitEx(line, "、");
String[] questionTypes = StrKit.splitEx(questions[1], "(");
tkqustionTypeId=changeQuestionType(questionTypes[0]);
} else if (isQuestion(line)) {
tkquestion = getQuestion(line);;
} else if (isAnswer(line)) {
if(tkoption==null){
tkoption= new ArrayList<String>();
}
tkoption.addAll(getAnswers(line));
} else if (isReferenceAnswer(line)) {
tkanswer = getReferenceAnswer(line);
tkexplain = getAnswerAnalysis(line);
questionId = IdKit.newUid();
CttQuestionModel model = new CttQuestionModel();
model.set("id", questionId);
model.setDescription(tkquestion);
model.setAnswer(tkanswer);
model.setIsEnable(true);
model.setDifficulty(2);
model.setExplain(tkexplain);
model.setQuestionTypeId(tkqustionTypeId);
model.setZyId(tkzyId);
model.setAddTime(new Date());
model.setAddUser(ShiroKit.getCurrentUser().getUserCode());
model.setIsShow(true);
model.save();
if(tkqustionTypeId==1L||tkqustionTypeId==2L){
CttQuestionOptionModel optionModel = new CttQuestionOptionModel();
if(tkoption!=null){
for (int j=0;j<tkoption.size();j++) {
optionModel.set("id", IdKit.newUid());
optionModel.set("questionId", questionId);
optionModel.set("xh", j+1);
optionModel.set("description", tkoption.get(j));
optionModel.set("sourceType", 1);
String[] ans = StrKit.splitEx(tkanswer, ",");
for (String str : ans) {
if(str.charAt(0) - ‘A‘==j){
optionModel.set("isRight", true);
}else{
optionModel.set("isRight", false);
}
}
optionModel.save();
}
tkoption = null;
}
}
}
//处理txt --end
}
read.close();
return true;
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static boolean isBlankline(String line) {
if (line.matches("^[ \\s]*$")) {
return true;
}
return false;
}
private static boolean isHandline1(String line) {
if (line.matches("^第[一-龥]+部分、[一-龥]+$")) {
return true;
}
return false;
}
private static boolean isHandline2(String line) {
if (line.matches("^[一-龥]+、[一-龥]+([0-9]+[大]?[题道])$")) {
return true;
}
return false;
}
private static boolean isQuestion(String line) {
if (line.matches("^\\d+[.\\.\\s]+.+$")) {
return true;
}
return false;
}
private static boolean isAnswer(String line) {
if (line.matches("^[A-Z][.\\.、\\s]+.+$")) {
return true;
}
return false;
}
private static boolean isReferenceAnswer(String line) {
if (line.matches("(?:参考)?答案[::\\s]+.+$")) {
return true;
}
return false;
}
public long changeQuestionType(String question){
long questionTypeId = 0L;
if(question.equals("单选题")){
questionTypeId=1L;
}else if(question.equals("多选题")){
questionTypeId=2L;
}else if(question.equals("填空题")){
questionTypeId=3L;
}else if(question.equals("判断题")){
questionTypeId=4L;
}else if(question.equals("简答题")){
questionTypeId=5L;
}
return questionTypeId;
}
//切割选项
private static List<String> getAnswers(String line) {
List<String> list = new ArrayList<String>();
String[] arr = line.split("[A-Z][.\\.\\s]+");
for (int i = 0; i < arr.length; i++) {
String answer = arr[i].trim();
if (StrKit.isEmpty(answer)) {
continue;
}
list.add(answer);
}
return list;
}
//切割题目
private static String getQuestion(String line) {
return line.replaceFirst("^\\d+[.\\.\\s、]+", "");
}
//答案解析切割
private static String getAnswerAnalysis(String line) {
return line.replaceAll("(?:参考)?答案[::\\s]+[A-Z×√]+[;;。.\\s]*(.*)$", "$1");
}
//切割答案
private static String getReferenceAnswer(String line) {
String s = line.replaceAll("(?:参考)?答案[::\\s]+([A-Z×√]+).*$", "$1");
if ("√".indexOf(s)>=0) {
return "1";
}else if("×".indexOf(s)>=0){
return "0";
}
String result = "";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (result.length()>0) {
result += ",";
}
result += String.valueOf(c);
}
return result;
}