[英]How to send file from client to server to another client?
我想问的是我可以对文件做些什么吗? 文件发送哪个流?文件应更改为其他数据吗?
您可以使用InputStream
读取文件并将其数据写入Socket
的OutputStream
。
可能看起来像这样:
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:我不确定这是否真的有效。 旨在帮助您入门...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.