在毕老师讲的,用TCP向服务端上传文件中,他提到过一般我们传文件,先是将文件名传给服务器。我想过现在客户端创建一个PrintWriter对象将文件名发送过去,再在服务器端创建一个BufferedReader对象,读一行文本,获得文件名。
客户端实现的代码如下:- package uploadFile;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.Scanner;
- public class TransClient {
- public static void main(String[] args) {
- try {
- Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 10010);
- OutputStream out = socket.getOutputStream();
-
- Scanner scanner = new Scanner(System.in);
- File file = null;
- while (true) {
- System.out.println("Please put in the name of file:");
- String fileName = scanner.nextLine();
- file = new File(fileName);
-
- if (!file.exists()) {
- System.out.println(fileName + " isn't exists");
- }else{
- break;
- }
- }
-
- <font color="#ff0000"> PrintWriter pw = new PrintWriter(out, true);
- pw.println(file.getName()); //发送文件名</font>
-
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
- BufferedOutputStream bos = new BufferedOutputStream(out);
- byte[] buf = new byte[1024];
- int len;
- while (-1 != (len = bis.read(buf))) {
- bos.write(buf, 0, len);
- bos.flush();//注意这里一定要刷新,要不然数据没有出缓冲区。更谈不上发送了
- }
-
- socket.shutdownOutput();
-
- BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- System.out.println(br.readLine());
-
- bis.close();
- socket.close();//是不是与socket相关的流,都被关闭了
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
复制代码 服务器端实现的代码:- package uploadFile;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class TransServer {
- public static void main(String[] args) {
- try {
- ServerSocket ss = new ServerSocket(10010);
- Socket s = ss.accept();
- System.out.println(s.getInetAddress().getHostAddress() + "...contected");
- <font color="#ff0000"> BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- File file = new File(br.readLine()); //获得文件名
- </font>
- BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\" + file.getName()));
- byte[] buf = new byte[1024];
- int len;
- while (-1 != (len = bis.read(buf))) {
- bos.write(buf, 0, len);
- }
-
- bos.flush();
-
- PrintWriter pw = new PrintWriter(s.getOutputStream(), true);//注意一定要加true,(如果为 true,则 println、printf 或 format 方法将刷新输出缓冲区)要不然就自己刷新缓冲区
- pw.println("上传成功");//这里就不要用write(),因为它不会自动刷新的
-
- s.shutdownOutput();
-
- // s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
复制代码 求高手,给个专业的解法!!!
|