java 网络文件传输

平台要求:<o:p></o:p>

1.         WindowsUnix/Linux

2.         JAVAVCBCBDelphi

<o:p> </o:p>

功能要求:<o:p></o:p>

1.         通过SOCKET实现点对点的文件交换功能

2.         支持连接握手、登录校验、文件传输请求应答、出错重传等交互的通信协议机制

3.         提供简单的用户界面或命令行界面

java 代码
java 代码
  1. package filetranslate;   
  2.   
  3. import java.io.BufferedInputStream;   
  4. import java.io.BufferedOutputStream;   
  5. import java.io.DataInputStream;   
  6. import java.io.DataOutputStream;   
  7. import java.io.File;   
  8. import java.io.IOException;   
  9. import java.io.InputStream;   
  10. import java.io.OutputStream;   
  11. import java.io.RandomAccessFile;   
  12. import java.net.Socket;   
  13. import java.util.HashMap;   
  14.   
  15. public class FileServ extends Thread{   
  16.        
  17.     Socket sc=null;   
  18.     InputStream in =null;   
  19.     OutputStream out=null;   
  20.     DataInputStream inStream = null;   
  21.     DataOutputStream outStream = null;   
  22.     BufferedInputStream bn =null;   
  23.     BufferedOutputStream bo =null;   
  24.     String servercepath="d:/socketserver/";   
  25.        
  26.     String tempfilename=null;   
  27.     static int point=0;   
  28.        
  29.     HashMap hs=new HashMap();   
  30.   
  31.     public FileServ(){   
  32.            
  33.     }   
  34.        
  35.     public FileServ(Socket sc){   
  36.         this.sc=sc;   
  37.         try {   
  38.             bn=new BufferedInputStream( new DataInputStream(sc.getInputStream()));   
  39.             bo=new BufferedOutputStream(new DataOutputStream(sc.getOutputStream()));   
  40.         } catch (IOException e) {   
  41.             e.printStackTrace();   
  42.         }   
  43.            
  44.     }   
  45.     /**   
  46.      * 接受发送消息  
  47.      * @return 返回的消息  
  48.      * @throws IOException   
  49.      */  
  50.     public String receiverMessage() throws IOException {   
  51.         int num = 0;   
  52.         byte[] br = null;   
  53.         br = new byte[2048];   
  54.         num = bn.read(br);   
  55.         if(num!=-1){   
  56.             return new String(br, 0, num);     
  57.         }else{   
  58.             return null;   
  59.         }   
  60.            
  61.            
  62.     }/**   
  63.      * 接受文件  
  64.      * @return  
  65.      */    
  66.     public boolean receviceFile(){   
  67.         //使用本地文件系统接受网络数据并存为新文件   
  68.         File file=new File(servercepath+tempfilename);   
  69.         RandomAccessFile raf=null;   
  70.         int count=0;   
  71.         try {   
  72.             file.createNewFile();   
  73.             raf=new RandomAccessFile(file,"rw");   
  74.             byte[] buf=new byte[2048];   
  75.             int num=bn.read(buf);   
  76.                
  77.             while(num!=(-1)){//是否读完所有数据   
  78.                    raf.write(buf,0,num);//将数据写往文件   
  79.                    count=count+num;   
  80.                    raf.skipBytes(num);//顺序写文件字节   
  81.                    num=bn.read(buf);//继续从网络中读取文件   
  82.             }   
  83.             bn.close();   
  84.             raf.close();   
  85.                
  86.         } catch (IOException e) {   
  87.             hs.put(tempfilename, new Integer(count));   
  88.             e.printStackTrace();   
  89.             return false;   
  90.         }finally{   
  91.             if(raf!=null){   
  92.                 try {   
  93.                     raf.close();   
  94.                 } catch (IOException e) {   
  95.                     return false;   
  96.                 }   
  97.             }   
  98.                
  99.         }   
  100.         hs.put(tempfilename, new Integer(count));   
  101.         return true;   
  102.  }   
  103.   
  104. /**   
  105. * 接受断点文件  
  106. * @return  
  107. */  
  108.     public boolean recevicefollowFile(){   
  109.         //使用本地文件系统接受网络数据并存为新文件   
  110.         File file=new File(servercepath+tempfilename);   
  111.         RandomAccessFile raf=null;   
  112.         int count=0;   
  113.         try {   
  114.             raf=new RandomAccessFile(file,"rw");   
  115.             byte[] buf=new byte[2048];   
  116.             int num=bn.read(buf);   
  117.             while(num!=(-1)){//是否读完所有数据   
  118.                    raf.skipBytes(point);//顺序写文件字节   
  119.                    raf.write(buf,0,num);//将数据写往文件   
  120.                    count=count+num;   
  121.                    raf.skipBytes(num);//顺序写文件字节   
  122.                    num=bn.read(buf);//继续从网络中读取文件   
  123.             }   
  124.             bn.close();   
  125.             raf.close();   
  126.                
  127.         } catch (IOException e) {   
  128.             hs.put(tempfilename, new Integer(count));   
  129.             e.printStackTrace();   
  130.             return false;   
  131.         }finally{   
  132.             if(raf!=null){   
  133.                 try {   
  134.                     raf.close();   
  135.                 } catch (IOException e) {   
  136.                     return false;   
  137.                 }   
  138.             }   
  139.                
  140.         }   
  141.         hs.put(tempfilename, new Integer(count));   
  142.         return true;   
  143.  }   
  144.     /**   
  145.      * 发送消息  
  146.      * @return 返回的消息  
  147.      * @throws IOException   
  148.      */  
  149.     public void sendMessage(String str) throws IOException {   
  150.         byte[] bw = str.getBytes();   
  151.         try {   
  152.             bo.write(bw);   
  153.             bo.flush();   
  154.         } catch (IOException e1) {   
  155.             e1.printStackTrace();   
  156.         }   
  157.             }   
  158.     /**  
  159.      * 关闭流和socket  
  160.      *   
  161.      */  
  162.     public void destory() {   
  163.         if (bo != null) {   
  164.             try {   
  165.                 bo.close();   
  166.             } catch (IOException e) {   
  167.                 e.printStackTrace();   
  168.             }   
  169.         }   
  170.         if (bn != null) {   
  171.             try {   
  172.                 bn.close();   
  173.             } catch (IOException e) {   
  174.                 e.printStackTrace();   
  175.             }   
  176.             if (sc != null) {   
  177.                 try {   
  178.                     sc.close();   
  179.                 } catch (IOException e) {   
  180.                     e.printStackTrace();   
  181.                 }   
  182.             }      
  183.   
  184.         }   
  185.     }   
  186.     public void run() {   
  187.         while(true){   
  188.             try {   
  189.                 String str=receiverMessage()    ;   
  190.                 if(str!=null){   
  191.                 if(str.substring(01).equalsIgnoreCase("1")){   
  192.                     System.out.println("认证处理");   
  193.                     String [] strarry=str.split(",");   
  194.                     if(strarry[1].equalsIgnoreCase("admin")&&strarry[2].equalsIgnoreCase("admin")){   
  195.                         System.out.println("认证成功");   
  196.                         sendMessage("ok");   
  197.                     }else{   
  198.                         System.out.println("认证失败");   
  199.                         sendMessage("error");   
  200.                            
  201.                     }   
  202.                 }else if(str.substring(01).equalsIgnoreCase("2")){//上传请求处理   
  203.                     String [] strarry=str.split(",");   
  204.                     tempfilename=strarry[1].substring(strarry[1].lastIndexOf("/")+1);   
  205.                     System.out.println("文件接受:文件名为"+tempfilename);   
  206.                     if(!hs.containsKey(tempfilename)){   
  207.                         sendMessage("2,ok");   
  208.                         if(receviceFile()){   
  209.                         System.out.println("文件接受成功");   
  210.                         }else{   
  211.                         System.out.println("文件接受失败");   
  212.                         }   
  213.                         break;   
  214.                     }else{   
  215.                         sendMessage("2,error,"+((Integer)hs.get(tempfilename)).intValue());   
  216.                            
  217.                     }   
  218.                        
  219.                 }else if(str.substring(01).equalsIgnoreCase("3")){//重传处理3,1.  续传3,2   
  220.                     String [] strarry=str.split(",");   
  221.                     if(strarry[1].equalsIgnoreCase("1")){   
  222.                         System.out.println("客户端重新上传文件");   
  223.                         sendMessage("3,ok");   
  224.                         File fl=new File(servercepath+tempfilename);   
  225.                         if(fl.exists())   
  226.                         {   
  227.                         fl.delete();       
  228.                         }   
  229.                         hs.remove(tempfilename);   
  230.                         if(receviceFile()){   
  231.                             System.out.println("文件接受成功");   
  232.                             }else{   
  233.                             System.out.println("文件接受失败");   
  234.                             }   
  235.                         break;   
  236.                     }else if(strarry[1].equalsIgnoreCase("2")){   
  237.                         System.out.println("客户端续传文件");   
  238.                         sendMessage("3,ok");   
  239.                         hs.remove(tempfilename);   
  240.                         if(receviceFile()){   
  241.                             System.out.println("续传接受成功");   
  242.                             }else{   
  243.                             System.out.println("续传接受失败");   
  244.                             }   
  245.                         break;   
  246.                     }   
  247.                        
  248.                 }   
  249.                 }   
  250.             } catch (IOException e) {   
  251.                    
  252.             }   
  253.                
  254.     }   
  255.     }   
  256.        
  257. }   
