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是服務器代碼
的前部
但while循環用於流連接。 – Stephanie
你確定嗎?這部分應該用於流連接: while((read = d.read(buffer))!= - 1)outStream.write(buffer,0,read); outStream.flush(); //當輸入緩衝區未完成時,它讀取服務器發送的輸入並將其寫入客戶機器 } – Dakkaron