gpt4 book ai didi

Java - 如果要发送的文件不是普通文本文件,则通过套接字发送的文件为空

转载 作者:行者123 更新时间:2023-11-30 01:47:17 26 4
gpt4 key购买 nike

我创建了一个程序,它将向客户端发送文件。我已经在普通文本文件上进行了测试,它已完整发送给客户端,没有丢失。但是当我尝试发送一些 zip 文件时,输出文件是空的。我该怎么办?

服务器.java

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
Scanner scan = new Scanner(System.in);
String fileSend;
System.out.print("Type the path to the file to send >> ");
fileSend = scan.nextLine();
try {
serverSocket = new ServerSocket(5467);
} catch (Exception e) {
System.out.println("Could not bind to port 5467, Maybe address is already is use or you need to run as administrator");
scan.close();
return;
}
System.out.println("Listening on port 5467");
System.out.println("Waiting for the connection...");
while (true) {
File FileSend = null;
Socket socket = serverSocket.accept();
OutputStream out = socket.getOutputStream();
System.out.println("Accepted connection : " + socket);
//รับข้อมูลจาก Client เข้ามา
InputStream in = socket.getInputStream();
DataInputStream dataIn = new DataInputStream(in);
String login = dataIn.readUTF();
String password = dataIn.readUTF();
String result = "You credential is ";
if (login.equals("1c18b5cdef8f9b4c5d6b2ad087265e597d1d4639337b73a04a335103c00ec64b") && password.equals("1c18b5cdef8f9b4c5d6b2ad087265e597d1d4639337b73a04a335103c00ec64b13d0b73358bfa8978dfaaf180565bcfecd3dc0631cda525920865145fb3fa131")) {
result += "correct";
} else {
result += "incorrect";
}
DataOutputStream dataOut = new DataOutputStream(out);
dataOut.writeUTF(result);
FileSend = new File(fileSend);
ObjectOutputStream out1 = new ObjectOutputStream(socket.getOutputStream());
System.out.println("Sending file...");
out1.writeObject(FileSend);
//ส่งข้อมูลกลับไปยัง Client
}
}
}

客户端.java

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Client {
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, ClassNotFoundException {
Socket socket = null;
String PlainLogin;
String FileOut;
String PlainPassword;
Scanner scan = new Scanner(System.in);
System.out.print("What is the IP address of the server >> ");
String host = scan.nextLine();
try {
socket = new Socket(host, 5467); //หรือกำหนดเป็น 127.0.0.1
} catch (ConnectException | NullPointerException e) {
System.out.println("Connection Refused, Have you run the server first?");
scan.close();
return;
}

OutputStream out = socket.getOutputStream();
DataOutputStream dataOut = new DataOutputStream(out);
System.out.println("Connection Established");
System.out.println("Credential Required, Please login");
System.out.print("Type your username >> ");
PlainLogin = scan.next();
System.out.print("Type your password >> ");
PlainPassword = scan.next();
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hashInBytes = md.digest(PlainLogin.getBytes(StandardCharsets.UTF_8));

StringBuilder sb = new StringBuilder();
for (byte b : hashInBytes) {
sb.append(String.format("%02x", b));
}
String HashedLogin = sb.toString();

byte[] hashInBytesP = md.digest(PlainPassword.getBytes(StandardCharsets.UTF_8));

for (byte b : hashInBytesP) {
sb.append(String.format("%02x", b));
}
String HashedPassword = sb.toString();
dataOut.writeUTF(HashedLogin); //ลำดับในการส่งข้อมูล ต้องตรงกับลำดับในการรับข้อมูลทางฝั่ง Server
dataOut.writeUTF(HashedPassword);
InputStream in = socket.getInputStream();
DataInputStream dataIn = new DataInputStream(in);
String str = dataIn.readUTF();
if (str == "Your credential is incorrect") {
System.out.println(str);
socket.close();
scan.close();
return;
} else {
System.out.println(str);
// Writing the file to disk
// Instantiating a new output stream object
System.out.print("Type any file name you want >> ");
scan.nextLine();
FileOut = scan.nextLine();
File SaveFile = new File(FileOut);
ObjectInputStream in1 = new ObjectInputStream(socket.getInputStream());
File receivedFile = null;
receivedFile = (File) in1.readObject();
Scanner sc = new Scanner(receivedFile);
PrintWriter printer = new PrintWriter(SaveFile);
while(sc.hasNextLine()) {
String s = sc.nextLine();
printer.write(s);
}
socket.close();
dataIn.close();
printer.close();
sc.close();
scan.close();
System.out.println("Done");
}
}
}

我希望 zip 文件能够发送并且不会损坏,但该文件在输出时为空。

最佳答案

您正在使用对象流发送File对象。那不是你想的那样。 File 是表示文件系统路径名的对象。它不代表文件的内容。

所以你的代码实际做的是:

  • 发送哈希用户名和密码。
  • 在服务器端检查它们
  • 发送文件
  • 在服务器端,“打开”文件并复制其内容。

但是您正在服务器文件系统的上下文中打开文件,而不是客户端的文件系统,因此没有数据从客户端复制到服务器.

您还使用扫描仪复制(错误的)文件。一般来说,这对于非文本文件无法正常工作。无论其“类型”如何,复制数据的最安全方法是使用二进制流。

如果要发送文件内容,请不要使用ObjectInputStreamObjectOutputStream来发送路径名。相反,使用普通的 OutputStream::write(...)OutputStream::read(...) 以及循环来复制文件数据:

  • 客户端应从(客户端)本地文件读取字节并复制到套接字流。

  • 服务器端应从套接字读取字节并写入(服务器端)本地文件。

关于Java - 如果要发送的文件不是普通文本文件,则通过套接字发送的文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57488866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com