通过 ObjectOutputStream 发送文件然后将其保存在 Java 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11593725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:42:12  来源:igfitidea点击:

Sending file through ObjectOutputStream and then saving it in Java?

javafileobjectinputstreamoutputstream

提问by ZimZim

I have this simple Server/Client application. I'm trying to get the Server to send a file through an OutputStream (FileOutputStream, OutputStream, ObjectOutputStream, etc) and receive it at the client side before saving it into an actual file. The problem is, I've tried doing this but it keeps failing. Whenever I create the file and write the object I received from the server into it, I get a broken image (I just save it as a jpg, but that shouldn't matter). Here are the parts of the code which are most likely to be malfunctioning (all the seemingly un-declared objects you see here have already been declared beforehand):

我有这个简单的服务器/客户端应用程序。我试图让服务器通过 OutputStream(FileOutputStream、OutputStream、ObjectOutputStream 等)发送文件,并在将其保存到实际文件之前在客户端接收它。问题是,我试过这样做,但它一直失败。每当我创建文件并将我从服务器收到的对象写入其中时,我都会得到一个损坏的图像(我只是将它保存为 jpg,但这应该无关紧要)。以下是代码中最有可能出现故障的部分(您在此处看到的所有看似未声明的对象都已预先声明):

Server:

服务器:

                ObjectOutputStream outToClient = new ObjectOutputStream(
                        connSocket.getOutputStream());
                File imgFile = new File(dir + children[0]);
                outToClient.writeObject(imgFile);
                outToClient.flush();

Client:

客户:

ObjectInputStream inFromServer = new ObjectInputStream(
                clientSocket.getInputStream());
        ObjectOutputStream saveImage = new ObjectOutputStream(
                new FileOutputStream("D:/ServerMapCopy/gday.jpg"));
        saveImage.writeObject(inFromServer.readObject());

So, my problem would be that I can't get the object through the stream correctly without getting a broken file.

所以,我的问题是我无法在没有损坏文件的情况下正确地通过流获取对象。

回答by Jeffrey

A Fileobject represents the pathto that file, not its actual content. What you should do is read the bytes from that file and send those over your ObjectOutputStream.

一个File对象表示的路径到该文件,而不是它的实际内容。您应该做的是byte从该文件中读取s 并将它们发送到您的ObjectOutputStream.

File f = ...
ObjectOutputStream oos = ...

byte[] content = Files.readAllBytes(f.toPath);
oos.writeObject(content);



File f=...
ObjectInputStream ois = ...

byte[] content = (byte[]) ois.readObject();
Files.write(f.toPath(), content);

回答by sarcan

You are not actually transferring the file, but the File instance from Java. Think of your File object as a (server-)local handle to the file, but not its contents. For transferring the image, you'd actually have to read it on the server first.

您实际上不是在传输文件,而是从 Java 传输 File 实例。将您的 File 对象视为文件的(服务器)本地句柄,而不是其内容。要传输图像,您实际上必须先在服务器上读取它。

However, if you're just going to save the bytes on the client anyway, you can forget about the ObjectOutputStream to begin with. You can just transfer the bytes stored in the File. Take a look at the FileChannel class and its transferTo and transferFrom methods as a start.

但是,如果您只是打算在客户端上保存字节,则可以从一开始就忘记 ObjectOutputStream。您可以只传输存储在文件中的字节。首先看一下 FileChannel 类及其 transferTo 和 transferFrom 方法。