我不知道我在此代码中出错的地方,这些是客户端和服务器程序源代码。首先,我被执行的服务器程序可以很好,但是当我执行客户端程序时,我在侧面(服务器和客户端(上都有错误。新图像(testfile.jpg(也不完整。我将cat.jpg(尺寸1 MB(放在桌面上。
fileclient.java
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
public class FileClient {
private Socket s;
public FileClient(String host, int port, String file) {
try {
s = new Socket(host, port);
sendFile(file);
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendFile(String file) throws IOException {
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
while (fis.read(buffer) > 0) {
dos.write(buffer);
}
fis.close();
dos.close();
}
public static void main(String[] args) {
FileClient fc = new FileClient("localhost", 1988, "cat.jpg");
}
}
fileserver.java
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class FileServer extends Thread {
private ServerSocket ss;
public FileServer(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
Socket clientSock = ss.accept();
saveFile(clientSock);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void saveFile(Socket clientSock) throws IOException {
DataInputStream dis = new DataInputStream(clientSock.getInputStream());
FileOutputStream fos = new FileOutputStream("testfile.jpg");
byte[] buffer = new byte[4096];
int filesize = 15123; // Send file size in separate msg
int read = 0;
int totalRead = 0;
int remaining = filesize;
while((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
System.out.println("read " + totalRead + " bytes.");
fos.write(buffer, 0, read);
}
fos.close();
dis.close();
}
public static void main(String[] args) {
FileServer fs = new FileServer(1988);
fs.start();
}
}
客户端错误
C:UsersvishalDesktop>java FileClient
java.io.FileNotFoundException: cat.jpg (The system cannot find the file
specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at FileClient.sendFile(FileClient.java:23)
at FileClient.<init>(FileClient.java:15)
at FileClient.main(FileClient.java:35)
服务器端错误
C:UsersvishalDesktop>java FileServer
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.read(Unknown Source)
at FileServer.saveFile(FileServer.java:39)
at FileServer.run(FileServer.java:23)
您需要在客户端代码的主要方法中为文件cat.jpg
提供正确的路径。
,例如
替换
中的文件名 FileClient fc = new FileClient("localhost", 1988, "cat.jpg");
带有的绝对路径,例如
FileClient fc = new FileClient("localhost", 1988, "c:/user/vishal/desktopcat.jpg");
或诸如
之类的相对路径 FileClient fc = new FileClient("localhost", 1988, "../vishal/desktopcat.jpg");
请注意,根据您执行Java代码的位置,相对路径将很棘手。因此,如果不确定,请使用绝对路径。
解决了此问题后,您的客户端将能够将文件写入套接字,此时您的服务器也将停止抱怨。