局域网文件传输和复制,含文件名

类别:Java 点击:0 评论:0 推荐:

局域网文件传输和复制,含文件名
 

实施局域网内文件复制,包括需要把文件名也拷贝过去。特点是文件名传输和文件传输动用了两个端口。进程完成这样的功能:每隔24小时进

行上传一次,保持两台机器的根目录下文件一致。

1:服务器端,也既待上传端。运行命令:java FileServer 3107
import java.io.*;
import java.net.*;

public class FileServer implements Runnable {
      
       private static int ipsocket = 0;
      
       public static void main(String[] args)throws Exception{
              ipsocket = Integer.parseInt(args[0]);
              //建议使用3107
              System.out.println("系统在端口:" + ipsocket + "等待发送");
              Thread t1 = new Thread(new FileServer());
              t1.start();
       }
      
       public void run(){
              while(true){
               try{
                String[] fileNames = seachFileName(1);
                if(fileNames == null || fileNames.length < 1) return;
                for(int i = 0; i < fileNames.length; i ++){
                      sendFileName(fileNames[i]);
                      sendFile(fileNames[i]);
                }       
                       Thread.sleep(24 * 60 * 60 * 1000); //24小时
        }catch(Exception e) {
         System.out.println(e);
        }
       }
       }
      
 /*
 listType = 0 返回含文件夹和文件
 listType = 1 只返回文件
 */
       public static String[] seachFileName(int listType) throws Exception{
              String directory = ".";
              File dir = new File(directory);
           File dirTemp;
           String[] filesTemp;
           String[] fileNames;
           String[] re;
           int length = 0;
           if (!dir.isDirectory()) 
             throw new IllegalArgumentException("FileList: no such directory");

           filesTemp = dir.list();        
              java.util.Arrays.sort(filesTemp);
    
              for(int i = 0; i < filesTemp.length; i++) {
                    dirTemp = new File(directory + "\\" + filesTemp[i]);
                    if(!dirTemp.isDirectory()){
                            length++;
                    }
              }    
              fileNames = new String[length];  
              length = 0;           
              for(int i = 0; i < filesTemp.length; i++) {
                    dirTemp = new File(directory + "\\" + filesTemp[i]);
                    if(!dirTemp.isDirectory()){
                         fileNames[length] = filesTemp[i];   
                    }
                    length++;
              }
              if(listType == 0) re = filesTemp;
            else re = fileNames;
            return re;

        }
       
        /*发文件名
 */
        private static void sendFileName(String fileNames) throws Exception{
              if(fileNames == null) return;
       //创建网络服务器接受客户请求
              ServerSocket ss=new ServerSocket(ipsocket);
              Socket client=ss.accept();
             
              //创建网络输出流并提供数据包装器
              OutputStream netOut=client.getOutputStream();
              OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut));
             
              //创建文件读取缓冲区
              byte[] buf=null;
              String fileName = fileNames;
              buf = fileName.getBytes();
              int num=buf.length;
              if(num > 0){//是否读完文件
                    doc.write(buf,0,num);//把文件数据写出网络缓冲区
                     doc.flush();//刷新缓冲区把数据写往客户端
              }
              doc.close();
              ss.close();
 }
       /*发文件本身
        */
        private static void sendFile(String fileName) throws Exception{
              if(fileName == null) return;
       //创建文件流用来读取文件中的数据
              File file=new File(fileName);
              FileInputStream fos=new FileInputStream(file);
             
              //创建网络服务器接受客户请求
              ServerSocket ss=new ServerSocket(ipsocket + 1);
              Socket client=ss.accept();
             
              //创建网络输出流并提供数据包装器
              OutputStream netOut=client.getOutputStream();
              OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut));
             
              //创建文件读取缓冲区
              byte[] buf=new byte[2048];
              int num=fos.read(buf);
              while(num!=(-1)){//是否读完文件
                     doc.write(buf,0,num);//把文件数据写出网络缓冲区
                     doc.flush();//刷新缓冲区把数据写往客户端
                     num=fos.read(buf);//继续从文件中读取数据
              }
              fos.close();
              doc.close();
              ss.close();
         
        }
}

2:客户端,也既文件接受端。运行命令:java FileClient 127.0.0.1 3107。注意跟服务器端口要一致。
import java.io.*;
import java.net.*;
 
public class FileClient implements Runnable {
      
       private static String ip = "";
       private static int ipsocket = 0;
      
       public static void main(String[] args)throws Exception{
              ip = args[0];
              ipsocket = Integer.parseInt(args[1]);
              //建议使用3107
              System.out.println("系统在地址@" + ip + ":" + ipsocket + "倾听");
              Thread t1 = new Thread(new FileClient());
              t1.start();
       }
             
       public void run(){
              while(true){
               try{
                String strTemp = getFileName();
                getFile(strTemp);
               }catch(Exception e){}
               try{
                Thread.sleep(5 * 1000); //5秒
        }catch(Exception e) {
         System.out.println(e);
        }       
       }
       }
      
       private static String getFileName() throws Exception{
              // 通过Socket连接文件服务器
              Socket server=new Socket(ip,ipsocket);             

              //创建网络接受流接受服务器文件数据
              InputStream netIn=server.getInputStream();
              InputStream in=new DataInputStream(new BufferedInputStream(netIn));             

              //创建缓冲区缓冲网络数据
              byte[] buf=new byte[2048];
              int num=in.read(buf);
             
              while(num!=(-1)){//是否读完所有数据
                     num=in.read(buf);//继续从网络中读取文件
              }
              String fileName = new String(buf);
              fileName = fileName.trim();
              in.close();
              server.close();
              return fileName;             
       }
      
       private static void getFile(String strTemp) throws Exception{
              //使用本地文件系统接受网络数据并存为新文件
              File file=new File(strTemp);
             
              //如果文件已经存在,先删除
              if(file.exists()) file.delete();
              for(int i = 0 ; i <  10000; i++) {}
              file.createNewFile();
              RandomAccessFile raf=new RandomAccessFile(file,"rw");             

              // 通过Socket连接文件服务器
              Socket server=new Socket(ip,(ipsocket + 1) );             

              //创建网络接受流接受服务器文件数据
              InputStream netIn=server.getInputStream();
              InputStream in=new DataInputStream(new BufferedInputStream(netIn));             

              //创建缓冲区缓冲网络数据
              byte[] buf=new byte[2048];
              int num=in.read(buf);
             
              while(num!=(-1)){//是否读完所有数据
                     raf.write(buf,0,num);//将数据写往文件
                     raf.skipBytes(num);//顺序写文件字节
                     num=in.read(buf);//继续从网络中读取文件
              }
              in.close();
              raf.close();             
              server.close();
       }
}

关于本文:纯参考用,有意见欢迎BT
[email protected]或www.10ms.net


 

本文地址:http://com.8s8s.com/it/it11843.htm