开发者社区> 问答> 正文

采用ObjectInputStream序列化收发文件例子

求一个:java socket 采用ObjectInputStream序列化收发文件例子

展开
收起
蛮大人123 2016-02-26 13:33:08 1910 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    服务器端:

    class javaserver extends Thread {
     ServerSocket server;
    
     public javaserver() {
     try {
     server = new ServerSocket(6000);
     } catch (IOException e) {
     System.out.println("Cannot   create   Server");
     System.exit(0);
     }
     System.out.println("Now   socket   server   will   Start");
     this.start();
     }
    
     public void run() {
     try {
     while (true) {
     Socket client = server.accept();
     // System.out.println("IP: "+client.getInetAddress());//get
     // client's ip address;
     service ss = new service(client);
     }
     } catch (IOException e) {
     System.out.println("cannot   provide   service   !");
     System.exit(1);
     }
     }
    
     public static void main(String args[]) {
     new javaserver();
     /*
      * KeyInput = new DataInputStream(System.in); try { data =
      * KeyInput.readLine(); } catch (IOException e) { return; } if
      * (data.equals("quit")) System.exit(1);
      */
     }
     }
    
     class service extends Thread {
    
     DataInputStream InputS;
    
     AddImages addimag ;
    
     DataInputStream dis = null;
    
     DataOutputStream dos = null;
     DataOutputStream OutputS;
    
     Socket Client;
    
     public service(Socket ClientSocket) {
     Client = ClientSocket;
     try {
     InputS = new DataInputStream(Client.getInputStream());
     } catch (IOException e) {
     System.out.println("Cannot   Connect   with   Client   !");
     return;
     }
     this.start();
     }
    
     public void run() {
     try {
     ObjectOutputStream out = new ObjectOutputStream(Client
     .getOutputStream());
     OutputStream ops = Client.getOutputStream();
     List images = message.getImageFiles();
     if (!images.isEmpty()) {
     out.writeObject(images);
     out.flush();
     File image = null;
     for (int j = 0; j < images.size(); j++) {
     image = (File)images.get(j); // 文件已经根据路径New File了
    BufferedInputStream imgstream = new BufferedInputStream(
     new FileInputStream(image));
     dis = new DataInputStream(
     new BufferedInputStream(
     new FileInputStream(image)));
     dos = new DataOutputStream(
     new BufferedOutputStream(ops));
     int imglength = Integer.parseInt(String
     .valueOf(image.length()));
     byte[] buf = new byte[imglength];
     while ((dis.read(buf)) != -1) {
     dos.write(buf, 0, buf.length);
     }
     }
     dos.close();
     dos.flush();
    
     }
     } catch (Exception e) {
     System.out.println("Read   Data   error");
     e.printStackTrace();
     }
       try {
       Client.close(); 
       } catch (IOException e) {
       System.out.println("Cannot close socket");
       }
     }
     }

    客户端:

    class javaclient {
    
     private static Socket Client;
    
     public static void main(String args[]) {
     try {
     InetAddress in = InetAddress.getLocalHost();
     System.out.println(in.getHostAddress());// get host ip address;
     Client = new Socket(in.getHostAddress(), 6000);
     } catch (IOException e) {
     System.out.println("Cannot   Connect with   Server");
     e.printStackTrace();
     }
     try {
    
     ObjectInputStream in = null;
     if (Client.getInputStream() != null) {
     in = new ObjectInputStream(Client.getInputStream());
     } else {
     return;
     }
     DataOutputStream dos = null;
     DataInputStream dis = null;
     Collection messages = (Collection) in.readObject(); // 接收到的文字信息集合
    List info = (List) messages;
     System.out.println("Size: " + info.size());
     if (!messages.isEmpty()) {
     for (int i = 0; i < info.size(); i++) {
     File imageFile = (File) info.get(i);
     String imagename = imageFile.getName(); // 图片名字
    System.out.println("Length: " + imageFile.length());
     int imglength = Integer.parseInt(String. // 图片长度
    valueOf(imageFile.length()));
     InputStream ips =Client.getInputStream();
     System.out.println("available: "+ips.available());
     File image = new File("E:\\" + imagename);
     File renameimg = new File("E:\\" + i + imagename);
     BufferedInputStream br = new BufferedInputStream(
     ips);
     dis = new DataInputStream(br);
     // 以DataInputStream来包装字节缓冲输入流
    if (!image.exists()) {
     // 以DataOutputStream来包装字节缓冲输出流
    dos = new DataOutputStream(new BufferedOutputStream(
     new FileOutputStream(image))); // 将图片保存到文件夹
    } else {
     dos = new DataOutputStream(new BufferedOutputStream(
     new FileOutputStream(renameimg)));
     }
     byte[] bufr = new byte[imglength];
     // 网络传输都是以字节的方式传递的
    while ((dis.read(bufr)) != -1) {
     // 一边读,一边写
    dos.write(bufr, 0, bufr.length);
     }
     System.out.println("客户端端接收完毕");
     }
     } else {
     }
     } catch (Exception e) {
     System.out.println("IOException Happened");
     e.printStackTrace();
     }
     try {
     System.out.println("Now   will  end  this   program");
     Client.close();
     } catch (IOException e) {
     System.out.println("system   cannot  close   socket");
     e.printStackTrace();
     }
     }
     }
    2019-07-17 18:48:08
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载