java 代码
  1. package filetranslate;   
  2.   
  3. import java.io.*;   
  4. import java.net.*;   
  5. import java.util.HashMap;   
  6. /**   
  7.  * 文件传输服务器端口  
  8.  * @author sydxide  
  9.  *  
  10.  */  
  11.   
  12. public class FileServer {   
  13.     ServerSocket ss = null;   
  14.   
  15.     Socket s = null;   
  16.   
  17.     DataInputStream inStream = null;   
  18.   
  19.     DataOutputStream outStream = null;   
  20.        
  21.     String servercepath="d:/socketserver/";   
  22.        
  23.     String tempfilename=null;   
  24.     static int point=0;   
  25.        
  26.     HashMap hs=new HashMap();   
  27.   
  28.     public FileServer() {   
  29.         try {   
  30.             ss = new ServerSocket(5678);   
  31.             init();   
  32.         } catch (Exception e) {   
  33.             System.out.println(e.toString());   
  34.         }   
  35.     }   
  36. /**   
  37.  * 初始化SOCKET  
  38.  * @throws Exception  
  39.  */  
  40.     public void init() throws Exception {   
  41.            
  42.         s = ss.accept();   
  43.         System.out.println("客户端连接了服务器 "+s.getInetAddress());   
  44.         if(inStream==null){   
  45.         inStream = new DataInputStream(s.getInputStream());}   
  46.         if(outStream==null){   
  47.         outStream = new DataOutputStream(s.getOutputStream());}   
  48.         process();   
  49.     }   
  50.        
  51.     /**   
  52.      * 接受发送消息  
  53.      * @return 返回的消息  
  54.      * @throws IOException   
  55.      */  
  56.     public String receiverMessage() throws IOException {   
  57.         int num = 0;   
  58.         byte[] br = null;   
  59.         br = new byte[2048];   
  60.         num = inStream.read(br);   
  61.         if(num!=-1){   
  62.             return new String(br, 0, num);     
  63.         }else{   
  64.             return null;   
  65.         }   
  66.            
  67.            
  68.     }   
  69.     /**   
  70.      * 根据接受的参数进行处理  
  71.      *  
  72.      */  
  73.         public  void process(){   
  74.         while(true){   
  75.                 try {   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值