我无法将文件上传到服务器。服务器收到文件,但一旦发送文件就不会停止接收。如何让它只收到一次?我怀疑我所包含的部分服务器代码有问题,希望有人能够识别问题=(
以下是代码。
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;
}
}
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()));
答案 0 :(得分:0)
开头的注释:请正确格式化代码,以便更容易阅读。大多数IDE都具有相当不错的自动格式功能。请在粘贴代码之前使用它。
现在问你的问题: 我看到你的服务器在if之后直接有一个无限的while循环。从您发布的代码片段中,这是我看到的最大问题。因此,一旦完成接收第一个文件,它就会通过while循环循环到下一个文件。
尝试删除服务器代码开头的while(true),看看是否能解决问题。