Java Socke实现的文件服务器。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fuck51cto/article/details/80524445

服务端代码:

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;  
import java.io.File;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket; 

/*
*author: zvall
*/

public class Server extends Thread {  
    
    private String FileName;  
 
    private int port;  
  
    public String getFileName() {  
        return FileName;  
    }  
  
    public void setFileName(String filename) {  
        this.FileName = filename;  
    }  
  
    public int getPort() {  
        return port;  
    }  
  
    public void setPort(int port) {  
        this.port = port;  
    }  
  
    public static void main(String[] args) {  
    	if(args.length ==0 ) {
    		System.out.print("uage: java -jar Server.jar \\tmp\\file.zip 8080");
    		System.exit(0);
    		
    	}
    	//System.out.println(args[0]);
    	Server server = new Server();  
       // server.setFileName("M:\\data\\responsive_pba\\log1.txt");  
    	server.setFileName(args[0]);
        server.setPort(Integer.parseInt(args[1]));  
        server.start();  
    }  
    
    
    public void DownFile(String filePath){

    	}
 
  
  
    public void run() {  
    	
	       try {
	           while (true) {
	        	   ServerSocket server= new ServerSocket(port);
	
	               File fi = new File(FileName);
	               System.out.println("File length:" + (int) fi.length());
	       
	               System.out.println("Create a socket link\n");
	               System.out.println("Waiting for client link\n");  
	               Socket socket = server.accept();
	               
	            
	               DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(FileName)));
	               DataOutputStream ps = new DataOutputStream(socket.getOutputStream());
	             
	  
	 
	               int bufferSize = 8192;
	               byte[] buf = new byte[bufferSize];
	               while (true) {
	                   int read = 0;
	                   if (fis != null) {
	                       read = fis.read(buf);
	                
	                   }
	                   if (read == -1) {
	                       break;
	                   }
	                   ps.write(buf, 0, read);
	                   ps.flush();
	               }
	               ps.flush();            
	               fis.close();
	               socket.close();                
	               System.out.println("File transfer completed\n");
	               System.exit(0);
	           }
	       } catch (Exception e) {
	           e.printStackTrace();
	       } 
    	
    	 
  
       
    }  
  
} 

客户端代码:

import java.io.DataInputStream;  
import java.io.FileOutputStream;
import java.io.IOException;  
import java.net.Socket;
  
/*
*author: zvall
*/
public class Client  extends Thread{  
    private String saveFile;  

    private String host;  
    private int port;  
    
  
    public void setSaveFile(String filePath) {  
        this.saveFile = filePath;  
    }  

    public void setPort(int port) {  
        this.port = port;  
    }  

    public void setHost(String host) {  
        this.host = host;  
    }  
      
    public static void main(String[] args) {  
      	if(args.length ==0 ) {
    		System.out.print("file down uage: java -jar Client.jar 192.168.1.56 8080 e:\\savepath\\test.7z");
    		System.exit(0);
    		
    	}
        Client client = new Client();  
        client.setHost(args[0]);  
        client.setPort(Integer.parseInt(args[1]));  
        //client.setSaveFile("M:\\aaa\\down\\log2.txt");
        client.setSaveFile(args[2]);
        client.start();
    }  
    
    public void run() {
    	
    	try { 
    	    Socket socket = new Socket(host,port);
    	   // long passedlen = 0;
    	    if(socket.isConnected()) {
    	    	System.out.println("connection succeeded!\n");
    	    }
    	    
    	    System.out.println("Start receiving files!" + "\n");  
            DataInputStream is = new  DataInputStream(socket.getInputStream());   
            FileOutputStream fos = new FileOutputStream(saveFile);  
    
            
            byte[] b = new byte[1024]; 
    	    int length = 0;  
    	    // len = Long.parseLong(new String(buf, 0, is.));  
    	    while((length=is.read(b))!=-1){
 
    	    	fos.write(b, 0, length);  
    	        fos.flush();
    	     }  
    	     fos.flush();  
    	     fos.close();               
    	     is.close();  
    	     socket.close();  
    	  
    	           

    	       } catch (IOException e) {  
    	           e.printStackTrace();  
    	           System.exit(0);
    	       }   
    	 System.out.println("File download completed\n");
    	}  
     
 
    
    
    
  

  
}  

猜你喜欢

转载自blog.csdn.net/fuck51cto/article/details/80524445
今日推荐