?? web158_com.java
字號:
in = new DataInputStream(new FileInputStream(file));
out = new DataOutputStream(clientSocket.getOutputStream());
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
out.flush();
in.close();
out.close();
System.out.println("文件發(fā)送完畢!");
clientSocket.close();
// return true;
} catch (Exception e) {
// TODO: handle exception
System.out.println("錯誤:服務(wù)器沒開啟" + e);
//return false;
}//處理其它不可想問題
}
//把考生的選擇題題目寫出
public String getChoose()
{
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String dbUrl="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=web158_exam/exam.dll";
Statement stmt=null;
Connection conn=null;
//需要返回的記錄集
String returnStr="以下為考生"+stuname+"("+stuid+")的試卷備份"+System.getProperty("line.separator")+System.getProperty("line.separator");
//加載驅(qū)動
//**************************
try
{
Class.forName(driver);
}
catch(java.lang.ClassNotFoundException nofound)
{
JOptionPane.showMessageDialog(null, "讀取試卷時數(shù)據(jù)驅(qū)動錯誤");
System.err.println(nofound.getMessage());
return "error" ;
}
//開始添加試題
//**********************************
try{
conn=DriverManager.getConnection(dbUrl,"web158.com","web158.com");
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from exam");
int rnum=0;
String ok="";
String s=System.getProperty("line.separator");
while(rs.next())
{
String t=rs.getString("answer");
String c=rs.getString("stuanswer");
if(t.equals(c))
{
rnum++;
ok="正確";
}
else
{
ok="錯誤";
}
returnStr+="第"+rs.getInt("id")+"題:"+s+s;
returnStr+=" "+rs.getString("question")+s+s;
returnStr+=" A:"+rs.getString("a")+s+s;
returnStr+=" B:"+rs.getString("b")+s+s;
returnStr+=" C:"+rs.getString("c")+s+s;
returnStr+=" D:"+rs.getString("d")+s+s;
returnStr+=" 正確答案:"+t+" 考生所選答案:"+c+" "+ok+s+s;
}
//操作題
rs=stmt.executeQuery("select * from operate");
//添加操作標(biāo)題
returnStr+=s+s+s+"以下為操作題,考生操作題答案在同文件夾中的answer.rar中:"+s+s+s;
while(rs.next())
{
String t=rs.getString("id");
String c=rs.getString("question");
returnStr+="第"+t+"題:"+s+s+" "+c+s+s;
}
//選擇題正確
returnStr+=s+s+"選擇題正確題數(shù):"+rnum+" 得分:"+rnum*prechoose+s+s+s;
//把全局分?jǐn)?shù)付值
score=rnum*prechoose;
//關(guān)閉連接
stmt.close();
conn.close();
return returnStr;
}//end of try
catch(Exception ef)
{
JOptionPane.showMessageDialog(null, "考試交卷初始化選擇題失敗");
System.out.println(ef.getMessage());
return "error";
}
finally
{
try
{
stmt.close();
conn.close();
}catch(Exception ef)
{}
}
}
//刪除殘余文件
public void delExt()
{
try
{
File file;
//先刪除D盤中的系統(tǒng)文件
deletefile("D:/myexam");
//刪除考生相關(guān)文件
file=new File("web158_exam/exam.dll");
if(file.exists())
{
file.delete();
}
file=new File("web158_exam/answer.dll");
if(file.exists())
{
file.delete();
}
}
catch(Exception ef)
{
System.out.print(ef.getMessage());
}
}
//刪除文件夾
public void deletefile(String delpath) throws FileNotFoundException,IOException
{
try
{
File file = new File(delpath);
if (!file.isDirectory())
{
file.delete();
}
else if (file.isDirectory())
{
String[] filelist = file.list();
for(int i = 0;i<filelist.length; i++)
{
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory())
delfile.delete();
else if (delfile.isDirectory())
deletefile(delpath+"\\"+filelist[i]);
}
file.delete();
}
}
catch (FileNotFoundException e)
{ }
}
//啟動服務(wù)器線程
public String startClient(String ip,int port,String name,String id,int receiveport)
{
String str="";
try
{
System.out.println("正在與服務(wù)器建立連接...");
socket = new Socket(ip,port); //建立和ServerSocket的連接
System.out.println("連接已經(jīng)建立");
out = new PrintWriter(socket.getOutputStream(),true);
//向服務(wù)器發(fā)送姓名,考號,命令
out.println(name+"#"+id+"#"+"web158");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(in.readLine()); //按行輸入
String order=in.readLine();
//服務(wù)器響應(yīng)接收傳來的文件
if(order.equals("noexit"))
{
JOptionPane.showMessageDialog(null, "您的考生信息不存在");
return "您的考生信息不存在";
}
//已經(jīng)取過試題
else if(order.equals("examed"))
{
JOptionPane.showMessageDialog(null, "您的試題已經(jīng)取過,請聯(lián)系監(jiān)考人員...");
return "你已經(jīng)參加過考試";
}
//服務(wù)器出錯
else if(order.equals("error"))
{
System.out.println("數(shù)據(jù)庫出錯");
JOptionPane.showMessageDialog(null, "服務(wù)器處理數(shù)據(jù)失敗,請聯(lián)系監(jiān)考人員...");
return "服務(wù)器處理數(shù)據(jù)失敗,請聯(lián)系監(jiān)考人員...";
}
//服務(wù)器生成試卷失敗
else if(order.equals("cerror"))
{
JOptionPane.showMessageDialog(null, "服務(wù)器生成隨機試卷時失敗,請聯(lián)系監(jiān)考人員...");
return "服務(wù)器生成隨機試卷時失敗,請聯(lián)系監(jiān)考人員...";
}
//服務(wù)器關(guān)閉
else if(order.equals("closeed"))
{
JOptionPane.showMessageDialog(null, "服務(wù)器已經(jīng)關(guān)閉");
return "服務(wù)器已經(jīng)關(guān)閉";
}
//服務(wù)器回復(fù)正在回發(fā)試題
else if(order.equals("web158"))
{
try
{
qpanel.top_info_text.setText("正在接收試卷...");
Thread.sleep(500);
clientReceive(ip,receiveport,"","","web158_exam/exam.dll");
qpanel.top_info_text.setText("正在接收考生身份信息...");
//接收圖片數(shù)據(jù)
Thread.sleep(500);
clientReceive(ip,receiveport,"","","web158_com_skin/"+id+".dll");
System.out.print("文件接收成功");
str="文件接收成功";
}
catch(Exception ef)
{
System.out.print(ef.getMessage());
str="接收試題失敗,請重試...";
}
//處理其它不可想情況
else if(order.equals("othererror"))
{
JOptionPane.showMessageDialog(null, "服務(wù)器出現(xiàn)未知錯誤");
return "服務(wù)器出現(xiàn)未知錯誤";
}
out.close();
in.close();
socket.close();
str="試題提取成功";
return str;
}
catch(IOException e)
{
System.out.println("服務(wù)器沒有開啟"+e.getMessage()+ip+":"+port);
return "提取試題失敗,與服務(wù)器:"+ip+":"+port+"建立連接失敗...";
//return e.getMessage();
}
finally
{
try
{
socket.close();
out.close();
in.close();
}
catch(Exception ef)
{}
}
}
//接收文件
public boolean clientReceive(String ip,int port,String name,String id,String filename){
try{
Socket clientSocket = new Socket(ip, port);
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
File file = new File(filename);
byte[] buffer = new byte[1024];
int length = 0;
if(!file.exists())
file.createNewFile();
FileOutputStream fileOut = new FileOutputStream(file);
while((length=in.read(buffer))!= -1){
fileOut.write(buffer, 0, length);
}
fileOut.close();
return true;
}catch (Exception e) {
// TODO: handle exception
System.out.println("總出錯,老子火了 :" +e.getMessage());
return false;
}
}
//
void web158_info()
{
// 取試題退出按鈕事件
qpanel.top_button_cancle.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
closetimer.start();
}
});
//取試題確定按鈕事件
qpanel.top_button_ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// 顯示處理中
qpanel.top_info_text.setText("系統(tǒng)處理中...");
String host=qpanel.top_host_input.getText();
int port=8888;
try
{
port=Integer.parseInt(qpanel.top_port_input.getText());
if(port<1000)
{
JOptionPane.showMessageDialog(null, "請輸入數(shù)字類型端口號");
qpanel.top_info_text.setText("端口號填寫不正確");
return;
}
}
catch(Exception ef)
{
JOptionPane.showMessageDialog(null, "請輸入數(shù)字類型端口號");
qpanel.top_info_text.setText("端口號填寫不正確");
return;
}
//考生姓名考證號
String name=qpanel.top_name_input.getText();
//考生號碼
String id=qpanel.top_id_input.getText();
//輸入姓名和考號有注入信息
if(name.indexOf("#")!=-1||name.indexOf("'")!=-1||id.indexOf("#")!=-1||id.indexOf("'")!=-1)
{
JOptionPane.showMessageDialog(null, "姓名和考號中請不要輸入“'”或“#”");
qpanel.top_info_text.setText("姓名和考號中請不要輸入“'”或“#”");
return;
}
//clientReceive(host,port,name,id);
//啟動服務(wù)器
//是否初始化試題
String str=startClient(host,port,name,id,receiveport);
System.out.println(str);
if(str.equals("試題提取成功"))
{
//把全局變量付值////////////////////////////////////////////////
serverip=host;
sendport=port;
stuname=name;
stuid=id;
//初始化按鈕////////////////////////////////////////////////////
qpanel.top_info_text.setText("試題領(lǐng)取成功,點擊確定開始答題");
//添加新事件
qpanel.top_begin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
txtinfo.setText("注意:\n現(xiàn)在答的是第一大題:選擇題\n請從A、B、C、D四個選項中選擇出正確答案,點擊上一題,下一題進行更改和答題");
//試題面板
getExamPanel();
//計時
timer.start();
return;
}
});//end of actionPerformed
qpanel.top_begin.setVisible(true);
qpanel.top_button_ok.setVisible(false);
qpanel.top_button_cancle.setVisible(false);
//更新考生信息
//頭象
headpic.setPicurl(id+".dll",name);
headpic.setPreferredSize(new Dimension(120,150));
pic_panel.removeAll();
pic_panel.add(headpic);
pic_panel.repaint();
pic_panel.validate();
//姓名
web158_stu_name.setText("考生姓名:"+name);
//全局變量付值
//準(zhǔn)考證號
web158_stu_id.setText("準(zhǔn)考證號:"+id);
//把四個表彰不可用
qpanel.top_id_input.setEnabled(false);
qpanel.top_name_input.setEnabled(false);
qpanel.top_host_input.setEnabled(false);
qpanel.top_port_input.setEnabled(false);
//寫出考試說明
txtinfo.setText("考生"+name+":\n\n請注意:\n試題已經(jīng)發(fā)下!\n請點擊開始答題進行答題!\n有不明白的問題請聯(lián)系監(jiān)考人員");
//返回
return ;
}//取試題失敗
else
{
//qpanel.top_info_text.setText("試題領(lǐng)取失敗,與服務(wù)器 "+host+":"+port+" 建立連接失敗,請重試...");
qpanel.top_info_text.setText(str);
return ;
}
}
});
//以下更新信息
web158_com_Cpanel.removeAll();
web158_com_Cpanel.add(qpanel);
web158_com_Cpanel.repaint();
web158_com_Cpanel.validate();
//m=m-1;
//timer.start();
}//end of web158_info
}//end of web158_frame
/********************************************************
* *
* 程序入口 *
* *
*********************************************************/
public class Web158_Com {
//點位
static Point origin = new Point();
//主入口
public static void main(String[] args) {
/*
//以下設(shè)置考生考試時不得做其它操作
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); //得到用戶屏幕大小
JFrame f=new JFrame();
f.setSize(screenSize.width,screenSize.height);
f.setUndecorated(true);
f.setVisible(true);
*/
//創(chuàng)建窗口對象
final web158_frame frame=new web158_frame();
//添加窗口移動事件
frame.addMouseListener (new MouseAdapter() {
public void mousePressed(MouseEvent e) {
origin.x = e.getX();origin.y = e.getY();
}
});//end of frame.addMouseListener ()
//可以拖動
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point p = frame.getLocation();
frame.setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y );
}
});//end of frame.addMouseMotionListener()
//添加最小化按鈕
frame.web158_com_system.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.setExtendedState(Frame.ICONIFIED|frame.getExtendedState());
}
});
//窗口移動事件完畢
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -