【Java源码】TCP协议之服务端与客户端文件传输

 

---------------------------------------------------------------------------------------------------------------------------------------

服务端

---------------------------------------------------------------------------------------------------------------------------------------

 

package socket.file;

import java.io.*;
import java.net.*;

public class FileServer {
 private static String basePath;

 public static void main(String[] args){

  ServerSocket ss = null;
  Socket sc = null;
  FileOutputStream fos = null;
  try {
   ss = new ServerSocket(2222);//创建端口
   sc = ss.accept();//进入阻塞
   BufferedInputStream bis = new BufferedInputStream(sc.getInputStream());//反冲区
   byte[] b = new byte[512];//接收文件名
   bis.read(b);//读取文件名
   fos = new FileOutputStream("src/");//目标路径

   String fileName = new File(new String(b).trim()).getName();
   String filePath = basePath + fileName;

   for (int k = bis.read(); k != -1; fos.write(b), k = bis.read())
    ;
   fos.flush();
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
  try {
   if (null != fos) {
    fos.close();
   }
   if (null != fos) {
    fos.close();
   }
   if (null != sc) {
    sc.close();
   }
   if (null != ss) {
    ss.close();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  }

 }

}

 

 

 

---------------------------------------------------------------------------------------------------------------------------------------

客户端

---------------------------------------------------------------------------------------------------------------------------------------

package socket.file;

import java.io.*;
import java.net.*;

public class FileClient {

 public static void main(String[] args) {
  Socket cs = null;
  BufferedOutputStream bos = null;
  FileInputStream fis = null;
  try {
   cs = new Socket("127.0.0.1", 2222);
   String filePath = "H:/Photos.jpg";
   bos = new BufferedOutputStream(cs.getOutputStream());
   byte[] b = new byte[512];
   System.arraycopy(filePath.getBytes(), 0, b, 0,
     filePath.getBytes().length);// src:源数组;DestPos:目标数组;dest:;destPos:;

   bos.write(b);
   fis = new FileInputStream(filePath);
   for (int k = fis.read(); k != -1; bos.write(k), k = fis.read())
    ;
   bos.flush();
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (null != bos)
     bos.close();
    if (null != fis)
     fis.close();
    if (null != cs)
     cs.close();
   } catch (IOException e) {
    e.printStackTrace();
   }

  }
 }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子兮曰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值