简体   繁体   English

如何将文件从客户端发送到服务器到另一个客户端?

[英]How to send file from client to server to another client?

what I want ask is could I do something with file? 我想问的是我可以对文件做些什么吗? which Stream is file send by ?Should file change to another data? 文件发送哪个流?文件应更改为其他数据吗?

You can read the file using an InputStream and write its data to the OutputStream of a Socket . 您可以使用InputStream读取文件并将其数据写入SocketOutputStream

This may look something like this: 可能看起来像这样:

OutputStream out = null;
FileInputStream in = null;

try {
    // Input from file
    String pathname = "path/to/file.dat";
    File file = new File(pathname);
    in = new FileInputStream(file);

    // Output to socket
    String host = "10.0.1.8";
    int port = 6077;
    Socket socket = new Socket(host, port);
    socket.connect(endpoint); // TODO: define endpoint
    out = socket.getOutputStream();

    // Transfer
    while (in.available() > 0) {
        out.write(in.read());
    }

} catch (Exception e) {
    // TODO: handle exception

} finally {
    if (out != null)
        out.close();
    if (in != null)
        in.close();
}

PS: I'm not sure if this actually works. PS:我不确定这是否真的有效。 It's meant to get you started... 旨在帮助您入门...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM