2013-02-07 38 views
1

我正在創建一個簡單的程序,允許上傳和下載到文件服務器。在服務器完全在線之前,我需要在上傳和下載之間進行選擇。事情是,我對DataInput/Output流等感到困惑,目前上傳選項不起作用。它會自己工作,但不是我在這裏編碼的方式。它在這個階段破壞我的頭!多個數據輸入/輸出流混淆

Server代碼:

import java.io.*; 
import java.net.*; 

import javax.swing.JOptionPane; 

public class Server_Test { 

public static void main(String a[]) throws Exception { 

    int timeoutsecs = 600; 
    int port = 4444; 
    Socket sock; 
    ServerSocket servsock = new ServerSocket(port, timeoutsecs); 

    while (true) { 

     // wait for the next client connection 
     sock = servsock.accept(); 

     DataInputStream choice = new DataInputStream(sock.getInputStream()); 


     if (choice.read() == 1){ 

     // ****1***** 
     // Download 
     // Send I/O streams to the socket 



     DataOutputStream out = new DataOutputStream(sock.getOutputStream()); 
     new Server_Test().sendFile(out); 

     }else if (choice.read() == 2){ 

     // ****2**** 
     //Upload 
     // Receive I/O streams from socket 



     DataInputStream in = new DataInputStream(sock.getInputStream()); 
     new Server_Test().receiveFile(in); 

     } 

     // Close this connection, (not the overall // server socket) 

     sock.close(); 

} 
} 

public void sendFile(DataOutputStream os) throws Exception{ 

    // String fileLoc = JOptionPane.showInputDialog(null, "What is the file location of the file you want to download?"); 
    File file = new File("C:\\TestFile.txt"); 
    FileInputStream fis = new FileInputStream(file); 
    byte [] mybytearray = new byte [(int)file.length()+1]; 

    BufferedInputStream bis = new BufferedInputStream(fis); 

     bis.read(mybytearray,0,mybytearray.length); 
     System.out.println("Sending..."); 
     os.write(mybytearray,0,mybytearray.length); 
     os.flush(); 

    bis.close(); 


} 

public void receiveFile(DataInputStream in) throws Exception{ 

    int filesize=6022386; 
     int bytesRead; 
     int current = 0; 
     byte [] mybytearray = new byte [filesize]; 

     FileOutputStream fos = new FileOutputStream("C:\\UploaTest.txt"); 
     BufferedOutputStream bos = new BufferedOutputStream(fos); 
     bytesRead = in.read(mybytearray,0,mybytearray.length); 
     current = bytesRead; 


     do { 
      bytesRead = 
       in.read(mybytearray, current, (mybytearray.length-current)); 
      if(bytesRead >= 0) current += bytesRead; 
     } while(bytesRead > -1); 

     bos.write(mybytearray, 0 , current); 
     bos.flush(); 
     bos.close(); 

} 

}

客戶端代碼:

import java.io.*; 
import java.net.*; 

import javax.swing.JOptionPane; 

public class Client_Test { 
public static void main(String a[]) throws Exception { 

    Socket sock; 


// ServerSocket serverSocket = new ServerSocket(5556); 

    sock = new Socket("127.0.0.1", 4444); 
    System.out.println("Connecting..."); 

    Integer choice = Integer.parseInt(JOptionPane.showInputDialog(null, "Please type 1 for download and 2 for upload.")); 
    DataOutputStream outs = new DataOutputStream(sock.getOutputStream()); 



    outs.write(choice); 
    //outs.flush(); 
    //outs.close(); 

    if(choice == 1){  

    // *****1***** 
    //Download 
    // Get I/O streams from the socket 
    DataInputStream is = new DataInputStream(sock.getInputStream()); 
    // receive file 
    new Client_Test().downloadFile(is); 

    }else if(choice == 2){ 

    //*****2**** 
    //Upload 
    //Send I/O streams to the socket. 
    DataOutputStream out = new DataOutputStream(sock.getOutputStream()); 
    new Client_Test().uploadFile(out); 

    } 


    sock.close(); 
} 

public void downloadFile(DataInputStream in) throws Exception{ 

    int filesize=6022386; 
     int bytesRead; 
     int current = 0; 
     byte [] mybytearray = new byte [filesize]; 

     FileOutputStream fos = new FileOutputStream("C:\\DowloadTest.txt"); 
     BufferedOutputStream bos = new BufferedOutputStream(fos); 
     bytesRead = in.read(mybytearray,0,mybytearray.length); 
     current = bytesRead; 


     do { 
      bytesRead = 
       in.read(mybytearray, current, (mybytearray.length-current)); 
      if(bytesRead >= 0) current += bytesRead; 
     } while(bytesRead > -1); 

     bos.write(mybytearray, 0 , current); 
     bos.flush(); 
     bos.close(); 


} 



public void uploadFile (DataOutputStream out)throws Exception{ 

    File file = new File("C:\\TestFile.txt"); 
    FileInputStream fis = new FileInputStream(file); 
    byte [] mybytearray = new byte [(int)file.length()+1]; 

    BufferedInputStream bis = new BufferedInputStream(fis); 

     bis.read(mybytearray,0,mybytearray.length); 
     System.out.println("Sending..."); 
     out.write(mybytearray,0,mybytearray.length); 
     out.flush(); 


     bis.close(); 

} 

} 

回答

2

你正面臨着由這些線路問題造成的:

if (choice.read() == 1){ 

}else if (choice.read() == 2){ 

流阻塞,直到數據可用,所以服務器在第一個等待線等待。

當客戶端發送1或2時,繼續讀取調用,並將讀取值與1進行比較。如果發送值2,則比較結果爲false,服務器繼續到第二行,並等待更多輸入的塊。

此時應更換兩次讀取調用與一個單一的一個:

int choiceValue = choice.read(); 
if (choiceValue==1) 
{ 
} 
else if (choiceValue==2 
{ 
}  

這應該可以解決當前的問題。

我覺得有可能是在您Server_Test.receiveFile另一個問題()方法,因爲它似乎您試圖讀取該文件兩次,一次在這條線

bytesRead = in.read(mybytearray,0,mybytearray.length); 

,然後再在循環:

do { 
      bytesRead = 
       in.read(mybytearray, current, (mybytearray.length-current)); 
      if(bytesRead >= 0) current += bytesRead; 
     } while(bytesRead > -1);