2016-09-16 130 views

回答

0

用java插座

服務器/發件人傳輸文件

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.Scanner; 

public class Sender { 
    public static Scanner scanner; 
    /** 
    * 
    * @param args 
    * this function collect all required data from user. 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException 
    { 
     String fileLocation; 
     int portNo; 
     scanner=new Scanner(System.in); 
     System.out.println("Enter port number of machine(e.g. '2000') :"); 
     portNo=scanner.nextInt(); 
     System.out.println("Please enter file location with file name (e.g. 'D:\\abc.txt'): "); 
     fileLocation=scanner.next(); 
     Sender.send(portNo,fileLocation); 
    } 
    /** 
    * this function actually transfers file 
    * @param portNo 
    * @param fileLocation 
    * @return 
    * @throws IOException 
    */ 
    public static void send(int portNo,String fileLocation) throws IOException 
    { 

     FileInputStream fileInputStream = null; 
     BufferedInputStream bufferedInputStream = null; 

     OutputStream outputStream = null; 
     ServerSocket serverSocket = null; 
     Socket socket = null; 

     //creating connection between sender and receiver 
     try { 
      serverSocket = new ServerSocket(portNo); 
      System.out.println("Waiting for receiver..."); 
       try { 
         socket = serverSocket.accept(); 
         System.out.println("Accepted connection : " + socket); 
         //connection established successfully 

         //creating object to send file 
         File file = new File (fileLocation); 
         byte [] byteArray = new byte [(int)file.length()]; 
         fileInputStream = new FileInputStream(file); 
         bufferedInputStream = new BufferedInputStream(fileInputStream); 
         bufferedInputStream.read(byteArray,0,byteArray.length); // copied file into byteArray 

         //sending file through socket 
         outputStream = socket.getOutputStream(); 
         System.out.println("Sending " + fileLocation + "(size: " + byteArray.length + " bytes)"); 
         outputStream.write(byteArray,0,byteArray.length);   //copying byteArray to socket 
         outputStream.flush();          //flushing socket 
         System.out.println("Done.");        //file has been sent 
        } 
        finally { 
         if (bufferedInputStream != null) bufferedInputStream.close(); 
         if (outputStream != null) bufferedInputStream.close(); 
         if (socket!=null) socket.close(); 
        }  
      } catch (IOException e) { 

       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      finally { 
       if (serverSocket != null) serverSocket.close(); 
      } 
    } 
} 

客戶端/接收器

import java.io.BufferedOutputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.Socket; 
import java.util.Scanner; 

public class Receiver { 


    public static Scanner scanner; 


    public static void main (String [] args) throws IOException { 


     String fileLocation,ipAddress; 
     int portNo; 
     scanner=new Scanner(System.in); 
     System.out.println("Enter ipAddress of machine(if you are testing this on same machine than enter 127.0.0.1) :"); 
     ipAddress=scanner.next(); 

     System.out.println("Enter port number of machine(e.g. '2000') :"); 
     portNo=scanner.nextInt(); 
     System.out.println("Please enter file location with file name to save (e.g. 'D:\\abc.txt'): ");  //you can modify this program to receive file name from server and then you can skip this step 
     fileLocation=scanner.next(); 
     Receiver.receiveFile(ipAddress, portNo, fileLocation); 


    } 
    public static void receiveFile(String ipAddress,int portNo,String fileLocation) throws IOException 
    { 

     int bytesRead=0; 
     int current = 0; 
     FileOutputStream fileOutputStream = null; 
     BufferedOutputStream bufferedOutputStream = null; 
     Socket socket = null; 
     try { 

      //creating connection. 
      socket = new Socket(ipAddress,portNo); 
      System.out.println("connected."); 

      // receive file 
      byte [] byteArray = new byte [6022386];     //I have hard coded size of byteArray, you can send file size from socket before creating this. 
      System.out.println("Please wait downloading file"); 

      //reading file from socket 
      InputStream inputStream = socket.getInputStream(); 
      fileOutputStream = new FileOutputStream(fileLocation); 
      bufferedOutputStream = new BufferedOutputStream(fileOutputStream); 
      bytesRead = inputStream.read(byteArray,0,byteArray.length);     //copying file from socket to byteArray 

      current = bytesRead; 
      do { 
       bytesRead =inputStream.read(byteArray, current, (byteArray.length-current)); 
       if(bytesRead >= 0) current += bytesRead; 
      } while(bytesRead > -1); 
      bufferedOutputStream.write(byteArray, 0 , current);       //writing byteArray to file 
      bufferedOutputStream.flush();            //flushing buffers 

      System.out.println("File " + fileLocation + " downloaded (size: " + current + " bytes read)"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally { 
      if (fileOutputStream != null) fileOutputStream.close(); 
      if (bufferedOutputStream != null) bufferedOutputStream.close(); 
      if (socket != null) socket.close(); 
     } 
    } 
} 
相關問題