2014-01-27 51 views
0

我在上传文件到服务器时遇到问题。服务器收到文件,但一旦发送文件就不会停止接收。如何让它只接受一次?我怀疑我已经包括有问题的服务器的代码部分的一部分,希望有人能找出问题所在=(客户端/服务器系统从客户端向服务器上传不准确

下面是代码。

Client.java

System.out.println("These are the files available on the Server: \n"); 
String line = null; 
while ((line = get1.readLine()) != null) { 
    System.out.println(line); 

} 
int ans1; 
Scanner scan = new Scanner(System.in); 

System.out.println("\n1.Upload a file \n2.Download file \n"); 
int ans = scan.nextInt(); 
put.print(ans); 
put.flush(); 
ans1 = ans; 



switch(ans1){ 
    case 1: 
     System.out.println("Name the file u wanna upload\n"); 
     String n=rd.readLine(); 
     put.println(n); 
     n ="Client\\"+n; 
//   System.out.println("Requesting "+n); 
      //physical source path is added to the name of file 
       File files=new File(n); 
       //open the file requested by client on the server machine 
       if(files.exists()){ 
       BufferedInputStream di=new BufferedInputStream(new FileInputStream(n)); 
       //creates a buffer stream to read bytes from the file 
       BufferedOutputStream outStream = new BufferedOutputStream(s.getOutputStream()); 
       //create a buffer stream to send bytes to the client socket 
       byte buffer[] = new byte[1024]; 
       // creates a buffer to read from the file 
       int read; 
       while((read = di.read(buffer))!=-1){ 

      outStream.write(buffer, 0, read); 
      outStream.flush(); 
      //while file is not finished it reads bytes from the file and send it to the client server 
     } 
      s.close(); 
      } 
     break; 


    case 2: 
     String u,f; 
     System.out.println("Enter the file name to download from server:"); 
     DataInputStream dis=new DataInputStream(System.in); 
     f=dis.readLine(); 
     //it reads the name of file from the user 
     put.println(f); 
     //it sends the name of requested file to the server machine 
     File f1=new File("Client\\"+f); 
     //it creates a file with the same name in the physical path given on the client machine 
     FileOutputStream fs=new FileOutputStream(f1); 
     //it creates an output stream to write bytes to the file 
     BufferedInputStream d=new BufferedInputStream(s.getInputStream()); 
     //it creates buffer to receive data from server machine 
     BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(f1)); 
     //it creates a buffer to write buffer of bytes to the file 
     byte buffer[] = new byte[1024]; 
     int read; 
     while((read = d.read(buffer))!=-1){ 
     outStream.write(buffer, 0, read); 
     outStream.flush(); 
     //while the input buffer is not finished it reads the input sent by the server and writes it on the file created in the client machine 
     } 
     fs.close(); 
     //file is closed 
     System.out.println("File received"); 
     s.close(); 
     //socket is closed 
     System.out.println("wanna cont?"); 
     int yn = scan.nextInt(); 
     if(yn == 1){ 

     } 
      break; 
    case 3: 
     System.out.println("exit"); 
     break; 
} 


} 

服务器这里的.java

if(num == 49){ 
     while(true) 
      { 
       String t=st.readLine(); 
       put.println(t); 
       //it sends the name of requested file to the server machine 
       System.out.println("the file will be receiving is "+t); 
       File f1=new File("Server\\"+t); 
       //it creates a file with the same name in the physical path given on the client machine 
       FileOutputStream fs=new FileOutputStream(f1); 
       //it creates an output stream to write bytes to the file 
       BufferedInputStream d=new BufferedInputStream(cs.getInputStream()); 
       //it creates buffer to receive data from server machine 
       BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(f1)); 
       //it creates a buffer to write buffer of bytes to the file 
       byte buffer[] = new byte[1024]; 
       int read; 
       while((read = d.read(buffer))!=-1){ 
       outStream.write(buffer, 0, read); 
       outStream.flush(); 
       //while the input buffer is not finished it reads the input sent by the server and writes it on the file created in the client machine 
       } 
       fs.close(); 
       //file is closed 
       System.out.println("File received"); 
       cs.close(); 
       //socket is closed 
       ss.close(); 
       s2.close(); 

@Dakkaron是服务器代码

的前部

回答

0

开头说明:请正确格式化您的代码,以便阅读。大多数IDE都有一个相当不错的自动格式化功能。请在此处粘贴代码之前使用它。

现在你的问题: 我看到你的服务器有一个无限的while循环,直接在if后面。从你发布的代码片段来看,这是我看到的最大的问题。因此,一旦它完成接收第一个文件,它将通过while循环循环到下一个文件。

尝试删除服务器代码开始处的while(true),看看是否可以解决您的问题。

+0

但while循环用于流连接。 – Stephanie

+0

你确定吗?这部分应该用于流连接: while((read = d.read(buffer))!= - 1)outStream.write(buffer,0,read); outStream.flush(); //当输入缓冲区未完成时,它读取服务器发送的输入并将其写入客户机器 } – Dakkaron