海运管理系统

更新时间:2023-09-11 03:41:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

目录

海运集装箱管理需求说明书 -------------------------------------------------------------------------------------------------------------- 1 -

项目开发内容: ----------------------------------------------------------------------------------------------------------------------- 1 - 要求: ---------------------------------------------------------------------------------------------------------------------------------- 2 - 总体设计 ------------------------------------------------------------------------------------------------------------------------------- 4 - 运行设计 ------------------------------------------------------------------------------------------------------------------------------- 4 -

运行控制-------------------------------------------------------------------------------------------------------------------------- 4 -

源代码---------------------------------------------------------------------------------------------------------------------------------------- 5 -

main包 --------------------------------------------------------------------------------------------------------------------------------- 5 -

Start程序运行入口 -------------------------------------------------------------------------------------------------------------- 5 - Gui程序主界面 ------------------------------------------------------------------------------------------------------------------ 5 - file包 ----------------------------------------------------------------------------------------------------------------------------------- 9 -

Data提单数据-------------------------------------------------------------------------------------------------------------------- 9 - Parse解析提单 ---------------------------------------------------------------------------------------------------------------- - 11 - Count统计--------------------------------------------------------------------------------------------------------------------- - 15 - NewFile新建提单 ------------------------------------------------------------------------------------------------------------ - 16 - net包 -------------------------------------------------------------------------------------------------------------------------------- - 21 -

Server服务器 ----------------------------------------------------------------------------------------------------------------- - 21 - Client客户端 ------------------------------------------------------------------------------------------------------------------ - 23 -

海运管理系统--------@赵腾园

海运集装箱管理需求说明书

项目开发内容:

======================================

读取预配报文文件(参见06062313、06062314),将报文重新格式化,并进行统计分析,然后将结果写入文件。 文件体格式如下:

船名:航次:提单号:目的港# 尺寸:箱型:数量:经营人# 尺寸:箱型:数量:经营人#

TITANIC:603E:SNL 018576:SIGAPORE# 20:GP:4:HUB# 40:RF:5:SNL#

单船多票文件结构: YUNFENG:603E:SNL 0185762:SIGAPORE# 20:GP:4:HUB# 40:RF:5:SNL# YUNFENG:603E:JL 4547357:PUSAN# 20:GP:4:HUB# 40:RT:5:JL# HANYANG:603E:HUB 8576276:SIGAPORE# 20:RH:4:HUB# 40:RF:5:SNL# YUNFENG:603E:SNL 4195325:CNSHA# 20:RF:4:HUB# 40:GP:5:JL#

提单类型的数据结构: 英文名称 shipName voyage blno

destination cnttype

中文名称 船名 航次 提单号 目的港 箱型

- 1 -

海运管理系统--------@赵腾园

cntsize cntqnt

cntoperator remark 集装箱尺寸 箱量

箱经营人 备注

要求:

1、从文件中读取数据,进行解析,并展示。形式如下:

显示在JTable中 。每一条数据,要显示箱子的信息和箱子所在船的信息。

2、对解析后数据要提供保存功能,保存到数据库中(现阶段利用文件替代数据,将数据保存在文件中)。

3、完成新建提单的功能。

4、完成统计功能。对解析后的内容进行统计。 统计如下功能: 1). 总共有多少个航次

3). 总共有多少票业务(按提单号统计)

6). 所有业务的箱型尺寸汇总(每个尺寸的箱子的个数) 7). 所有业务中有多少个箱子

8). 计算总共有多少个TEU(twentyfoot equivalent unit) 统计出尺寸是20的箱子的个数。

9)统计每个经营人的数量。

5、将统计结果存入文件,同时显示在界面上。

- 2 -

海运管理系统--------@赵腾园

6、 Socket 数据传递,要在局域网中的不同计算机之间测试通过。(要求完成聊天和传送文件) 7、如果提单头 是SNL,HUB 要在记录的最后(备注)加上SOC

8、实现翻译,根据下表,翻译对应的箱型,和尺寸。

