Java:从二进制文件读取,通过套接字发送字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10618441/
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 01:51:26  来源:igfitidea点击:

Java: read from binary file, send bytes over socket

javasocketsbinary-data

提问by fweigl

This should be easy, but I can't get my head around it right now. I wanna send some bytes over a socket, like

这应该很容易,但我现在无法理解。我想通过套接字发送一些字节,比如

Socket s = new Socket("localhost", TCP_SERVER_PORT);
DataInputStream is = new DataInputStream(new BufferedInputStream(s.getInputStream()));

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));

for (int j=0; j<40; j++) {
  dos.writeByte(0);
}

That works, but now I dont want to writeByte to the Outputstream, but read from a binary file, then write it out. I know(?) I need a FileInputStream to read from, I just can't figure out hot to construct the whole thing.

那行得通,但现在我不想将字节写入输出流,而是从二进制文件中读取,然后将其写出。我知道(?)我需要一个 FileInputStream 来读取,我只是想不出构建整个事情的热度。

Can someone help me out?

有人可以帮我吗?

回答by sarcan

public void transfer(final File f, final String host, final int port) throws IOException {
    final Socket socket = new Socket(host, port);
    final BufferedOutputStream outStream = new BufferedOutputStream(socket.getOutputStream());
    final BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(f));
    final byte[] buffer = new byte[4096];
    for (int read = inStream.read(buffer); read >= 0; read = inStream.read(buffer))
        outStream.write(buffer, 0, read);
    inStream.close();
    outStream.close();
}

This would be the naive approach without proper exception handling - in a real-world setting you'd have to make sure to close the streams if an error occurs.

如果没有适当的异常处理,这将是一种幼稚的方法 - 在现实世界的设置中,如果发生错误,您必须确保关闭流。

You might want to check out the Channel classes as well as an alternative to streams. FileChannel instances, for example, provide the transferTo(...) method that may be a lot more efficient.

您可能想查看 Channel 类以及流的替代方法。例如,FileChannel 实例提供了可能更高效的 transferTo(...) 方法。

回答by arthur

        Socket s = new Socket("localhost", TCP_SERVER_PORT);

        String fileName = "....";

create a FileInputStream using a fileName

使用文件名创建 FileInputStream

    FileInputStream fis = new FileInputStream(fileName);

create a FileInputStream File Object

创建一个 FileInputStream 文件对象

        FileInputStream fis = new FileInputStream(new File(fileName));

to read from the file

从文件中读取

    DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(
        s.getOutputStream()));

reading from it byte after byte

从它一个字节一个字节地读取

    int element;
    while((element = fis.read()) !=1)
    {
        dos.write(element);
    }

or reading from it buffer wise

或从中读取缓冲区明智

byte[] byteBuffer = new byte[1024]; // buffer

    while(fis.read(byteBuffer)!= -1)
    {
        dos.write(byteBuffer);
    }

    dos.close();
    fis.close();

回答by ratchet freak

read a byte from the input and write the same byte to the output

从输入读取一个字节并将相同的字节写入输出

or with a byte buffer it like this:

或使用字节缓冲区,如下所示:

inputStream fis=new fileInputStream(file);
byte[] buff = new byte[1024];
int read;
while((read=fis.read(buff))>=0){
    dos.write(buff,0,read);
}

note that you don't need to use the DataStreams for this

请注意,您不需要为此使用 DataStreams