簡體   English   中英

執行客戶端向服務器發送文件的程序時出錯

[英]Errors While executing a programs in which client is sending file to server

我不知道這段代碼哪里出錯了,這些是客戶端和服務器程序源代碼。 首先我執行了服務器程序,它運行良好,但是當我執行客戶端程序時,我在雙方(服務器和客戶端)上都出現錯誤。 新圖像(testfile.jpg)也不完整。 我將 cat.jpg(大小 1 MB)文件放在桌面上。

文件客戶端.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");
}

}

文件服務器

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)

您需要在客戶端代碼的 main 方法中為文件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 代碼的位置,相對路徑會很棘手。 因此,如果您不確定,請使用絕對路徑。

一旦你解決了這個問題,你的客戶端將能夠將文件寫入套接字,此時你的服務器也將停止抱怨。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM