Sending file through ObjectOutputStream and then saving it in Java?

前端 未结 2 593
渐次进展
渐次进展 2021-01-06 07:35

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)

2条回答
  •  情深已故
    2021-01-06 07:56

    A File object represents the path to that file, not its actual content. What you should do is read the bytes from that file and send those over your 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);
    

提交回复
热议问题