我正在尝试创建交换文件的简单客户端和服务器,因此,我尝试使用writeUtf()
方法将文件名作为String
发送,但它根本不起作用
public class Client {
public static void main(String[] args) throws Exception {
@SuppressWarnings("resource")
Socket client = new Socket("localhost", 1241);
Path currentRelativePath = Paths.get("");
String currentPath = currentRelativePath.toAbsolutePath().toString();
DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
DataInputStream dataInFromServer = new DataInputStream(client.getInputStream());
Scanner keyboardInput=new Scanner(System.in);
System.out.println("Enter Operation Type(PUt, Get, Or Exit)");
String OperationType=keyboardInput.nextLine();
switch(OperationType){
case "PUT":
outToServer.writeChar('P');
System.out.println("File Or Directory name:");
String FileOrDirName=keyboardInput.nextLine();
//outToServer.writeUTF(FileOrDirName);
//outToServer.write((short)FileOrDirName.length());
outToServer.writeUTF(FileOrDirName);
File fileOrDir=new File(FileOrDirName);
if(fileOrDir.getParent()==currentPath || fileOrDir.getParent()==null){
if(fileOrDir.isDirectory()){
outToServer.writeInt(1);
FileOperations.zipDir(fileOrDir);
outToServer.write(FileOperations.readFileAsByteArray(FileOrDirName+".zip"));
File tmp=new File(FileOrDirName+".zip");
tmp.delete();
}
else {
outToServer.writeInt(0);
byte toSend[]=FileOperations.readFileAsByteArray(FileOrDirName);
outToServer.writeDouble(toSend.length);
for(int i=0; i<toSend.length; i++)
outToServer.writeByte(toSend[i]);
// outToServer.write(ToSend,0,ToSend.length);
//outToServer.write(FileOperations.readFileAsByteArray(FileOrDirName));
}
outToServer.flush();
outToServer.close();
keyboardInput.close();
client.close();
}
}
}
}
这是服务器:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.nio.file.Path;
import java.nio.file.Paths;
class TcpFileServerThread extends Thread{
private Socket client;
public TcpFileServerThread(Socket client) {
this.client = client;
}
public void run() {
try {
DataOutputStream outToClient = new DataOutputStream(client.getOutputStream());
DataInputStream dataInFromClient = new DataInputStream(client.getInputStream()) ;
Path currentRelativePath = Paths.get("");
String currentPath = currentRelativePath.toAbsolutePath().toString();
//File f;
//System.out.println(dataInFromClient.readInt());
/** client request PUT **/
switch(dataInFromClient.readChar()){
case 'P':
int lengthRecvd= (int)dataInFromClient.readDouble();
byte[] toRecv=new byte[lengthRecvd];
for(int i=0; i<lengthRecvd; i++){
toRecv[i]=dataInFromClient.readByte();
}
// dataInFromClient.read(file_or_dir_from_client);
//FileOrDirName="uu.txt";
String FileOrDirName=dataInFromClient.readUTF();
// int arrLen=dataInFromClient.readShort();
/** for(int i=0; i<arrLen; i++){
char o=dataInFromClient.readChar();
System.out.println(o);
}**/
System.out.println(FileOrDirName);
File f=new File(FileOrDirName);
if(f.getParent()==null || f.getParent()==currentPath){
FileOperations.writeFromByteArrayToFile(toRecv, FileOrDirName);
if(dataInFromClient.readInt()==1){
File zipFl=new File(FileOrDirName+".zip");
FileOperations.unzip(zipFl);
zipFl.delete();
}
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
我得到FileNotFoundException
,服务器中System.out.println(FileOrDirName)
的输出是空行,因为它是空的。