Java语言程序设计展示报告学院专业组长信息组员信息1组员信息2组员信息3指导教师日期年月日目录第一章选题概述 (1)第二章完成情况 (2)第三章成果展示 (3)一、登陆注册界面展示 (3)1、效果截图 (3)2、核心代码 (4)二、客户端界面展示 (5)1、效果截图 (8)2、核心代码 (8)三、服务端界面展示 (4)1、效果截图 (3)2、核心代码 (3)四、数据库直连和文件线程 (10)第四章总结与感受 (12)第一章选题概述在一学期的Java课程学习之后,我们对Java语言有了一定的认识与了解。
在此基础上我们选择了题目一作为本组的目标,题目的相关要求如下(初期拟完成目标):设计编程实现桌面文档共享与管理系统(100%)实现以下功能--1.能够实现文件的下载与上传(客户端与服务器端)2.能够通过安全认证与别人分享文档3.能形成“圈子”,对朋友或这陌生人设置不同的安全级别4.能够共享桌面与聊天5.必须用数据库存储信息6.必须用extends 和 implements 关键字7.必须用this关键字8.必须用图形用户界面9.界面设计与功能不限,充分发挥题目一比较有挑战性,题目中的要求几乎涉及到了我们学过的所有知识,除此之外还需要我们课外学习一些课堂额外的知识,比如数据库的创建于连接。
由组长XXX1负责整合所学容与指挥,组员查阅资料并编写程序,再和组长讨论并完成各自的模块设计。
我们设计了如下的流程图:图1-1 流程图第二章完成情况经过将近一个月左右的编程设计,我们实现的桌面文档共享与管理系统(90%)实现了以下功能--1.能够实现文件的上传(客户端与服务器端)2.能够通过安全认证与别人分享文档3.能形成“圈子”,对朋友或这陌生人设置不同的安全级别4.能够聊天并截取屏幕信息5.采用了数据库存储信息6.采用了extends 和 implements 关键字7.采用了this关键字8.采用了图形用户界面程序的编程设计图2-1 类成果展示图其中jp.java功能为截图;khd.java功能为建立客户端;sc.java功能为文件上传线程;zc.java功能为实现用户登录;Main功能为总体调用;fwq.java功能为建立服务端;User.java 功能为实现与数据库的连接(直连)。
第三章成果展示一、登录注册界面展示1.效果截图经过我们的编程设计,实现了用户的登录,其运行效果如图3-1所示、登陆成功效果如图3-2示、登陆未成功效果如图3-3示。
注册效果如图3-4所示,注册后数据库数据改变效果如图3-5示图3-1 登陆界面运行成功效果图图3-2 登陆成功效果图图3-3 登陆未成功效果图图3-4 注册界面效果图2.核心代码相关核心代码及其详细注解如下:package ceshi;import java.awt.*;import java.awt.event.*;import java.io.PrintWriter;import javax.swing.*;public class zc extends JFrame{User login_user=new User();JTextField j1=new JTextField();//用户名的文本框JPasswordField jpw=new JPasswordField();//密码的文本框 JButton jb1=new JButton("登录");JButton jb2=new JButton("注册");private Container a = getContentPane();public zc(){setLayout(null);setVisible(true);setBounds(600,320,400,160);setTitle("登录窗口");setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//控制窗口关闭//添加登录和注册的按钮jb1.setBounds(270,10,60,30);j1.setBounds(100,15,100,20);jpw.setBounds(100,60,100,20);a.add(j1);a.add(jpw);jb1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String name1=j1.getText();String password1=new String(jpw.getPassword());String password2=login_user.getPWD(name1);String security1=login_user.getSCT(name1);if(password2.trim().equals(password1.trim())){if(security1.trim().equals("管理员")){//System.out.println("3" +login_user.getPWD(name1));setVisible(false);JOptionPane.showMessageDialog(null, "恭喜您登陆成功!", "消息", RMATION_MESSAGE);new fwq();}else{setVisible(false);JOptionPane.showMessageDialog(null, "恭喜您登陆成功!", "消息", RMATION_MESSAGE);new khd();}}else{JOptionPane.showMessageDialog(null, "对不起您的用户名或密码错误!", "错误",JOptionPane.ERROR_MESSAGE);}}});jb2.setBounds(270,55,60,30);a.add(jb1);a.add(jb2);jb2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){Register c=new Register();}});//添加用户名和密码的标签JLabel jl1=new JLabel("用户名");jl1.setBounds(40,10,60,30);JLabel jl2=new JLabel("密码");jl2.setBounds(40,55,60,30);a.add(jl1);a.add(jl2);}public static void main(String argsp[]){ new zc();}}class Register extends JFrame{User Login_user = new User();JTextField jt1=new JTextField();JTextField jt2=new JTextField();JTextField jt3=new JTextField();JTextField jt4=new JTextField();JTextField jt5=new JTextField();JButton jb1=new JButton("注册");JButton jb2=new JButton("退出");JLabel jl1=new JLabel("用户名");JLabel jl2=new JLabel("密码");JLabel jl3=new JLabel("重新输入密码");JLabel jl4=new JLabel("邮箱");JLabel jl5=new JLabel("身份");JPanel jp=new JPanel();private Container a = getContentPane();public Register(){setLayout(null);setVisible(true);setTitle("注册");setBounds(750,320,300,250);jb1.setBounds(10,150,60,30);jb2.setBounds(140,150,60,30);jl1.setBounds(10,10,100,30);jl2.setBounds(10,35,100,30);jl3.setBounds(10,60,100,30);jl4.setBounds(10,85,100,30);jl5.setBounds(10,110,100,30);jt1.setBounds(120,15,130,20);jt2.setBounds(120,40,130,20);jt3.setBounds(120,65,130,20);jt4.setBounds(120,90,130,20);jt5.setBounds(120,115,130,20);a.add(jb1);a.add(jb2);a.add(jl1);a.add(jl2);a.add(jl3);a.add(jl4);a.add(jl5);a.add(jt1);a.add(jt2);a.add(jt3);a.add(jt4);a.add(jt5);jb1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String name1=jt1.getText();String password1=new String(jt2.getText());String rpassword1=new String(jt3.getText());String gender1=new String(jt4.getText());String security1=new String(jt5.getText());if (Login_user.jiancha(name1)==1){JOptionPane.showMessageDialog(null, "对不起您的用户名已被注册!", "错误",JOptionPane.ERROR_MESSAGE);return;}else if(name1.length()>10){JOptionPane.showMessageDialog(null,"用户名不能大于10位");return;}else if(password1.length()<6){JOptionPane.showMessageDialog(null,"密码不能小于6位");return;}else if (password1.equals(name1)){JOptionPane.showMessageDialog(null,"密码不能与用户名一致");return;}else if (!password1.equals(rpassword1)){JOptionPane.showMessageDialog(null,"两次输入的密码不一样 ");return;}Login_user.insert(name1,password1,rpassword1,gender1,security1);JOptionPane.showMessageDialog(null, "注册成功!");setVisible(false);}});jb2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){setVisible(false);}});}}二、客户端界面展示1.效果截图经过我们的编程设计,实现了客户端的相关操作,其运行效果如图3-6所示,能够实现连接服务端、断开,选择文件并上传文件、发送、截屏等操作图3-6 客户端界面图2.核心代码核心代码如下:package ceshi;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.TitledBorder; import ceshi.wjsc;import java.awt.*;import java.awt.event.*;import java.io.*;import .Socket;import java.util.HashMap;import java.util.Map;import java.util.StringTokenizer;public class khd extends JFrame{User Login_user=new User();JButton jb1=new JButton("发送");JButton jb2=new JButton("文件");JButton jb3=new JButton("连接");JButton jb4=new JButton("断开");static JButton jb5=new JButton("上传");JButton jb6=new JButton("截屏");static JTextArea jt1=new JTextArea(1,1);JTextArea jt2=new JTextArea(1,1);JTextField jt3=new JTextField("127.0.0.1");JTextField jt4=new JTextField("6999");JTextField jt5=new JTextField("d");JTextField jt6=new JTextField();JTextField jt7=new JTextField();JLabel jl1=new JLabel("聊天窗口");JLabel jl2=new JLabel("写消息");JLabel jl3=new JLabel("服务器IP");JLabel jl4=new JLabel("端口");JLabel jl5=new JLabel("昵称");JFileChooser j1 = new JFileChooser();static File fileFlag = new File("");static File file;private Socket socket;private Socket socket2;private PrintWriter writer;private BufferedReader reader;private MessageThread messageThread;// 负责接收消息的线程private Map<String, User> onLineUsers = new HashMap<String, User>();// 所有在线用户private boolean isConnected = false;private Container a = getContentPane();public khd(){setLayout(null);setVisible(true);setTitle("聊天室");setBounds(600,150,460,600);jl1.setBounds(10,50,100,20);JScrollPane js1 = new JScrollPane(jt1);jt1.setEditable(false);js1.setBounds(10,80,420,280);a.add(js1);a.add(jl1);jl2.setBounds(10,420,100,20);JScrollPane js2 = new JScrollPane(jt2);js2.setBounds(10,440,420, 80);a.add(jl2);jb1.setBounds(250,370,60,30); a.add(jb1);jb3.setBounds(310,370,60,30); a.add(jb3);jb4.setBounds(370,370,60,30); a.add(jb4);jb5.setBounds(190,370,60,30); a.add(jb5);jb6.setBounds(10,370,60,30); a.add(jb6);jb2.setBounds(70,370,60,30); a.add(jb2);jt6.setBounds(130,370,60,30); a.add(jt6);jt3.setBounds(60,10,100,20); a.add(jt3);jl3.setBounds(10,10,100,20); a.add(jl3);jt4.setBounds(200,10,100, 20);a.add(jt4);jl4.setBounds(168,10, 100, 20);a.add(jl4);jt5.setBounds(330,10,100, 20);jl5.setBounds(300,10, 100, 20);a.add(jl5);//按钮控制jb1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {send();}});jb2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){int n = j1.showOpenDialog(null);String filename = j1.getSelectedFile().toString();if (n == JFileChooser.APPROVE_OPTION) {jt6.setText(filename);fileFlag = new File(filename);file = j1.getSelectedFile();System.out.println(""+file);}}});jb3.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {int port;if (isConnected) {JOptionPane.showMessageDialog(null, "已处于连接上状态,不要重复连接!","错误", JOptionPane.ERROR_MESSAGE);return;}try {try {port = Integer.parseInt(jt4.getText().trim()); } catch (NumberFormatException e2) {throw new Exception("端口号不符合要求!端口为整数!");}String hostIp = jt3.getText().trim();String name = jt5.getText().trim();if (name.equals("") || hostIp.equals("")) {throw new Exception("、服务器IP不能为空!"); }boolean flag = connectServer(port, hostIp, name); if (flag == false) {throw new Exception("与服务器连接失败!");}setTitle(name);JOptionPane.showMessageDialog(null, "成功连接!"); } catch (Exception exc) {JOptionPane.showMessageDialog(null,exc.getMessage(),"错误", JOptionPane.ERROR_MESSAGE);}}});jb4.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (!isConnected) {JOptionPane.showMessageDialog(null, "已处于断开状态,不要重复断开!","错误", JOptionPane.ERROR_MESSAGE);return;}try {boolean flag = closeConnection();// 断开连接if (flag == false) {throw new Exception("断开连接发生异常!");}JOptionPane.showMessageDialog(null, "成功断开!"); } catch (Exception exc) {JOptionPane.showMessageDialog(null,exc.getMessage(),"错误", JOptionPane.ERROR_MESSAGE);}}});jb6.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {new jp();RectD rd = new RectD();GraphicsDevice gd =GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();gd.setFullScreenWindow(rd);}});}public synchronized boolean closeConnection() {try {sendMessage("CLOSE");// 发送断开连接命令给服务器 messageThread.stop();// 停止接受消息线程// 释放资源if (reader != null) {reader.close();}if (writer != null) {writer.close();}if (socket != null) {socket.close();}isConnected = false;return true;} catch (IOException e1) {e1.printStackTrace();isConnected = true;return false;}}public void sendMessage(String message) {writer.println(message);writer.flush();}public void send() {if (!isConnected) {JOptionPane.showMessageDialog(null, "还没有连接服务器,无法发送消息!", "错误",JOptionPane.ERROR_MESSAGE);return;}String message = jt2.getText().trim();if (message == null || message.equals("")) {JOptionPane.showMessageDialog(null, "消息不能为空!", "错误",JOptionPane.ERROR_MESSAGE);return;}sendMessage(getTitle() + "" + "ALL" + "" + message);jt2.setText(null);}public static void main(String args[]){new khd();}class MessageThread extends Thread {private BufferedReader reader;private JTextArea textArea;// 接收消息线程的构造方法public MessageThread(BufferedReader reader, JTextArea textArea) {this.reader = reader;this.textArea = textArea;}// 被动的关闭连接public synchronized void closeCon() throws Exception { // 清空用户列表// listModel.removeAllElements();// 被动的关闭连接释放资源if (reader != null) {reader.close();}if (writer != null) {writer.close();}if (socket != null) {socket.close();}isConnected = false;// 修改状态为断开}public void run() {String message = "";while (true) {try {message = reader.readLine();StringTokenizer stringTokenizer = new StringTokenizer(message, "/");String command = stringTokenizer.nextToken();// 命令if (command.equals("CLOSE"))// 服务器已关闭命令{textArea.append("服务器已关闭!\n");closeCon();// 被动的关闭连接return;// 结束线程} else if (command.equals("ADD")) {// 有用户上线更新在线列表String username = "";String userIp = "";if ((username =stringTokenizer.nextToken()) != null&& (userIp =stringTokenizer.nextToken()) != null) {User user = new User(username, userIp);onLineUsers.put(username, user);//listModel.addElement(username);}} else if (command.equals("DELETE")) {// 有用户下线更新在线列表String username =stringTokenizer.nextToken();User user = (User)onLineUsers.get(username);onLineUsers.remove(user);} else if (command.equals("USERLIST")) {// 加载在线用户列表int size = Integer.parseInt(stringTokenizer.nextToken());String username = null;String userIp = null;for (int i = 0; i < size; i++) {username =stringTokenizer.nextToken();userIp = stringTokenizer.nextToken();User user = new User(username, userIp);onLineUsers.put(username, user);//listModel.addElement(username);}} else if (command.equals("MAX")) {// 人数已达上限textArea.append(stringTokenizer.nextToken()+ stringTokenizer.nextToken() + "\n");closeCon();// 被动的关闭连接JOptionPane.showMessageDialog(null, "服务器缓冲区已满!", "错误",JOptionPane.ERROR_MESSAGE);return;// 结束线程} else {// 普通消息textArea.append(message + "\n");}} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}}public boolean connectServer(int port, String hostIp, String name){// 连接服务器try {socket = new Socket(hostIp, port);// 根据端口号和服务器ip建立连接socket2=new Socket(hostIp,port);writer = new PrintWriter(socket.getOutputStream());reader = new BufferedReader(newInputStreamReader(socket.getInputStream()));// 发送客户端用户基本信息(用户名和ip地址)sendMessage(name + "" +socket.getLocalAddress().toString());// 开启接收消息的线程messageThread = new MessageThread(reader, jt1);messageThread.start();new wjsc(socket2).start();//new Input(socket2).start();isConnected = true;// 已经连接上了return true;} catch (Exception e) {jt1.append("与端口号为:" + port + " IP地址为:" + hostIp+ " 的服务器连接失败!" + "\n");isConnected = false;// 未连接上return false;}}}三、服务端界面展示1.效果截图经过我们的编程设计,实现了服务端的相关操作,其运行效果如图3-5所示,能够实现通过自己的IP地址创造一个服务端。