错误执行客户端将文件发送到服务器

时间:2018-02-12 17:53:52

标签: java serversocket

我不知道在这段代码中我出错了,这些是客户端和服务器程序的源代码。 首先,我执行的服务器程序很好,但是当我执行客户端程序时,我在侧面(服务器和客户端)都有错误。 新图像(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:\Users\vishal\Desktop>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:\Users\vishal\Desktop>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)

1 个答案:

答案 0 :(得分:0)

您需要在客户端代码的main方法中为文件cat.jpg提供正确的路径。

E.g。

替换

中的文件名

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代码的位置,相对路径会很棘手。因此,如果您不确定,请使用绝对路径。

解决此问题后,您的客户端就可以将文件写入套接字,此时您的服务器也会停止抱怨。