package com.sinojava.haiyun;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.*;
import java.net.*;
//客戶端類
public class ExporterBL {
?? ?//定義相應的私有變量
?? ?private Frame frame;
?? ?private TextArea display;
?? ?private Panel panel;
?? ?private ScrollPane sp;
?? ?private Button button,buttonsave;
?? ?private FileDialog dialog,dialogsave;
?? ?private String str1,str2;
?? ?private String strContext;
?? ?private String line;
?? ?private String result="";
?? ?private int count = 0;
?? ?ArrayList list = new ArrayList();
?? ?private ObjectOutputStream output;
?? ?private ObjectInputStream input;
?? ?private Socket soc;
?? ?private String chatServer;
?? ?//輸出信息
?? ?private String message = "";
?? ?private String a[];
?? ?
?? ?public ExporterBL(String host) {
?? ??? ?//創建客戶端GUI界面
?? ??? ?chatServer = host;
?? ??? ?frame = new Frame("出口預配集裝箱貨物信息導入");
?? ??? ?frame.setLayout(new BorderLayout());
?? ??? ?panel = new Panel();
?? ??? ?panel.setLayout(new FlowLayout());
?? ??? ?button = new Button("打開");
?? ??? ?buttonsave = new Button("保存");
?? ??? ?panel.add(button);
?? ??? ?panel.add(buttonsave);
?? ??? ?sp = new ScrollPane();
?? ??? ?display = new TextArea();
?? ??? ?sp.add(display);
?? ??? ?dialog = new FileDialog(frame,"打開",FileDialog.LOAD);
?? ??? ?dialogsave = new FileDialog(frame,"保存",FileDialog.SAVE);
?? ??? ?frame.add(sp,BorderLayout.CENTER);
?? ??? ?frame.add(panel,BorderLayout.SOUTH);
?? ??? ?frame.setSize(500,500);
?? ??? ?frame.setLocation(200, 200);
?? ??? ?frame.setVisible(true);
?? ??? ?frame.pack();
?? ??? ?//退出程序
?? ??? ?frame.addWindowListener(new WindowAdapter() {
?? ??? ??? ?public void windowClosing(WindowEvent e) {
?? ??? ??? ??? ?System.exit(0);
?? ??? ??? ?}
?? ??? ?});
?? ??? ?button.addActionListener(new ActionListener() {
?? ??? ??? ?public void actionPerformed(ActionEvent e) {
?? ??? ??? ??? ?dialog.show();
?? ??? ??? ??? ?//獲取文件的路徑和文件名
?? ??? ??? ??? ?str1 = dialog.getDirectory()+dialog.getFile();
?? ??? ??? ??? ?//發送字符數據給服務器端
?? ??? ??? ??? ?sendData(readFile(str1));
?? ??? ??? ?}
?? ??? ?});
?? ??? ?//對文本進行保存
?? ??? ?buttonsave.addActionListener(new ActionListener() {
?? ??? ??? ?public void actionPerformed(ActionEvent e) {
?? ??? ??? ??? ?dialogsave.show();
?? ??? ??? ??? ?str2 = dialogsave.getDirectory()+dialogsave.getFile();
?? ??? ??? ??? ?String strContext = display.getText();
?? ??? ??? ??? ?writeFile(str2,strContext);
?? ??? ??? ?}
?? ??? ?});?? ?
?? ?}
?? ?
?? ?//連接服務器處理信息
?? ??? public void runClient() {
?? ?????? try {
?? ??? ??? ?? // 設置JTextArea不能被更改
?? ??? ??? ?? display.setEditable(false);
?? ????????? // 第一步:創建一個Socket,連接服務器
?? ????????? connectToServer();
?? ????????? // 第二步:獲取接受數據
?? ????????? getStreams();
?? ????????? // 第三步:連接處理
?? ????????? processConnection();
?? ????????? // 第四步:關閉連接
?? ????????? closeConnection();
?? ?????? }
?? ?????? catch (IOException e) {
?? ????????? e.printStackTrace();
?? ?????? }
?? ??? }
?? ?? ?
?? ??? //獲取接受數據
?? ??? private void getStreams() throws IOException {
?? ?????? output = new ObjectOutputStream(soc.getOutputStream());
?? ?????? output.flush();???? ?
?? ?????? input = new ObjectInputStream(soc.getInputStream());
?? ?????? display.append("\n獲得I/O流\n");
?? ??? }
?? ??? // 創建一個Socket,連接服務器
?? ??? private void connectToServer() throws IOException {???? ?
?? ?????? display.setText("連接中\n");
?? ?????? //InetAddress類采用工廠設計模式,有三個靜態工廠方法,如getByName。
?? ?????? soc = new Socket(InetAddress.getByName(chatServer),5000);
?? ?????? display.append("連接到:"+soc.getInetAddress().getHostName());
?? ??? }
?? ??? // 連接處理
?? ??? private void processConnection() throws IOException {
?? ?????? do {
?? ????????? try {
?? ??????? ??? ?//接收服務器端傳回來的字符數據
?? ??????? ??? ?message = (String)input.readObject();
?? ??????? ??? ?//顯示到TextArea上
?? ???????????? display.append("\n"+message);
?? ???????????? display.setCaretPosition(display.getText().length());
?? ????????? }
?? ????????? catch(ClassNotFoundException e) {
?? ???????????? display.append("\n沒有對象接受");
?? ????????? }
?? ?????? } while(true);
?? ??? } ?
?? ??? // 關閉連接
?? ??? private void closeConnection() throws IOException {
?? ?????? display.append("\n關閉連接");
?? ?????? output.close();
?? ?????? input.close();
?? ?????? soc.close();
?? ??? }
?? ??? // 發送信息到服務器
?? ??? private void sendData(String message) {
?? ?????? try {
?? ????????? output.writeObject(message);
?? ????????? output.flush();
?? ?????? }
?? ?????? catch (IOException e) {
?? ????????? display.append("\n錯誤的對象");
?? ?????? }
?? ??? }? ?
?? ?
?? ?//讀取文件內容
?? ?public String readFile(String s) {
?? ??? ?try {
?? ??? ??? ?FileReader fr = new FileReader(s);
?? ??? ??? ?BufferedReader br = new BufferedReader(fr);
?? ??? ??? ?while((line=br.readLine())!=null) {
?? ??? ??? ?? ??? ? result+=line+"\n";
?? ??? ??? ?}? ??? ??? ??? ?? ?
?? ??? ??? ?fr.close();
?? ??? ??? ?br.close();
?? ??? ?}catch(IOException e) {
?? ??? ??? ?System.out.println("Error:"+e.getMessage());
?? ??? ?}
?? ??? ?return result;
?? ?}
?? ?//寫入文件內容
?? ?public void writeFile(String s,String ss) {
?? ??? ?try {
?? ??? ??? ?PrintWriter out = new PrintWriter(new FileWriter(s),true);?? ??? ??? ?
?? ??? ??? ?out.println(ss+"\n");
?? ??? ??? ?out.flush();
?? ??? ??? ?out.close();?? ?
?? ??? ?}catch(IOException e) {
?? ??? ??? ?System.out.println("Error:"+e.getMessage());
?? ??? ?}
?? ?}
?? ?//main方法
?? ?public static void main(String[] args) {
?? ??? ?ExporterBL ebl = new ExporterBL("127.0.0.1");
?? ??? ?//運行客戶端
?? ??? ?ebl.runClient();
?? ?}
}