A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© fmi110 高级黑马   /  2015-10-7 17:19  /  404 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Tcp协议传输文件时,传输完成一定要记得关闭输出流     new Socket().shutdownOutput();
以便将文件结尾标志传至服务器端,让程序正常终止。
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;


  9. public class TcpDemo4 {

  10.         /**
  11.          * TCP协议服务端多线程技术
  12.          * @throws Exception
  13.          */
  14.         public static void main(String[] args) throws Exception {
  15.                 ServerSocket ss = new ServerSocket(10888);
  16.                 while(true){
  17.                         Socket s = new Socket();
  18.                         s = ss.accept();
  19.                         new Thread(new Upload(s)).start();
  20.                 }
  21.         }

  22. }
  23. class Client{
  24.         public static void main(String[] args) throws Exception, IOException {
  25.                 Socket s = new Socket("192.168.220.1",10888);
  26.                 File src = new File("F:\\深色壁纸\\1 (2).jpg");
  27.                 FileInputStream fin = new FileInputStream(src);
  28.                 byte[] buf = new byte[1024];
  29.                 int len = 0;
  30.                 OutputStream os = s.getOutputStream();
  31.                 System.out.println("客户端开始传输...");
  32.                 while ((len = fin.read(buf)) != -1) {
  33.                         os.write(buf, 0, len);
  34.                 }
  35.                 s.shutdownOutput();
  36.                 InputStream in = s.getInputStream();
  37.                 len = in.read(buf);
  38.                 System.out.println(new String(buf, 0, len));
  39.                 fin.close();
  40.                 s.close();
  41.         }
  42. }
  43. class Upload implements Runnable{
  44.         private Socket s;
  45.         private int count = 0;
  46.         public Upload(Socket s) {
  47.                 super();
  48.                 this.s = s;
  49.         }

  50.         @Override
  51.         public void run() {
  52.                 System.out.println("服务器启动");
  53.                 String ip = s.getInetAddress().getHostAddress();
  54.                 File dir = new File("f:\\tcp多线程");
  55.                 dir.mkdirs();
  56.                 File des = new File(dir,ip+".jpg");
  57.                 while (des.exists())
  58.                         des = new File(dir, ip + "(" + ++count + ")" + ".jpg");
  59.                 FileOutputStream fos = null;
  60.                 try {
  61.                          fos = new FileOutputStream(des);
  62.                         InputStream in = s.getInputStream();
  63.                         byte[] buf = new byte[1024];
  64.                         int len = 0;
  65.                         System.out.println("开始上传...");
  66.                         while ((len = in.read(buf)) != -1) {
  67.                                 fos.write(buf, 0, len);
  68.                         }
  69.                         System.out.println("done");
  70.                         OutputStream os = s.getOutputStream();
  71.                         os.write("上传成功".getBytes());
  72.                 } catch (Exception e) {
  73.                         // TODO Auto-generated catch block
  74.                         e.printStackTrace();
  75.                 }finally{
  76.                         if(null!=fos)
  77.                                 try {
  78.                                         fos.close();
  79.                                 } catch (IOException e) {
  80.                                         e.printStackTrace();
  81.                                 }
  82.                         try {
  83.                                 s.close();
  84.                         } catch (IOException e) {
  85.                                 e.printStackTrace();
  86.                         }
  87.                 }
  88.         }
  89. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马