gpt4 book ai didi

java - 将文件从客户端复制到服务器

转载 作者:行者123 更新时间:2023-12-01 13:18:35 25 4
gpt4 key购买 nike

我正在尝试用 Java 将文件从客户端复制到服务器,如下所示:

客户:

public class Client {

public static void main(String[] args) throws Exception {
String fileName = "D:\\6282.mp3";

try {

} catch (Exception e) {
Scanner scanner = new Scanner(System.in);
String file_name = fileName;

File file = new File(file_name);
Socket socket = new Socket("localhost", 3332);
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

oos.writeObject(file.getName());

FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[Server.BUFFER_SIZE];
Integer bytesRead = 0;

while ((bytesRead = fis.read(buffer)) > 0) {
oos.writeObject(bytesRead);
oos.writeObject(Arrays.copyOf(buffer, buffer.length));
}

oos.close();
ois.close();
System.exit(0);
}

}

}

服务器:

public class Server extends Thread {

public static final int PORT = 3332;
public static final int BUFFER_SIZE = 626;

@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);

while (true) {
Socket s = serverSocket.accept();
saveFile(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}

private void saveFile(Socket socket) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
FileOutputStream fos = null;
byte[] buffer = new byte[BUFFER_SIZE];

// 1. Read file name.
Object o = ois.readObject();

if (o instanceof String) {
fos = new FileOutputStream(o.toString());
} else {
throwException("Something is wrong");
}

// 2. Read file to the end.
Integer bytesRead = 0;

do {
o = ois.readObject();

if (!(o instanceof Integer)) {
throwException("Something is wrong");
}

bytesRead = (Integer) o;

o = ois.readObject();

if (!(o instanceof byte[])) {
throwException("Something is wrong");
}

buffer = (byte[]) o;

// 3. Write data to output file.
fos.write(buffer, 0, bytesRead);

} while (bytesRead == BUFFER_SIZE);

System.out.println("File transfer success");

fos.close();

ois.close();
oos.close();
}

public static void throwException(String message) throws Exception {
throw new Exception(message);
}

public static void main(String[] args) {
new Server().start();
}
}

当我运行时,我得到:

run:
BUILD SUCCESSFUL (total time: 0 seconds)

但是什么也没发生。这是我第一次使用客户端-服务器,我不确定我做错了什么。

请帮忙。谢谢。

最佳答案

您的代码中的一些问题是:

对于客户端来说,你已经将整个代码写在catch block 中,除非发生异常,否则该代码不会起作用。

您正尝试在此处传递文件名而不是文件本身。

oos.writeObject(file.getName());

您需要运行服务器,然后运行客户端。这是一个示例工作代码:

客户:

public class Client {

public static void main(String[] args) throws Exception {
String fileName = "C:\\2048.jpg";

try {
File file = new File(fileName);
Socket socket = new Socket("localhost", 3332);
byte[] mybytearray = new byte[(int) file.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = socket.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
os.close();
} catch (Exception e) {
}

}
}

服务器:

public class Server extends Thread {

public static final int PORT = 3332;
public static final int BUFFER_SIZE = 626;

@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
while (true) {
Socket s = serverSocket.accept();
saveFile(s);
}
} catch (Exception e) {
}
}

private void saveFile(Socket socket) throws Exception {
InputStream ois = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("C:\\2049.jpg");;

byte[] mybytearray = new byte[1024];
System.out.println("Reading file from server...");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead;
while ((bytesRead = ois.read(mybytearray)) != -1) {
bos.write(mybytearray);
}

bos.close();
System.out.println("Writing file complete...");

}

public static void main(String[] args) {
new Server().start();
}
}

关于java - 将文件从客户端复制到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22245846/

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