代码/原代号 箱 型 箱型群组代码 主 要 特 征 箱型代码 原代号 G/0 通用集装箱 GP G0-G9 00-11 (无通风装置) V/1 通风式通用集VH V1-V9 13-19 装箱 B/2 干散货集装箱 BU, BK B0-B9 20-24 S/2 以货物种类命SN 汽车集装箱,活S0-S8 25-29 名的集装箱 鱼集装箱 R/3 保温集装箱 RE,RF,RT,RS,RH 制冷/加热 R0-R9 31-39 H/4 保温集装箱 HR,HI 外置式挂装制H0-H9 40-49 冷/加热装 U/5 P/6 敞顶式集装箱 平台(和台架式)集装箱 罐式集装箱 空/陆/水联运集装箱 UT PL T/7 A TN AS 最低试验压力 -端或两端开口 U0-U6 平台集装箱 P0-P9 50-56 60-69 T0-T9 A0 70-79 90 尺寸 12 英尺 箱型 干货箱 干货高箱 干货箱 干货高箱 挂衣 开顶箱 冷冻箱 冷高箱 油罐箱 对应类型 GP GH(HC,HQ) GP GH(HC,HQ) HT OT RF RH TK - 3 -

12G1 15G1 22G1 25G1 22V1 22U1 22R1 25R1 22T1 95码 20英尺

海运管理系统--------@赵腾园

