How to send file from client to server to another client?
what I want ask is could I开发者_JS百科 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
.
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. It's meant to get you started...
精彩评论