注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

古城风~~~

竹密无妨溪水过,天高不碍白云飞。这天下总有一份是属于我古城的天地!

 
 
 

日志

 
 

用压缩流的方式实现文件读取  

2008-09-12 19:34:29|  分类: java/jsp |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

(1)服务器端

package ccp330.gucheng.zip.io;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ServerZipStream {
 //服务器端
 private ServerSocket server = null;
 private boolean flag = true;
 
 public ServerZipStream () {
  try {
   server = new ServerSocket(3300);
    while(flag){
     Socket clientSocket = server.accept();
     if(clientSocket != null){
      new ServerThread(clientSocket).start();
     }
    }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 
 
 
 class ServerThread extends Thread {

  private Socket clientSocket = null;
  
  public ServerThread(Socket clientSocket) {
   this.clientSocket=clientSocket;
   
  }

  @Override
  public void run() {
   //while(true) {
    try {
     ZipOutputStream out = new ZipOutputStream(clientSocket.getOutputStream());
     FileInputStream fileIn = new FileInputStream("src/ccp330/gucheng/zip/io/ServerZipStream.java");
     out.putNextEntry(new ZipEntry("src/ccp330/gucheng/zip/io/ServerZipStream.java"));
     byte[] array = new byte[1024];
     int len=-1;
     while((len = fileIn.read(array, 0, 1023)) != -1){
      out.write(array, 0, len);
      try {
       Thread.sleep(100);
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     }
     try {
      Thread.sleep(2000);
     } catch (InterruptedException e) {
      e.printStackTrace();
     } finally {
      fileIn.close();
      out.close();
     }
     
    } catch (IOException e) {
     System.out.println("可能文件路径出错请确认:");
     e.printStackTrace();
    }
   }
  //}
  
 }

 public static void main(String[] args) {
  new ServerZipStream();
 }
}

 

(2)客户端

 

package ccp330.gucheng.zip.io;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ClientZipStream extends Frame implements ActionListener {

 private static final long serialVersionUID = 1L;
 
 private static int count=0;
 private TextField text = null;
 private Button ok = null;
 private TextArea area = null;
 private static int conutVisable = 0; //计算隐藏的窗口数,三个都隐藏则系统退出

 private InetAddress address = null;
 
 public ClientZipStream () {
  super("客户 " + (++count));
  Panel p = new Panel();
  text = new TextField("127.0.0.1", 30);//默认为自己IP
  text.addKeyListener(new KeyAdapter(){
   public void keyPressed(KeyEvent ke) {
    if(ke.getKeyCode() == KeyEvent.VK_ENTER){
     if("".equals(text.getText().trim())){
      connectServer(text.getText().trim());     
     }
    }
   }
   
  });
  ok = new Button("连接");
  ok.addActionListener(this);
  p.add(new Label("请输入你要连接的地址:"));
  p.add(text);
  p.add(ok);
  this.add(p, BorderLayout.NORTH);
  
  area = new TextArea();
  this.add(area, BorderLayout.CENTER);
  
  this.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent we){
    setVisible(false);
    conutVisable++;
    if(conutVisable==3){
     //三个都隐藏则系统退出
     System.exit(0);
    }
   }
  });
  //this.setBounds(200, 200, 600, 400);
  this.setSize(600, 400);
  this.setVisible(true);
 }

 

 @Override
 public void actionPerformed(ActionEvent ae) {
  if(ae.getSource()==ok){
   if(!("".equals(text.getText().trim()))){
    System.out.println("执行到这!");
    connectServer(text.getText().trim());     
   }
  }
 }
 
 private void connectServer(String strAddress){
  try {
   address  = InetAddress.getByName(strAddress);
   Socket clientSocket = new Socket(address, 3300);
   new ClientThread(clientSocket).start();
  } catch (UnknownHostException e) {
   System.out.println("地址找不着或无法解释");
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 class ClientThread extends Thread {

  private boolean isRun = true;
  private Socket clientSocket = null;
  private ZipInputStream zipIn = null;
  private ZipEntry entry = null;
  
  public ClientThread(Socket clientSocket){
   this.clientSocket = clientSocket;
   
  }
  
  @Override
  public void run() {
   try {
    zipIn=new ZipInputStream(clientSocket.getInputStream());
   } catch (IOException e) {
    e.printStackTrace();
   }
   while(isRun){
    byte[] array = new byte[1024];
    try {
     while((entry=zipIn.getNextEntry()) != null){
      area.append("\n" + entry.toString() + "\n\n");
      int n = -1;
      while((n=zipIn.read(array, 0, 1023))!=-1){
       String str=new String(array, 0, n);
       area.append(str);
      }
     }
     stopThread();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  
  public void stopThread() {
   isRun = false;
  }
  
 }
 
 
 public static void main(String[] args) {
  //同时产生三个客户端程序
  new ClientZipStream().setLocation(50, 50);
  new ClientZipStream().setLocation(350, 50);
  new ClientZipStream().setLocation(650, 50);
 }

}

  评论这张
 
阅读(390)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017