本次代码沿用第三次中的User.java Administrator.java Operator.java Browser.java和Doc.java
和第五次中的DataProcessing.java
并且借用大部分了老师提供的Client.java和Server.java代码
实验目的
了解Java网络编程基础知识;掌握java.net包中关于网络的基本类及其属性和方法;掌握基于Socket的客户和服务器编程方法。
实验内容
编写程序,将前面课程所编写的档案管理系统从单机版改编成为客户机/服务器模式,实现档案文件在客户机和服务器之间的上传、下载。
需要运行的主程序
public class WinMain {
public static void main(String args[]) {
Client application;
LoginWindow window=new LoginWindow();
window.loginFrame();
if ( args.length == 0 )
application = new Client( "127.0.0.1" );
else
application = new Client( args[ 0 ] );
application.runClient();
}
}
客户端
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.sql.Timestamp;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class Client extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static String user_role="";
private static JFrame Jframe;
private static DataOutputStream output;
private static DataInputStream input;
private String message = "";
private static String user_name="";
private static String user_password="";
private String chatServer;
private static Socket client;
private static int row;
private static int row2;
private static String[][] UserData;
private static String[][] DocData;
public Client( String host ){
super( "Client" );
}
public void runClient(){
try{
connectToServer();
getStreams();
processConnection();
}
catch ( EOFException eofException ) {
displayMessage( "Client terminated connection" );
}
catch ( IOException ioException ){
ioException.printStackTrace();
}
finally{
try {
closeConnection();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void connectToServer() throws IOException{
displayMessage( "Attempting connection" );
client = new Socket( InetAddress.getByName( chatServer ), 12345 );
displayMessage( "Connected to: " +
client.getInetAddress().getHostName() );
}
private void getStreams() throws IOException{
output = new DataOutputStream( client.getOutputStream() );
output.flush();
input = new DataInputStream( client.getInputStream() );
displayMessage( "Got I/O streams" );
}
private void processConnection() throws IOException{
do {
message = input.readUTF();
if(message.equals("LOGIN_TRUE")) {
user_role=input.readUTF();
MenuWindow menuWindow=new MenuWindow();
menuWindow.showMenu();
Jframe.dispose();
}
else if(message.equals("LOGIN_FALSE")) {
JOptionPane.showMessageDialog(null, "账号或密码错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
else if(message.equals("SELFCHANGE_TRUE")) {
JOptionPane.showMessageDialog(null, "修改成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
String password=input.readUTF();
user_password=password;
System.out.println("SELFCHANGE_SUCCESS");
}
else if(message.equals("SELFCHANGE_FALSE")) {
JOptionPane.showMessageDialog(null, "修改失败", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
else if(message.equals("displayedUser")) {
int i=0;
UserData=new String[50][3];
i=input.readInt();
for(int j=0;j>> CLIENT_FILE_DOWN")) {
String filename=input.readUTF();
Long fileLength=input.readLong();
FileOutputStream fos=new FileOutputStream(new File("C:\\downloadfile\\"+filename));
byte[] sendBytes=new byte[1024];
int transLen=0;
System.out.println("----开始下载文件<"+filename+">,文件大小为<"+fileLength+">----");
while(true) {
int read=0;
read=input.read(sendBytes);
if(read==-1) break;
transLen+=read;
System.out.println("下载文件进度"+100*transLen*1.0/fileLength+"%...");
fos.write(sendBytes,0,read);
fos.flush();
if(transLen>=fileLength) break;
}
System.out.println("----下载文件<"+filename+">成功----");
JOptionPane.showMessageDialog(null, "下载成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
Jframe.dispose();
}
} while ( !message.equals( "SERVER>>> TERMINATE" ) );
}
static void closeConnection() throws IOException{
displayMessage( "Closing connection" );
String logout="CLIENET>>> CLIENT_LOGOUT";
output.writeUTF(logout);
output.flush();
System.out.println(logout);
try{
output.close();
input.close();
client.close();
}
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
static void sendData( String message ){
try
{
output.writeUTF( "CLIENT>>> " + message );
output.flush();
displayMessage( "CLIENT>>> " + message );
}
catch ( IOException ioException )
{
System.out.println( "Error writing object" );
}
}
static void displayMessage( String messageToDisplay ){
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
System.out.println(messageToDisplay);
}
}
);
}
static void Login(String name,String password,JFrame frame) throws IOException {
String login="CLIENT>>> CLIENT_LOGIN";
output.writeUTF(login);
System.out.println(login);
output.flush();
output.writeUTF(name);
user_name=name;
output.flush();
output.writeUTF(password);
user_password=password;
output.flush();
Jframe=frame;
}
static void ChangeSelfInfo(String old_password,String new_password,String new_password2) throws IOException {
if(user_password.equals(old_password)) {
if(new_password.equals(new_password2)) {
String changeSelfInfo="CLIENT>>> CLIENT_SELF_MOD";
System.out.println("CLIENT>>> CLIENT_SELF_MOD");
output.writeUTF(changeSelfInfo);
output.flush();
output.writeUTF(user_name);
output.flush();
output.writeUTF(new_password);
output.flush();
output.writeUTF(user_role);
output.flush();
}
else {
JOptionPane.showMessageDialog(null, "两次输入的新密码不一致", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(null, "密码错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
}
static void Display_user() throws IOException {
output.writeUTF("displayUser");
output.flush();
}
static void Display_Doc() throws IOException {
output.writeUTF("displayDoc");
output.flush();
}
static void DelUser(String del_name, JFrame frame) throws IOException {
Jframe=frame;
if(del_name.equals(user_name)) {
JOptionPane.showMessageDialog(null, "无法删除", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
else {
output.writeUTF("USER_DELETE");
output.flush();
output.writeUTF(del_name);
output.flush();
System.out.println("CLIENT>>> "+del_name+ "USER_DELETE");
}
}
static void UpdateUser(String name,String password,String role,JFrame frame) throws IOException {
Jframe=frame;
output.writeUTF("USER_UPDATE");
output.flush();
output.writeUTF(name);
output.flush();
output.writeUTF(password);
output.flush();
output.writeUTF(role);
output.flush();
System.out.println("CLIENT>>> "+name+ "USER_UPDATE");
}
static void AddUser(String name, String password,String role,JFrame frame) throws IOException {
Jframe=frame;
output.writeUTF("USER_ADD");
output.flush();
output.writeUTF(name);
output.flush();
output.writeUTF(password);
output.flush();
output.writeUTF(role);
output.flush();
System.out.println("CLIENT>>> "+name+ "USER_ADD");
}
static void Download(String ID,JFrame frame) throws IOException {
Jframe=frame;
output.writeUTF("DOWNLOAD");
output.flush();
output.writeUTF(ID);
output.flush();
}
static void Upload(String ID,String Creator,String description,String filename,JFrame frame) throws IOException{
Jframe=frame;
output.writeUTF("UPLOAD");
output.flush();
output.writeUTF(ID);
output.flush();
output.writeUTF(Creator);
output.flush();
output.writeUTF(description);
output.flush();
File file=new File(filename.trim());
String fileName=file.getName();
output.writeUTF(fileName);
output.flush();
long fileLength=file.length();
output.writeLong(fileLength);
output.flush();
FileInputStream fis=new FileInputStream(file);
DataOutputStream dos=new DataOutputStream(client.getOutputStream());
byte[] sendBytes=new byte[1024];
int length=0;
while((length=fis.read(sendBytes,0,sendBytes.length))>0) {
output.write(sendBytes,0,length);
output.flush();
}
System.out.println("CLIENT>>> CLIENT_FILE_UP");
}
static int get_Rows() {
return row;
}
static int get_Rows2() {
return row2;
}
static String[][] get_Docs(){
return DocData;
}
static String[][] get_Users(){
return UserData;
}
static String get_Name() {
return user_name;
}
static String get_Role() {
return user_role;
}
}
服务端
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Enumeration;
import javax.swing.SwingUtilities;
public class Server{
private static DataOutputStream output;
private static DataInputStream input;
private ServerSocket server;
private Socket connection;
public Server(){
super();
}
public void runServer() throws SQLException{
try{
server = new ServerSocket(12345,100);
while ( true ){
try{
waitForConnection();
getStreams();
processConnection();
}
catch ( EOFException eofException ){
displayMessage( "\nServer terminated connection" );
}
finally {
closeConnection();
}
}
}
catch ( IOException ioException ){
ioException.printStackTrace();
}
}
private void waitForConnection() throws IOException{
displayMessage( "Waiting for connection" );
connection = server.accept();
displayMessage( "Connection "+ " received from: " +
connection.getInetAddress().getHostName() );
}
private void getStreams() throws IOException{
output = new DataOutputStream( connection.getOutputStream() );
output.flush();
input = new DataInputStream( connection.getInputStream() );
displayMessage( "Got I/O streams" );
}
private void processConnection() throws IOException, SQLException{
String message = "Connection successful";
sendData( message );
do{
message = input.readUTF();
if(message.equals("CLIENT>>> CLIENT_LOGIN")) {
String name=input.readUTF();
String password=input.readUTF();
try {
if(DataProcessing.search(name, password)!=null) {
output.writeUTF("LOGIN_TRUE");
output.flush();
String role=DataProcessing.search(name, password).getRole();
output.writeUTF(role);
output.flush();
System.out.println(message);
System.out.println(name);
System.out.println("SERVER>>> SERVER_LOGIN");
}
else {
output.writeUTF("LOGIN_FALSE");
output.flush();
}
}catch (SQLException e) {
e.printStackTrace();
}
}
else if(message.equals("CLIENT>>> CLIENT_SELF_MOD")) {
String name=input.readUTF();
String password=input.readUTF();
String role=input.readUTF();
System.out.println("CLIENT_SELF_MOD");
if(DataProcessing.update(name, password, role)==true) {
output.writeUTF("SELFCHANGE_TRUE");
output.flush();
output.writeUTF(password);
output.flush();
System.out.println("SERVER>>> SERVER_SELF_MOD");
}
else {
output.writeUTF("SELFCHANGE_FALSE");
output.flush();
}
}
else if(message.equals("displayUser")) {
Enumeration e=DataProcessing.getAllUser();
String[][] rowData=new String[50][3];
User user;
int i=0;
while(e.hasMoreElements()) {
user=e.nextElement();
rowData[i][0]=user.getName();
rowData[i][1]=user.getPassword();
rowData[i][2]=user.getRole();
i++;
}
output.writeUTF("displayedUser");
output.flush();
output.writeInt(i);
output.flush();
for(int j=0;j e=DataProcessing.getAllDocs();
String[][] rowData=new String[50][5];
Doc doc;
int i=0;
while(e.hasMoreElements()) {
doc=e.nextElement();
rowData[i][0]=doc.getID();
rowData[i][1]=doc.getCreator();
rowData[i][2]=doc.getTimestamp().toString();
rowData[i][3]=doc.getDescription();
rowData[i][4]=doc.getFilename();
i++;
}
output.writeUTF("displayedDoc");
output.flush();
output.writeInt(i);
output.flush();
for(int j=0;j>> "+name+" USER_DELETE");
}
else {
output.writeUTF("DELETE_FALSE");
output.flush();
}
}
else if(message.equals("USER_ADD")) {
String name=input.readUTF();
String password=input.readUTF();
String role=input.readUTF();
if(DataProcessing.insert(name, password, role)) {
output.writeUTF("ADD_TRUE");
output.flush();
System.out.println("SERVER>>> "+name+" USER_ADD");
}
else {
output.writeUTF("ADD_FALSE");
output.flush();
}
}
else if(message.equals("USER_UPDATE")) {
String name=input.readUTF();
String password=input.readUTF();
String role=input.readUTF();
if(DataProcessing.update(name, password, role)) {
output.writeUTF("UPDATE_TRUE");
output.flush();
System.out.println("SERVER>>> "+name+" USER_UPDATE");
}
else {
output.writeUTF("UPDATE_FALSE");
output.flush();
}
}
else if(message.equals("UPLOAD")) {
Timestamp timestamp=new Timestamp(System.currentTimeMillis());
String ID=input.readUTF();
String Creator=input.readUTF();
String description=input.readUTF();
String filename=input.readUTF();
Long fileLength=input.readLong();
FileOutputStream fos=new FileOutputStream(new File("C:\\uploadfile\\"+filename));
DataInputStream dis=new DataInputStream(connection.getInputStream());
byte[] sendBytes=new byte[1024];
int transLen=0;
System.out.println("----开始接收文件<"+filename+">,文件大小为<"+fileLength+">----");
while(true) {
int read=0;
read=input.read(sendBytes,0,sendBytes.length);
if(read<=0) break;
transLen+=read;
System.out.println("接收文件进度"+100*transLen*1.0/fileLength+"%...");
fos.write(sendBytes,0,read);
fos.flush();
if(transLen>=fileLength) break;
}
System.out.println("----接收文件<"+filename+">成功----");
if(DataProcessing.insertDoc(ID, Creator, timestamp, description, filename)){
output.writeUTF("UPLOAD_TRUE");
output.flush();
System.out.println("SERVER>>> CLIENT_FILE_UP");
}
else {
output.writeUTF("UPLOAD_FALSE");
output.flush();
}
}
else if(message.equals("DOWNLOAD")) {
String ID=input.readUTF();
output.writeUTF("SERVER>>> CLIENT_FILE_DOWN");
output.flush();
System.out.println("SERVER>>> CLIENT_FILE_DOWN");
String filename=DataProcessing.searchDoc(ID).getFilename();
output.writeUTF(filename);
output.flush();
String filepath="C:\\uploadfile\\";
File file=new File(filepath+filename);
long fileLength=file.length();
output.writeLong(fileLength);
output.flush();
FileInputStream fis=new FileInputStream(file);
byte[] sendBytes=new byte[1024];
int length=0;
while((length=fis.read(sendBytes,0,sendBytes.length))>0) {
output.write(sendBytes,0,length);
output.flush();
}
}
else displayMessage(message);
} while ( !message.equals( "CLIENT>>> CLIENT_LOGOUT" ) );
}
private void closeConnection(){
displayMessage( "Terminating connection" );
try{
output.close();
input.close();
connection.close();
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}
static void sendData( String message ){
try{
output.writeUTF( "SERVER>>> " + message );
output.flush();
displayMessage( "SERVER>>> " + message );
}
catch ( IOException ioException ){
System.out.println( "Error writing object" );
}
}
static void displayMessage( String messageToDisplay ){
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
System.out.println(messageToDisplay);
}
}
);
}
public static void main( String args[] ){
String driverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/document";
String user="root";
String password="123456";
try {
DataProcessing.connectToDatabase(driverName, url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
Server application = new Server();
try {
application.runServer();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class LoginWindow extends JFrame{
JTabbedPane tabbedPane;
public void loginFrame() {
JFrame frame=new JFrame();
frame.setTitle("系统登录");
frame.setSize(380,300);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel L1=new JLabel("账号:");
JTextField te1=new JTextField(25);
JLabel L2=new JLabel("密码:");
JPasswordField te2=new JPasswordField(25);
te2.setEchoChar('*');
L1.setFont(new Font("黑体",Font.PLAIN,18));
L1.setSize(50,30);
L1.setBounds(5+30,50,50,30);
te1.setFont(new Font("黑体",Font.PLAIN,18));
te1.setSize(250,30);
te1.setBounds(5+80,50,250,30);
L2.setFont(new Font("黑体",Font.PLAIN,18));
L2.setSize(50,30);
L2.setBounds(5+30,100,50,30);
te2.setFont(new Font("黑体",Font.PLAIN,18));
te2.setSize(250,30);
te2.setBounds(5+80,100,250,30);
frame.add(L1);
frame.add(te1);
frame.add(L2);
frame.add(te2);
JButton B1=new JButton("登录");
JButton B2=new JButton("退出");
B1.setSize(100,50);
B1.setBounds((380-100-5)/2-60,(300-50-30)/2+50,100,50);
B2.setSize(100,50);
B2.setBounds((380-100-5)/2+60,(300-50-30)/2+50,100,50);
frame.add(B1);
frame.add(B2);
B1.addActionListener(new ButtonHandler(frame,te1,te2));
B2.addActionListener(new ButtonHandler(frame));
frame.setVisible(true);
}
public class ButtonHandler implements ActionListener{
public JTextField te1=new JTextField();
public JPasswordField te2=new JPasswordField();
public JFrame frame=new JFrame();
ButtonHandler(JFrame frame){
this.frame=frame;
}
ButtonHandler(JFrame frame,JTextField te1,JPasswordField te2) {
this.frame=frame;
this.te1=te1;
this.te2=te2;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="登录") {
String name=te1.getText();
String password=String.valueOf(te2.getPassword());
try {
try {
Client.Login(name,password,frame);
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (HeadlessException e1) {
e1.printStackTrace();
}
}
else {
frame.dispose();
}
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MenuWindow {
public void showMenu() {
try {
Client.Display_Doc();
Client.Display_user();
} catch (IOException e2) {
e2.printStackTrace();
}
JFrame frame=new JFrame();
frame.setTitle("菜单界面");
frame.setSize(1600,1200);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setLayout(null);
JMenuBar menu=new JMenuBar();
JMenu menu1=new JMenu("用户管理");
JMenu menu2=new JMenu("文件管理");
JMenu menu3=new JMenu("密码管理");
JMenuItem item1=new JMenuItem("添加用户");
JMenuItem item2=new JMenuItem("修改用户");
JMenuItem item3=new JMenuItem("删除用户");
JMenuItem item4=new JMenuItem("上传文件");
JMenuItem item5=new JMenuItem("下载文件");
JMenuItem item6=new JMenuItem("修改密码");
JMenuItem item7=new JMenuItem("退出");
String role=Client.get_Role();
String name=Client.get_Name();
if(role.equals("administrator")) {
menu1.setEnabled(true);
menu2.setEnabled(true);
menu3.setEnabled(true);
item1.setEnabled(true);
item2.setEnabled(true);
item3.setEnabled(true);
item4.setEnabled(false);
item5.setEnabled(true);
item6.setEnabled(true);
item7.setEnabled(true);
}
else if(role.equals("operator")){
menu1.setEnabled(false);
menu2.setEnabled(true);
menu3.setEnabled(true);
item1.setEnabled(false);
item2.setEnabled(false);
item3.setEnabled(false);
item4.setEnabled(true);
item5.setEnabled(true);
item6.setEnabled(true);
item7.setEnabled(true);
}
else if(role.equals("browser")){
menu1.setEnabled(false);
menu2.setEnabled(true);
menu3.setEnabled(true);
item1.setEnabled(false);
item2.setEnabled(false);
item3.setEnabled(false);
item4.setEnabled(false);
item5.setEnabled(true);
item6.setEnabled(true);
item7.setEnabled(true);
}
menu1.add(item1);
menu1.add(item2);
menu1.add(item3);
menu2.add(item4);
menu2.add(item5);
menu3.add(item6);
menu3.add(item7);
menu.add(menu1);
menu.add(menu2);
menu.add(menu3);
menu.setSize(195,30);
menu.setBounds(0,0,195,30);
menu.setVisible(true);
frame.add(menu);
item1.addActionListener(new MenuAction(name,role));
item2.addActionListener(new MenuAction(name,role));
item3.addActionListener(new MenuAction(name,role));
item4.addActionListener(new MenuAction(name,role));
item5.addActionListener(new MenuAction(name,role));
item6.addActionListener(new MenuAction(name,role));
item7.addActionListener(new MenuAction(frame));
/*frame.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
try {
Client.closeConnection();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});*/
frame.setVisible(true);
}
class MenuAction implements ActionListener{
String name;
String role;
JFrame frame;
MenuAction(JFrame frame){
this.frame=frame;
}
MenuAction(String name,String role){
this.name=name;
this.role=role;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="添加用户") {
UserWindow userWindow=new UserWindow();
userWindow.showMenu(0);
}
else if(e.getActionCommand()=="修改用户") {
UserWindow userWindow=new UserWindow();
userWindow.showMenu(1);
}
else if(e.getActionCommand()=="删除用户") {
UserWindow userWindow=new UserWindow();
userWindow.showMenu(2);
}
else if(e.getActionCommand()=="上传文件") {
UpDownloadWindow updownloadWindow=new UpDownloadWindow();
updownloadWindow.showMenu(1);
}
else if(e.getActionCommand()=="下载文件") {
UpDownloadWindow updownloadWindow=new UpDownloadWindow();
updownloadWindow.showMenu(0);
}
else if(e.getActionCommand()=="修改密码") {
PasswordWindow passwordWindow=new PasswordWindow();
passwordWindow.showMenu();
}
else if(e.getActionCommand()=="退出") {
frame.dispose();
try {
Client.closeConnection();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
public class UserWindow{
String admin_role;
public void showMenu(int index){
String role=Client.get_Role();
admin_role=role;
JFrame frame=new JFrame();
frame.setTitle("用户管理界面");
frame.setSize(380,300);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel panel_add=new JPanel();
panel_add.setLayout(null);
tabbedPane.addTab("添加用户", panel_add);
JLabel L1=new JLabel("账号:");
JTextField te1=new JTextField(25);
JLabel L2=new JLabel("密码:");
JPasswordField te2=new JPasswordField(25);
JLabel L3=new JLabel("角色:");
String Role[]= {"administrator","operator","browser"};
JComboBox box=new JComboBox(Role);
te2.setEchoChar('*');
L1.setFont(new Font("黑体",Font.PLAIN,18));
L1.setSize(50,30);
L1.setBounds(5+30,30,50,30);
te1.setFont(new Font("黑体",Font.PLAIN,18));
te1.setSize(250,30);
te1.setBounds(5+80,30,250,30);
L2.setFont(new Font("黑体",Font.PLAIN,18));
L2.setSize(50,30);
L2.setBounds(5+30,80,50,30);
te2.setFont(new Font("黑体",Font.PLAIN,18));
te2.setSize(250,30);
te2.setBounds(5+80,80,250,30);
L3.setFont(new Font("黑体",Font.PLAIN,18));
L3.setSize(50,30);
L3.setBounds(5+30,130,50,30);
box.setFont(new Font("黑体",Font.PLAIN,18));
box.setSize(200,30);
box.setBounds(5+80,130,200,30);
panel_add.add(L1);
panel_add.add(te1);
panel_add.add(L2);
panel_add.add(te2);
panel_add.add(L3);
panel_add.add(box);
JButton B1=new JButton("添加");
JButton B2=new JButton("退出");
B1.setSize(80,40);
B1.setBounds((380-80-5)/2-60,(300-40-30)/2+50,80,40);
B2.setSize(80,40);
B2.setBounds((380-80-5)/2+60,(300-40-30)/2+50,80,40);
panel_add.add(B1);
panel_add.add(B2);
B1.addActionListener(new ButtonHandler(frame,te1,te2,box));
B2.addActionListener(new ButtonHandler(frame));
String[][] rowData=new String[50][3];
String[] columnName={"用户名","密码","角色"};
int i=0;
i=Client.get_Rows();
rowData=Client.get_Users();
String[] nameData=new String[50];
int j=0;
for(;j box_name=new JComboBox(nameData);
JLabel L5=new JLabel("密码:");
JPasswordField te4=new JPasswordField(25);
JLabel L6=new JLabel("角色:");
JComboBox box2=new JComboBox(Role);
te4.setEchoChar('*');
L4.setFont(new Font("黑体",Font.PLAIN,18));
L4.setSize(50,30);
L4.setBounds(5+30,30,50,30);
box_name.setFont(new Font("黑体",Font.PLAIN,18));
box_name.setSize(250,30);
box_name.setBounds(5+80,30,250,30);
L5.setFont(new Font("黑体",Font.PLAIN,18));
L5.setSize(50,30);
L5.setBounds(5+30,80,50,30);
te4.setFont(new Font("黑体",Font.PLAIN,18));
te4.setSize(250,30);
te4.setBounds(5+80,80,250,30);
L6.setFont(new Font("黑体",Font.PLAIN,18));
L6.setSize(50,30);
L6.setBounds(5+30,130,50,30);
box2.setFont(new Font("黑体",Font.PLAIN,18));
box2.setSize(200,30);
box2.setBounds(5+80,130,200,30);
panel_mod.add(L4);
panel_mod.add(box_name);
panel_mod.add(L5);
panel_mod.add(te4);
panel_mod.add(L6);
panel_mod.add(box2);
JButton B3=new JButton("修改");
JButton B4=new JButton("退出");
B3.setSize(80,40);
B3.setBounds((380-80-5)/2-60,(300-40-30)/2+50,80,40);
B4.setSize(80,40);
B4.setBounds((380-80-5)/2+60,(300-40-30)/2+50,80,40);
panel_mod.add(B3);
panel_mod.add(B4);
B3.addActionListener(new ButtonHandler(frame,box_name,te4,box2));
B4.addActionListener(new ButtonHandler(frame));
JPanel panel_del=new JPanel();
panel_del.setLayout(null);
tabbedPane.addTab("删除用户", panel_del);
@SuppressWarnings("serial")
JTable table=new JTable(rowData,columnName) {
public boolean isCellEditable(int rowIndex,int ColIndex) {
return false;
}
};
table.setFont(new Font("黑体",Font.PLAIN,18));
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroll=new JScrollPane(table);
scroll.setVisible(true);
scroll.setSize(380,160);
scroll.setBounds(0,0,380,160);
panel_del.add(scroll);
JButton B5=new JButton("删除");
JButton B6=new JButton("退出");
B5.setSize(80,40);
B5.setBounds((380-80-5)/2-60,(300-40-30)/2+50,80,40);
B6.setSize(80,40);
B6.setBounds((380-80-5)/2+60,(300-40-30)/2+50,80,40);
panel_del.add(B5);
panel_del.add(B6);
B5.addActionListener(new ButtonHandler(frame,table));
B6.addActionListener(new ButtonHandler(frame));
tabbedPane.setVisible(true);
tabbedPane.setSelectedIndex(index);
frame.add(tabbedPane);
frame.setVisible(true);
}
public class ButtonHandler implements ActionListener{
public JTextField te1=new JTextField();
public JPasswordField te2=new JPasswordField();
public JFrame frame=new JFrame();
public JComboBox box=new JComboBox();
public JComboBox box_name=new JComboBox();
public JTable table;
public String role;
ButtonHandler(JFrame frame){
this.frame=frame;
}
ButtonHandler(JFrame frame,JTextField te1,JPasswordField te2,JComboBox box){
this.frame=frame;
this.te1=te1;
this.te2=te2;
this.box=box;
}
ButtonHandler(JFrame frame,JComboBox box_name,JPasswordField te2,JComboBox box) {
this.frame=frame;
this.box_name=box_name;
this.te2=te2;
this.box=box;
}
ButtonHandler(JFrame frame,JTable table) {
this.frame=frame;
this.table=table;
}
public void actionPerformed(ActionEvent e) {
String name=te1.getText();
String password=String.valueOf(te2.getPassword());
if(e.getActionCommand().equals("修改")) {
name=(String)box_name.getSelectedItem();
role=(String)box.getSelectedItem();
try {
Client.UpdateUser(name,password,role,frame);
} catch (IOException e1) {
e1.printStackTrace();
}
}
else if(e.getActionCommand().equals("添加")){
role=(String)box.getSelectedItem();
try {
Client.AddUser(name,password,role,frame);
} catch (IOException e1) {
e1.printStackTrace();
}
}
else if(e.getActionCommand().equals("删除")){
if(table.getSelectedRow()<0) ;
else {
String del_name=(String) table.getValueAt(table.getSelectedRow(), 0);
try {
try {
Client.DelUser(del_name,frame);
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
else if(e.getActionCommand().equals("退出")) {
frame.dispose();
}
}
}
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
public class UpDownloadWindow {
String user_role;
String uploadpath="C:\\uploadfile\\";
String downloadpath="C:\\downloadfile\\";
public void showMenu(int index) {
String role=Client.get_Role();
user_role=role;
JFrame frame=new JFrame();
frame.setTitle("文件管理界面");
frame.setSize(460,400);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel panel_download=new JPanel();
panel_download.setLayout(null);
tabbedPane.addTab("文件下载", panel_download);
String[] columnName={"ID","Creator","Time","FileName","Description"};
String[][] rowData=new String[50][5];
rowData=Client.get_Docs();
int j=Client.get_Rows2();
String[] nameData=new String[50];
int i=0;
for(;j
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class PasswordWindow {
String user_name;
String user_role;
public void showMenu() {
String name=Client.get_Name();
String role=Client.get_Role();
user_name=name;
user_role=role;
JFrame frame=new JFrame();
frame.setTitle("密码修改界面");
frame.setSize(380,400);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setLayout(null);
JLabel L1=new JLabel("账号:");
JTextField te1=new JTextField(25);
JLabel L2=new JLabel("旧密码:");
JPasswordField te2=new JPasswordField(25);
te2.setEchoChar('*');
JLabel L3=new JLabel("新密码:");
JPasswordField te3=new JPasswordField(25);
te3.setEchoChar('*');
JLabel L4=new JLabel("重复密码:");
JPasswordField te4=new JPasswordField(25);
te4.setEchoChar('*');
JLabel L5=new JLabel("角色:");
JTextField te5=new JTextField(25);
L1.setFont(new Font("黑体",Font.PLAIN,18));
L1.setSize(100,30);
L1.setBounds(5,30,100,30);
te1.setFont(new Font("黑体",Font.PLAIN,18));
te1.setSize(250,30);
te1.setBounds(5+100,30,250,30);
L2.setFont(new Font("黑体",Font.PLAIN,18));
L2.setSize(100,30);
L2.setBounds(5,80,100,30);
te2.setFont(new Font("黑体",Font.PLAIN,18));
te2.setSize(250,30);
te2.setBounds(5+100,80,250,30);
L3.setFont(new Font("黑体",Font.PLAIN,18));
L3.setSize(100,30);
L3.setBounds(5,130,100,30);
te3.setFont(new Font("黑体",Font.PLAIN,18));
te3.setSize(250,30);
te3.setBounds(5+100,130,250,30);
L4.setFont(new Font("黑体",Font.PLAIN,18));
L4.setSize(100,30);
L4.setBounds(5,180,100,30);
te4.setFont(new Font("黑体",Font.PLAIN,18));
te4.setSize(250,30);
te4.setBounds(5+100,180,250,30);
L5.setFont(new Font("黑体",Font.PLAIN,18));
L5.setSize(100,30);
L5.setBounds(5,230,100,30);
te5.setFont(new Font("黑体",Font.PLAIN,18));
te5.setSize(250,30);
te5.setBounds(5+100,230,250,30);
frame.add(L1);
frame.add(L2);
frame.add(L3);
frame.add(L4);
frame.add(L5);
frame.add(te1);
frame.add(te2);
frame.add(te3);
frame.add(te4);
frame.add(te5);
te1.setText(user_name);
te1.setEnabled(false);
te5.setText(user_role);
te5.setEnabled(false);
JButton B1=new JButton("确认");
JButton B2=new JButton("退出");
B1.setSize(80,40);
B1.setBounds((380-80-5)/2-60,300,80,40);
B2.setSize(80,40);
B2.setBounds((380-80-5)/2+60,300,80,40);
B1.addActionListener(new ButtonHandler(frame,te2,te3,te4));
B2.addActionListener(new ButtonHandler(frame));
frame.add(B1);
frame.add(B2);
frame.setVisible(true);
}
public class ButtonHandler implements ActionListener{
JFrame frame;
JTextField te2;
JTextField te3;
JTextField te4;
ButtonHandler(JFrame frame){
this.frame=frame;
}
ButtonHandler(JFrame frame,JTextField te2,JTextField te3,JTextField te4){
this.frame=frame;
this.te2=te2;
this.te3=te3;
this.te4=te4;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="确认") {
String old_password=te2.getText();
String new_password=te3.getText();
String new_password2=te4.getText();
try {
Client.ChangeSelfInfo(old_password, new_password, new_password2);
} catch (IOException e1) {
e1.printStackTrace();
}
}
else if(e.getActionCommand()=="退出"){
frame.dispose();
}
}
}
}
以下是需要完成的结果展示Java面向对象与多线程综合实验(六)之网络编程
PS:视频中貌似只要求实现上传文件,本例实现了添加、修改、删除账户,上传和下载文件,修改个人密码和退出。