将字符串从服务器发送到客户端 - 将其保存为文本文件
Send String from server to client - save it as textfile
我知道如何将文件从服务器发送到客户端,但如何将文本字符串发送到客户端,客户端将字符串保存为文件?我应该使用 PrintWriter
来解决这个问题吗?
这是从服务器向客户端发送文件的代码:
Sending files through sockets
我想做的是(而不是发送文件),从服务器发送一个String
让客户端接收并保存为文件。
public class SocketFileExample {
static void server() throws IOException {
ServerSocket ss = new ServerSocket(3434);
Socket socket = ss.accept();
InputStream in = new FileInputStream("send.jpg");
OutputStream out = socket.getOutputStream();
copy(in, out);
out.close();
in.close();
}
static void client() throws IOException {
Socket socket = new Socket("localhost", 3434);
InputStream in = socket.getInputStream();
OutputStream out = new FileOutputStream("recv.jpg");
copy(in, out);
out.close();
in.close();
}
static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
public static void main(String[] args) throws IOException {
new Thread() {
public void run() {
try {
server();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
client();
}
}
在这种情况下,在服务器端使用 ObjectOutputStream
写入字符串并在客户端使用 ObjectInputStream
读取来自服务器的字符串..
因此您的 server() 方法将如下所示..
static void server() throws IOException {
ServerSocket ss = new ServerSocket(3434);
Socket socket = ss.accept();
OutputStream out = socket.getOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(out);
oout.writeObject("your string here");
oout.close();
}
和 client() 方法将如下所示..
static void client() throws IOException, ClassNotFoundException {
Socket socket = new Socket("localhost", 3434);
InputStream in = socket.getInputStream();
ObjectInputStream oin = new ObjectInputStream(in);
String stringFromServer = (String) oin.readObject();
FileWriter writer = new FileWriter("received.txt");
writer.write(stringFromServer);
in.close();
writer.close();
}
也在main方法中抛出ClassNotFoundException
。
public static void main(String[] args) throws IOException, ClassNotFoundException {.......}
完成...
我知道如何将文件从服务器发送到客户端,但如何将文本字符串发送到客户端,客户端将字符串保存为文件?我应该使用 PrintWriter
来解决这个问题吗?
这是从服务器向客户端发送文件的代码: Sending files through sockets
我想做的是(而不是发送文件),从服务器发送一个String
让客户端接收并保存为文件。
public class SocketFileExample {
static void server() throws IOException {
ServerSocket ss = new ServerSocket(3434);
Socket socket = ss.accept();
InputStream in = new FileInputStream("send.jpg");
OutputStream out = socket.getOutputStream();
copy(in, out);
out.close();
in.close();
}
static void client() throws IOException {
Socket socket = new Socket("localhost", 3434);
InputStream in = socket.getInputStream();
OutputStream out = new FileOutputStream("recv.jpg");
copy(in, out);
out.close();
in.close();
}
static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
public static void main(String[] args) throws IOException {
new Thread() {
public void run() {
try {
server();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
client();
}
}
在这种情况下,在服务器端使用 ObjectOutputStream
写入字符串并在客户端使用 ObjectInputStream
读取来自服务器的字符串..
因此您的 server() 方法将如下所示..
static void server() throws IOException {
ServerSocket ss = new ServerSocket(3434);
Socket socket = ss.accept();
OutputStream out = socket.getOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(out);
oout.writeObject("your string here");
oout.close();
}
和 client() 方法将如下所示..
static void client() throws IOException, ClassNotFoundException {
Socket socket = new Socket("localhost", 3434);
InputStream in = socket.getInputStream();
ObjectInputStream oin = new ObjectInputStream(in);
String stringFromServer = (String) oin.readObject();
FileWriter writer = new FileWriter("received.txt");
writer.write(stringFromServer);
in.close();
writer.close();
}
也在main方法中抛出ClassNotFoundException
。
public static void main(String[] args) throws IOException, ClassNotFoundException {.......}
完成...