我想获取文件长度和块数,文件保存在服务器的文件夹中,所以当客户端发送请求获取文件长度时,客户端必须插入文件名,然后长度和块号该文件必须显示给客户端。这里的问题是长度只显示所有文件的相同数字。
服务器
public static void Filelength() throws Exception {
int length = dis.readInt(); // this is the command only
String filename = makeString(length, dis);
File file = new File(path + filename);
if(!file.exists())
{
dos.writeBoolean(false);
return;
}
dos.writeBoolean(true);
long byt = file.length();
long block = file.length()/1024;
dos.writeLong(byt);
dos.writeLong(block);
// dos.close();
}
}
客户端
public static void Filelength() throws Exception {
dos.writeInt(2);
dos.writeChars("go");
Scanner sc = new Scanner(System.in);
System.out.println("Enter File Name: ");
String le = sc.nextLine();
dos.writeInt(le.length());
dos.writeChars(le);
if(dis.readBoolean() == false){
System.out.println("The file not Exist");
return;
}
long byt = dis.readLong();
long block = dis.readLong();
System.out.println("number of bytes : " + byt);
System.out.println("number of blocks : " + block);
}
}
结果总是显示为
number of bytes : 4705984196517036032
number of blocks : 4660946338375008256