框架箱 干货箱 干货高箱 挂衣箱 开顶箱 冷冻箱 冷高箱 油罐箱 框架箱 干货箱 干货高箱 挂衣箱 开顶箱 冷冻箱 冷高箱 油罐箱 框架箱 FR GP GH(,HQ) HT OT RF RH TK FR GP GH(HC,HQ) HT OT RF RH TK FR 22P1 42G1 45G1 42V1 42U1 42R1 45R1 42T1 42P1 L2G` L5G1 L2V1 L2U1 L2R1 L5R1 L2T1 L2P1 40英尺 45英尺

9、不得在程序中出现业务数据或者业务文件名称等硬编码(配置文件可以),把所有需要硬编码的内容写入文件

总体设计

对文件解析并显示结果 对解析结果进行统计 如果文件格式有误应提示:文件有误 并从新进行选择文件的操作 统计后的结果可以保存到文件中 将解析结果存入文件中 保存文件类型: .TXT .SLX .DAT 运行设计 运行控制

双击该软件即可直接运行,点击界面当中的菜单即可进行相应的操作。

菜单选项共分为:文件,统计,网络,工具,帮助(下图是表示菜单中的子菜单结构)

- 4 -

海运管理系统--------@赵腾园

新建提单

文件 打开提单 保存文件 统计 统计提单信息

启动服务器 网络 关闭服务器

计算机 工具 画图程序

帮助 帮助文档

源代码

main包

Start程序运行入口

package main;

public class Start { public static void main(String[] args) { Gui g = new Gui(); g.setVisible(true); } }

Gui程序主界面

package main;

import file.*;

import java.awt.Container;

import java.awt.event.ActionEvent;

- 5 -

海运管理系统--------@赵腾园

import java.awt.event.ActionListener;

import java.io.*;

import javax.swing.*;

import net.*;

@SuppressWarnings(\

public class Gui extends JFrame implements ActionListener{ JMenu file; JMenuItem newfile; JMenuItem openfile; JMenuItem savefile; JMenu count; JMenuItem countinfo; JMenu net; JMenuItem chat; JMenuItem server; JMenuItem client; JMenu tools; JMenuItem computer; JMenuItem draw; JMenu game; JMenuItem saolei; JMenu help; JMenuItem version; public Gui(){ @SuppressWarnings(\ Container con=this.getContentPane(); this.setTitle(\海运管理系统 @Zhao Tengyuan\ this.setBounds(200, 200, 500, 500); JMenuBar bar = new JMenuBar(); this.setJMenuBar(bar); file =new JMenu(\文件\ bar.add(file); newfile =new JMenuItem(\新建\ openfile = new JMenuItem(\打开\ savefile = new JMenuItem(\保存\ file.add(newfile); file.add(openfile); file.add(savefile); newfile.addActionListener(this); openfile.addActionListener(this); savefile.addActionListener(this); count = new JMenu(\统计\ bar.add(count); countinfo = new JMenuItem(\统计信息\ count.add(countinfo); countinfo.addActionListener(this); net = new JMenu(\网络\ bar.add(net); chat = new JMenuItem(\聊天\ server = new JMenuItem(\服务器\ client = new JMenuItem(\客户端\

- 6 -

海运管理系统--------@赵腾园

net.add(chat); net.add(server); net.add(client); chat.addActionListener(this); server.addActionListener(this); client.addActionListener(this); tools = new JMenu(\工具\ bar.add(tools); computer = new JMenuItem(\计算器\ draw = new JMenuItem(\画图\ tools.add(computer); tools.add(draw); computer.addActionListener(this); draw.addActionListener(this); game = new JMenu(\游戏\ bar.add(game); saolei = new JMenuItem(\扫雷\ game.add(saolei); saolei.addActionListener(this); help = new JMenu(\帮助\ bar.add(help); version = new JMenuItem(\帮助信息\ help.add(version); version.addActionListener(this); }

JFrame jframe;// 用于主窗体

JTable jtable; // 用于显示打开内容 JScrollPane jsp;// 用于添中JTable String[] headerStr;// 用于存储表头 Object[][] objStr;// 用于存储表单

Parse ps = new Parse();

public void actionPerformed(ActionEvent e) { if(e.getSource().equals(newfile)){ new NewFile(); } if(e.getSource().equals(openfile)){ JFileChooser jfc = new JFileChooser(\ int i = jfc.showOpenDialog(null); if (i == JFileChooser.APPROVE_OPTION) { File file = jfc.getSelectedFile(); boolean isnot = true; String regex1 = \ String regex2 = \ try { BufferedReader buf = new BufferedReader(new FileReader(file)); // 判断这个文件是否符合要求 if (!buf.ready()) {// 判断是否为空 JOptionPane.showMessageDialog(null, \打开的文件格式错误!\ isnot = false; return; } else { do { String str = buf.readLine(); // 判断读入的每一句是否为指定格式

- 7 -

海运管理系统--------@赵腾园

if ((!str.matches(regex1))&& (!str.matches(regex2))) { JOptionPane.showMessageDialog(null, \打开的文件格式错误!\ isnot = false; return; } } while (buf.ready()); } buf.close(); // 如果文件符合要求,则调用解析程序分解文件内容 if (isnot) { BufferedReader bufReader = new BufferedReader(new FileReader(file)); while (bufReader.ready()) { String str = bufReader.readLine(); // 每读一句,调用一次解析方法 ps.reader_Parse(str); } bufReader.close(); } } catch (Exception e1) { e1.printStackTrace(); } } objStr = ps.getObjStr();// 取得显示内容 headerStr = ps.getHeader();// 取得表头 jtable = new JTable(objStr,headerStr); jtable.updateUI();//当第二次打开文件时实现文件的更新 jtable.clearSelection(); jtable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS ); jtable.setRowHeight(30);//设置表格的高度 jtable.getColumnModel().getColumn(0).setPreferredWidth(150);//设置表格宽度 JScrollPane scrollpane = new JScrollPane(jtable); this.add(scrollpane); this.setVisible(true); }

if(e.getSource().equals(savefile)){ JFileChooser jfc = new JFileChooser(\ int jin = jfc.showSaveDialog(null); File file = jfc.getSelectedFile(); if (jin == JFileChooser.APPROVE_OPTION) { try { // 保存jtable中的内容 PrintStream p = new PrintStream(file); for (int i = 0; i < ps.getList().size(); i++) { p.println(ps.getList().get(i)); } } catch (Exception e1) { JOptionPane.showMessageDialog(null, \无法保存!\ } } }

if(e.getSource().equals(countinfo)){ try{ Count count = new Count(); count.cFrame();

- 8 -

海运管理系统--------@赵腾园

}catch(Exception e1){ JOptionPane.showMessageDialog(null, \无法进行统计\ } } if(e.getSource().equals(chat)){ MyTCP mtcp = new MyTCP(); mtcp.setVisible(true); } if(e.getSource().equals(server)){ Server s = new Server(); s.serv(); } if(e.getSource().equals(client)){ Client c = new Client(); c.clientFrame(); } if(e.getSource().equals(computer)){ try { Runtime.getRuntime().exec(\ } catch (IOException e1) { JOptionPane.showMessageDialog(null, \ } } if(e.getSource().equals(draw)){ try { Runtime.getRuntime().exec(\ } catch (IOException e1) { JOptionPane.showMessageDialog(null, \ } } if(e.getSource().equals(saolei)){ try { Runtime.getRuntime().exec(\ } catch (IOException e1) { JOptionPane.showMessageDialog(null, \ } } if(e.getSource().equals(version)){ JOptionPane.showMessageDialog(null, \ersion: V Tengyuan\\nEmail:zhty2007@126.com\ } } }

file包 Data提单数据

package file;

//海运提单数据项目

public class Data {

- 9 -

1.1\\nAuthor: Zhao

海运管理系统--------@赵腾园

}

}

pos++; } } strlist += \经营人:\ + str[i] + \数量:\ + pos + \; break; } } }

return strlist;

Count统计

package file;

import java.awt.*;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File;

import java.io.FileNotFoundException; import java.io.PrintStream;

import javax.swing.*;

public class Count { Parse ps = new Parse(); Object[][] countObj = ps.get_Count(); JFrame countFrame; public void cFrame() { String[] countHeader = ps.getCount_Header(); countFrame = new JFrame(); countFrame.setLocation(200, 200); countFrame.setSize(600, 150); countFrame.setTitle(\统计信息 @Zhao Tengyuan\ JButton b1 = new JButton(\导出\ JButton b2 = new JButton(\关闭\ JPanel jtab=new JPanel(); jtab.add(b1); jtab.add(b2); b1.addActionListener(new Export()); b2.addActionListener(new Close()); countFrame.add(jtab,BorderLayout.SOUTH); JTable countJTable = new JTable(countObj, countHeader); countJTable.setEnabled(false); JScrollPane countJsp = new JScrollPane(countJTable); countFrame.add(countJsp);

- 15 -

海运管理系统--------@赵腾园

countFrame.setVisible(true); }

class Export implements ActionListener{ public void actionPerformed(ActionEvent e) { // 将统计结果输出存到文件 PrintStream p; try { JFileChooser j=new JFileChooser(\ int value = j.showSaveDialog(null); if(value==0){ File file=j.getSelectedFile(); p = new PrintStream(file); p.println(file+\统计信息\ for (int i = 0; i < 6; i++) { p.print(countObj[0][i] + \ } } } catch (FileNotFoundException e1) { e1.printStackTrace(); } } }

class Close implements ActionListener{ public void actionPerformed(ActionEvent e){ countFrame.dispose(); } }

}

NewFile新建提单

package file;

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File;

import java.io.FileWriter; import java.util.ArrayList;

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.JTextField;

@SuppressWarnings(\

public class NewFile extends JFrame{

\- 16 -

海运管理系统--------@赵腾园

JFrame f = new JFrame(\新建提单 @Zhao Tengyuan\ //JPanel 是一般轻量级容器。 JPanel p = new JPanel(); JPanel p1 = new JPanel(); JLabel l = new JLabel(\船信息\ JLabel l1 = new JLabel(\船名:\ JLabel l2 = new JLabel(\航次:\ JLabel l3 = new JLabel(\提单号:\ JLabel l4 = new JLabel(\目的港:\ JTextField jtf1 = new JTextField(5); JTextField jtf2 = new JTextField(5); JTextField jtf3 = new JTextField(5); JTextField jtf4= new JTextField(5); JButton addShip = new JButton(\添加船信息\ JButton addBox = new JButton(\添加箱信息\ JButton add = new JButton(\添加\ JButton save = new JButton(\保存\ StringBuffer sb = new StringBuffer(); ArrayList list = new ArrayList(); public NewFile(){ /*setEnabled public void setEnabled(boolean b)启用(或禁用)按钮。 覆盖: 类 JComponent 中的 setEnabled 参数: b - true 表示启用按钮,否则为 false*/ //按钮的默认状态 addShip.setEnabled(false); addBox.setEnabled(false); save.setEnabled(false); /*addWindowListener public void addWindowListener(WindowListener l)添加指定的窗口侦听器,以从此窗口接收窗口事件。 如果 l 为 null,则不抛出任何异常,且不执行任何操作。*/ /*dispose public void dispose()释放由此 Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源。 即这些 Component 的资源将被破坏,它们使用的所有内存都将返回到操作系统,并将它们标记为不可显示。 通过随后调用 pack 或 show 重新构造本机资源,可以再次显示 Window 及其子组件。 重新创建的 Window 及其子组件的状态与释放 Window 时这些对象的状态一致(不考虑这些操作之间的其他更改)。*/

- 17 -

海运管理系统--------@赵腾园

/*public abstract class WindowAdapterextends Objectimplements WindowListener, WindowStateListener, WindowFocusListener接收窗口事件的抽象适配器类。此类中的方法为空。此类存在的目的是方便创建侦听器对象。 扩展此类可创建 WindowEvent 侦听器并为所需事件重写该方法。(如果要实现 WindowListener 接口, 则必须定义该接口内的所有方法。此抽象类将所有方法都定义为 null,所以只需针对关心的事件定义方法。) 使用扩展的类可以创建侦听器对象,然后使用窗口的 addWindowListener 方法向该窗口注册侦听器。

当通过打开、关闭、激活或停用、图标化或取消图标化而改变了窗口状态时,将调用该侦听器对象中的相关方法, 并将 WindowEvent 传递给该方法。

void windowClosing(WindowEvent e) 窗口正处在关闭过程中时调用。*/

f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ f.dispose(); } });

/*public interface ActionListener extends EventListener用于接收操作事件的侦听器接口。 对处理操作事件感兴趣的类可以实现此接口,而使用该类创建的对象可使用组件的

addActionListener 方法向该组件注册。在发生操作事件时,调用该对象的 actionPerformed 方法。*/

//添加箱信息按钮事件监听

addBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { /*void setText(String text) 将此标签的文本设置为指定的文本。 */ l.setText(\箱信息\ l1.setText(\箱型:\ l2.setText(\箱尺寸:\ l3.setText(\箱量:\ l4.setText(\箱经营人:\ /*setText public void setText(String t)将此 TextComponent 文本设置为指定文本。 如果该文本为 null 或空,则具有只删除旧文本的效果。 插入文本时,得到的插入符的位置由该插入符类的实现确定。*/ jtf1.setText(null); jtf2.setText(null); jtf3.setText(null); jtf4.setText(null); addShip.setEnabled(true); add.setEnabled(true); } });

//添加船信息按钮事件监听

addShip.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { l.setText(\船信息\ l1.setText(\船名:\

- 18 -

海运管理系统--------@赵腾园

l2.setText(\航次:\ l3.setText(\提单号:\ l4.setText(\目的港:\ jtf1.setText(null); jtf2.setText(null); jtf3.setText(null); jtf4.setText(null); addShip.setEnabled(false); add.setEnabled(true); } });

//添加按钮事件监听

add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //正则表达式 String regex1 = \航次 String regex2 = \提单号 String regex3 = \目的港 String regex4 = \箱型 String regex5 = \尺寸 String regex6 = \箱容量 String regex7 = \箱经营人 if(l.getText().equals(\船信息\ if(jtf1.getText().equals(\ JOptionPane.showMessageDialog(null, \船名不能为空\ else if(jtf2.getText().matches(regex1)==false) JOptionPane.showMessageDialog(null, \请输入正确的航次,如:\\n603E\ else if(jtf3.getText().matches(regex2)==false) JOptionPane.showMessageDialog(null, \请输入正确的提单号,如:\\nSNL0185762\ else if(jtf4.getText().matches(regex3)==false) JOptionPane.showMessageDialog(null, \请输入正确的目的港,如:\\nSIGAPORE\ else{ //将添加的船添加到集合中 list.add(jtf1.getText()+\ sb.append(jtf1.getText()+\ addBox.setEnabled(true);//添加箱子按钮为可用 save.setEnabled(true);//保存按钮为可用 add.setEnabled(false);//添加按钮不可用 } } else{ if(jtf1.getText().matches(regex4)==false) JOptionPane.showMessageDialog(null, \请输入正确的箱型,如:\\n12|20|40|45\ else if(jtf2.getText().matches(regex5)==false) JOptionPane.showMessageDialog(null, \请输入正确的尺寸,如:\\nGP|GH|HT|OT|RF|RH|TK|FR\ else if(jtf3.getText().matches(regex6)==false)

- 19 -

海运管理系统--------@赵腾园

}

}

}

BufferedInputStream bInput = null;

OutputStream clientOutput = null;// 输出流 BufferedOutputStream bOutput = null; ObjectOutputStream objOutput = null; byte[] buff = new byte[1024 * 8]; try { fileInput = new FileInputStream(file); clientOutput = client.getOutputStream(); objOutput = new ObjectOutputStream(clientOutput); objOutput.writeObject(file.getName());// 向服务器写入文件名 bInput = new BufferedInputStream(fileInput); bOutput = new BufferedOutputStream(clientOutput); JOptionPane.showMessageDialog(null, \正在发送...\ int pos = 0; while ((pos += bInput.read(buff)) != -1) {// 读出文件并进行发送 bOutput.write(buff, 0, pos); bOutput.flush(); pos = 0; } JOptionPane.showMessageDialog(null, \文件发送完成!\\n保存在E盘\ } catch (Exception e1) { JOptionPane.showMessageDialog(null, \发送失败,请检查!\ return; }finally { try { if (fileInput != null) { fileInput.close(); } if (bInput != null) { bInput.close(); } if (clientOutput != null) { clientOutput.close(); } if (bOutput != null) { bOutput.close(); } if (objOutput != null) { objOutput.close(); } client = new Socket(ipText, portText); } catch (IOException e1) { e1.printStackTrace(); } }

- 25 -

海运管理系统--------@赵腾园

JOptionPane.showMessageDialog(null, \请输入正确的箱量\ else if(jtf4.getText().matches(regex7)==false) JOptionPane.showMessageDialog(null, \请输入正确的箱经营人,如:\\nABC\ else{ list.add(jtf1.getText()+\ sb.append(jtf1.getText()+\ addShip.setEnabled(true); add.setEnabled(false); save.setEnabled(true); } } } });

//保存按钮事件监听

save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { /*JFileChooser(File currentDirectory) 使用给定的 File 作为路径来构造一个 JFileChooser。*/ /*int showSaveDialog(Component parent) 弹出一个 \文件选择器对话框。 */ /*File getSelectedFile() 返回选中的文件。 */ /*CANCEL_OPTION public static final int CANCEL_OPTION 选择 cancel 后的返回该值。*/ JFileChooser jfc = new JFileChooser(\ int n = jfc.showSaveDialog(null); if(n==JFileChooser.CANCEL_OPTION) return; File filename = jfc.getSelectedFile(); try{ FileWriter fw = new FileWriter(filename); fw.write(sb.toString()); fw.close(); }catch(Exception e0){ e0.printStackTrace(); } } });

//对顺序有要求 p.add(l); p.add(l1); p.add(jtf1); p.add(l2); p.add(jtf2); p.add(l3);

- 20 -

海运管理系统--------@赵腾园

}

}

p.add(jtf3); p.add(l4); p.add(jtf4);

p1.add(addShip); p1.add(addBox); p1.add(add); p1.add(save);

f.add(p, BorderLayout.CENTER); f.add(p1, BorderLayout.SOUTH); f.setSize(600, 100); f.setLocation(300,300); f.setVisible(true);

net包 Server服务器

package net;

import java.io.*; import java.net.*; //服务器

public class Server { ServerSocket server = null; public void serv(){//开启服务器 try{ server = new ServerSocket(8888);//定义服务器端口 new serverAccept(server);//新开一个监听线程 }catch(Exception e){ e.printStackTrace(); } } }

//监听客户端登录

class serverAccept extends Thread{ Socket socket; ServerSocket server; public serverAccept(){} public serverAccept(ServerSocket server){ this.server = server; this.start(); } public void run(){ while(true){ try{ socket = server.accept();//等待客户端登录 new serverClient(socket); }catch(Exception e){ e.printStackTrace(); }

- 21 -

海运管理系统--------@赵腾园

} } }

//监听客户端信息发送

class serverClient extends Thread{ Socket socket; public serverClient(){} public serverClient(Socket socket){ this.socket = socket; this.start(); } public void run(){ InputStream socketInput = null; BufferedInputStream bInput = null; FileOutputStream fileOutput = null; BufferedOutputStream bOutput = null; ObjectInputStream objInput = null; byte[] b = new byte[1024]; try{ String clientIp = socket.getInetAddress().getHostAddress(); socketInput = socket.getInputStream();//接收文件名 objInput = new ObjectInputStream(socketInput); String fileName = (String)objInput.readObject(); fileOutput = new FileOutputStream(\发送的\创建文件 socketInput = socket.getInputStream(); bInput = new BufferedInputStream(socketInput); bOutput = new BufferedOutputStream(fileOutput); int pos = 0; while((pos += bInput.read(b))!= -1){ bOutput.write(b,0,pos); bOutput.flush(); pos = 0; } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(socketInput != null){ socketInput.close(); } if(bInput != null){ bInput.close(); } if(fileOutput != null){ fileOutput.close(); } if(bOutput != null){ bOutput.close(); } if(bInput != null){ bInput.close(); } }catch(Exception e){ e.printStackTrace(); }

- 22 -

海运管理系统--------@赵腾园

}

}

}

Client客户端

package net;

import java.awt.*;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.*; import javax.swing.*;

public class Client extends JFrame implements ActionListener{ private JFrame cFrame; private Socket client; private String ipText; private int portText; private TextField textIp; private TextField textPort; private JTextArea textShow; private JButton link; private JButton bBreak; private JButton select; private JButton send; private File file; public void clientFrame(){ cFrame = new JFrame(\客户端\ cFrame.setLocation(200, 200); cFrame.setSize(500, 500); cFrame.setVisible(true); cFrame.setLayout(new BorderLayout()); //将窗扣分成上、中、下三部分 JPanel up = new JPanel(); JPanel center = new JPanel(); JPanel down = new JPanel(); cFrame.add(up, BorderLayout.NORTH); cFrame.add(center, BorderLayout.CENTER); cFrame.add(down, BorderLayout.SOUTH); //上 JLabel ip = new JLabel(\请输入服务器IP:\ textIp = new TextField(\ JLabel port = new JLabel(\端口号:\ textPort = new TextField(\ up.add(ip); up.add(textIp); up.add(port); up.add(textPort); //中 textShow = new JTextArea(30,40); JScrollPane jsp = new JScrollPane(textShow);

- 23 -

海运管理系统--------@赵腾园

center.add(jsp); //下 link = new JButton(\连接\ bBreak = new JButton(\断开\ select = new JButton(\选择\ send = new JButton(\发送\ down.add(link); down.add(bBreak); down.add(select); down.add(send); link.addActionListener(this); bBreak.addActionListener(this); select.addActionListener(this); send.addActionListener(this); } public void actionPerformed(ActionEvent e) { //启动连接服务器程序 if(e.getSource().equals(link)){ ipText = textIp.getText(); portText = Integer.parseInt(textPort.getText()); String regexIp = \][4-5][0-5])\\\\.([0-1]?[0-9]{1,2}||[2][0-4][0-9]||[2][4-5][0-5])\ if (!ipText.matches(regexIp)) { JOptionPane.showMessageDialog(null, \请您输入正确的IP!\ } try{ client = new Socket(ipText, portText); JOptionPane.showMessageDialog(null, \连接成功!\ }catch(Exception e1){ JOptionPane.showMessageDialog(null, \不能连接,请确认服务器已启动!\ } } //启动断开服务器程序 if(e.getSource().equals(bBreak)){ try { client.close(); JOptionPane.showMessageDialog(null, \与服务器已经断开连接\ } catch (IOException e1) { e1.printStackTrace(); } } //启动选择文件程序 if(e.getSource().equals(select)){ JFileChooser jfc = new JFileChooser(\ int i = jfc.showOpenDialog(null); if (i == JFileChooser.APPROVE_OPTION) { file = jfc.getSelectedFile(); JOptionPane.showMessageDialog(null, file + \等待发送...\ } } //启动发送文件程序 if(e.getSource().equals(send)){ FileInputStream fileInput = null;// 输出流

- 24 -

海运管理系统--------@赵腾园

}

}

}

BufferedInputStream bInput = null;

OutputStream clientOutput = null;// 输出流 BufferedOutputStream bOutput = null; ObjectOutputStream objOutput = null; byte[] buff = new byte[1024 * 8]; try { fileInput = new FileInputStream(file); clientOutput = client.getOutputStream(); objOutput = new ObjectOutputStream(clientOutput); objOutput.writeObject(file.getName());// 向服务器写入文件名 bInput = new BufferedInputStream(fileInput); bOutput = new BufferedOutputStream(clientOutput); JOptionPane.showMessageDialog(null, \正在发送...\ int pos = 0; while ((pos += bInput.read(buff)) != -1) {// 读出文件并进行发送 bOutput.write(buff, 0, pos); bOutput.flush(); pos = 0; } JOptionPane.showMessageDialog(null, \文件发送完成!\\n保存在E盘\ } catch (Exception e1) { JOptionPane.showMessageDialog(null, \发送失败,请检查!\ return; }finally { try { if (fileInput != null) { fileInput.close(); } if (bInput != null) { bInput.close(); } if (clientOutput != null) { clientOutput.close(); } if (bOutput != null) { bOutput.close(); } if (objOutput != null) { objOutput.close(); } client = new Socket(ipText, portText); } catch (IOException e1) { e1.printStackTrace(); } }

- 25 -

本文来源:https://www.bwwdw.com/article/sy4h.html

Top