Java 客户端/服务器系统从客户端上传到服务器不准确
我无法将文件上载到服务器。服务器已收到文件,但在发送文件后不会停止接收。如何让它只收到一次?我怀疑我包含的服务器代码部分有问题,希望有人能够识别问题=( 下面是代码 Client.javaJava 客户端/服务器系统从客户端上传到服务器不准确,java,netbeans,client-server,Java,Netbeans,Client Server,我无法将文件上载到服务器。服务器已收到文件,但在发送文件后不会停止接收。如何让它只收到一次?我怀疑我包含的服务器代码部分有问题,希望有人能够识别问题=( 下面是代码 Client.java System.out.println("These are the files available on the Server: \n"); String line = null; while ((line = get1.readLine()) != null) { System.out.print
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;
}
}
Server.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这里是服务器代码的前面部分
ServerSocket ss=null;
try{
ss=new ServerSocket(8081);
} catch(IOException e){
System.out.println("couldn't listen");
System.exit(0);
}
Socket cs=null;
try{
cs=ss.accept();
System.out.println("Connection established"+cs);
} catch(Exception e){
System.out.println("Accept failed");
System.exit(1);
}
PrintWriter put=new PrintWriter(cs.getOutputStream(),true);
BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));
开始时需要注意的是:请正确格式化代码,以便阅读。大多数IDE都有一个相当不错的自动格式化功能。请在粘贴代码之前使用它 现在谈谈你的问题: 我看到您的服务器在if之后的开始处有一个无限while循环。从您发布的代码片段来看,这是我看到的最大问题。因此,一旦它接收完第一个文件,它就会通过while循环循环到下一个文件
尝试删除服务器代码开头的while(true),看看这是否能解决问题。但是while循环用于流连接。确定吗?这部分应该用于流连接:while((read=d.read(buffer))!=-1{outStream.write(buffer,0,read);outStream.flush();//当输入缓冲区未完成时,它读取服务器发送的输入并将其写入在客户端计算机中创建的文件}