好的,我一直在寻找这个地方,我发现这是一个常见的问题,有些人找到了解决方案,其他人没有,不幸的是我找到的解决方案没有用,所以我我想知道这里发生了什么。
我的程序的目的是手动发送或接收文件或"同步"服务器将文件夹与计算机上的文件夹同步。
以下是我的代码:
ClientConnection.java
public class ClientConnection implements Runnable {
private Socket clientSocket;
private BufferedReader in = null;
static List<String> reqFile = new ArrayList<String>();
static List<String> fname = new ArrayList<String>();
static List<String> results = new ArrayList<String>();
String folderPath="C:/Users/shami_000/Desktop/ServerFiles";
public ClientConnection(Socket client) {
this.clientSocket = client;
}
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String clientSelection;
clientSelection = in.readLine();
System.out.println(clientSelection);
while ((clientSelection) != null) {
switch (clientSelection) {
case "1":
receiveFile();
break;
case "2":
String outGoingFileName;
while ((outGoingFileName = in.readLine()) != null) {
sendFile(outGoingFileName);
}
break;
case "3":
sync();
break;
default:
System.out.println("Incorrect command received.");
break;
}
//in.close();
break;
}
} catch (IOException ex) {
Logger.getLogger(ClientConnection.class.getName()).log(
Level.SEVERE, null, ex);
}
}
public void receiveFile() {
try {
int bytesRead;
DataInputStream clientData = new DataInputStream(
clientSocket.getInputStream());
String fileName = clientData.readUTF();
OutputStream output = new FileOutputStream((fileName));
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0
&& (bytesRead = clientData.read(buffer, 0,
(int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
output.close();
clientData.close();
System.out.println("File " + fileName + " received from client.");
} catch (IOException ex) {
System.err.println("Client error. Connection closed.");
}
}
public void sync() {
int bytesRead;
try {
DataInputStream clientData = new DataInputStream(
clientSocket.getInputStream());
String fileNames = clientData.readUTF();
//System.out.println(fileNames);
File folder = new File("C:/Users/shami_000/Desktop/ServerFiles");
File[] listOfFiles = folder.listFiles();
String list = "";
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
results.add(listOfFiles[i].getName());
}
}
StringTokenizer st = new StringTokenizer(fileNames, ",");
while (st.hasMoreTokens()) {
String name = (String) st.nextElement();
fname.add(name);
}
getReqFiles();
for (int i = 0; i < reqFile.size(); i++) {
list = list + reqFile.get(i) + ",";
}
System.out.println(list);
OutputStream os = clientSocket.getOutputStream();
// Sending file name to the client
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(list);
dos.flush();
//dos.close();
os.close();
clientData.close();
reqFile.clear();
fname.clear();
results.clear();
} catch (Exception e) {
System.out.println(e);
}
}
public static void getReqFiles(){
boolean there = false;
String file="";
if(results.isEmpty())
reqFile=fname;
else{
for(int i=0;i<fname.size();i++){
file=fname.get(i);
if(!(results.contains(file)))
reqFile.add(file);
}
}
}
}
FileClient.java
public class FileClient {
private static Socket sock;
private static String fileName;
private static BufferedReader stdin;
private static PrintStream os;
private static String path = "C:\\Users\\shami_000\\Desktop\\ClientFiles\\";
public static void main(String[] args) throws IOException {
try {
sock = new Socket("localhost", 4444);
stdin = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
System.err
.println("Cannot connect to the server, try again later.");
System.exit(1);
}
os = new PrintStream(sock.getOutputStream());
try {
switch (Integer.parseInt(selectAction())) {
case 1:
os.println("1");
//sendFile();
break;
case 2:
os.println("2");
System.err.print("Enter file name: ");
fileName = stdin.readLine();
os.println(fileName);
receiveFile(fileName);
break;
case 3:
os.println("3");
sync();
break;
}
} catch (Exception e) {
System.err.println("not valid input");
}
sock.close();
}
public static String selectAction() throws IOException {
System.out.println("1. Send file.");
System.out.println("2. Recieve file.");
System.out.println("3. Sync files");
System.out.print("\nMake selection: ");
return stdin.readLine();
}
public static void sendFile(String fileName) {
try {
System.out.println(fileName);
File myFile = new File(fileName);
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
// bis.read(mybytearray, 0, mybytearray.length);
DataInputStream dis = new DataInputStream(bis);
dis.readFully(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
// Sending file name and file size to the server
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(myFile.getName());
dos.writeLong(mybytearray.length);
dos.write(mybytearray, 0, mybytearray.length);
dos.flush();
System.out.println("File " + fileName + " sent to Server.");
} catch (Exception e) {
System.err.println("File does not exist! "+e);
}
}
public static void sync() {
try {
String list = "";
File folder = new File("C:/Users/shami_000/Desktop/ClientFiles");
File[] listOfFiles = folder.listFiles();
List<String> results = new ArrayList<String>();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
results.add(listOfFiles[i].getName());
}
}
for (int i = 0; i < results.size(); i++) {
list = list + results.get(i) + ",";
}
OutputStream oss = sock.getOutputStream();
// Sending file name and file size to the server
DataOutputStream dos = new DataOutputStream(oss);
dos.writeUTF(list);
InputStream in = sock.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileNames = clientData.readUTF();
System.out.println(fileNames);
List<String> fname = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(fileNames, ",");
while (st.hasMoreTokens()) {
String name = (String) st.nextElement();
fname.add(name);
}
for(int i=0;i<fname.size();i++){
os.println("1");
sendFile(path+fname.get(i));
}
} catch (Exception e) {
}
}
}
手动选项DO工作(就像我选择选项1或2时,它会传输文件)但是当我尝试同步时,服务器成功获取一个列表(一个包含逗号分隔的文件名的长字符串),将文件与其自己的文件夹进行比较,并将所有丢失文件的列表返回给客户端。客户端确实成功接收到新列表,但是当我尝试使用sendfile函数上传文件时,我收到错误= / 我有一种感觉,可能有一些插座问题,但我似乎无法确定那将是什么。
这一点就在FileClient中:
for(int i=0;i<fname.size();i++){
os.println("1");
sendFile(path+fname.get(i));
}
是主要问题,服务器应该打印输出&#34; 1&#34;在控制台中,但它没有这样做,这意味着服务器还没有准备好接收文件(它从不执行receiveFile函数)。