java socket实现文件的发送接收

//接收端
import java.net.*;
import java.io.*;
public class GetFile {
 
     /**
      * @param args
      */
     public static void main(String[] args) {
         // TODO Auto-generated method stub
 
         
     
         
 
         ServerSocket server = null ;
         Socket you = null ;
         String s = null ;
         DataOutputStream out = null ;
         DataInputStream in = null ;
         String path = "D:/屏幕.jpg" ;
         RandomAccessFile inFile;
         byte [] byteBuffer = new byte [ 1024 ];
         InputStream inSocket;
         
         try {
             server = new ServerSocket( 4331 );
         } catch (IOException e1){
             System.out.println( "ERRO:" +e1);
         }
         try {
             you=server.accept();
//          in = new DataInputStream(you.getInputStream());
//          out = new DataOutputStream(you.getOutputStream());
//          while(true){
//              s=in.readUTF();
//              out.writeUTF("你好:我是服务器");
//              out.writeUTF("你说的数是:" + s);
//             
//              System.out.println("服务器收到:" + s);
//              Thread.sleep(500);
//             
//      }
             inSocket = you.getInputStream();
             inFile = new RandomAccessFile( new File(path), "rw" );
             int amount;
             while ((amount = inSocket.read(byteBuffer)) != - 1 ) { 
                 inFile.write(byteBuffer, 0 , amount); 
            
inFile.close();
you.close();
server.close();
         } catch (IOException e){
             System.out.println( "" +e);
         }
         
         
         
     }
 
}
 
//传送端
import java.net.*;
import java.io.*;
public class SendFile {
 
     /**
      * @param args
      */
     public static void main(String[] args) {
         // TODO Auto-generated method stub
 
         String s = null ;
         Socket mysocket;
         DataInputStream in = null ;
         DataOutputStream out = null ;
         RandomAccessFile outFile ;
         byte [] byteBuffer = new byte [ 1024 ];
         int amount;
         OutputStream outSocket;
         String path = ( "D:/测试/屏幕.jpg" );
         
         try {
             mysocket = new Socket( "111.117.111.196" , 4331 );
             outSocket = mysocket.getOutputStream();
             
             //in = new DataInputStream(mysocket.getInputStream());
             //out = new DataOutputStream(mysocket.getOutputStream());
             //out.writeUTF("你好");
//  while(true){
//              s=in.readUTF();
//              out.writeUTF(":" + Math.random());
//              System.out.println("客户收到:" + s);
//              Thread.sleep(500);
//  }
             outFile = new RandomAccessFile( new File(path), "r" );
               while ((amount = outFile.read(byteBuffer)) != - 1 ) { 
                     outSocket.write(byteBuffer, 0 , amount); 
                     System.out.println( "文件发送中..." ); 
                
               outFile.close();
mysocket.close();
         } catch (IOException e){
             System.out.println( "无法连接" );
         }
         
         
         
         
     }
 
}



网上程序当来当去,本身BUG真是不少,还是自己动手亲自写比较好,改动的地方清晰可见。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值