- import java.io.*;
- import java.net.*;
- //上传图片 1.建立服务 2.读取图片 3.写入网络输出流 4 读取服务端反馈信息 5.关闭资源
- class Kehu
- {
- public static void main(String[] args) throws Exception
- {
- Socket s = new Socket("lanlan-PC",10006);
-
- BufferedReader bffr = new BufferedReader(new InputStreamReader(new FileInputStream("1.jpg")));
- PrintWriter out = new PrintWriter(s.getOutputStream(),true);
- String line = null;
- while((line=bffr.readLine())!=null)
- {
- out.println(line);
- }
- s.shutdownOutput(); //关闭客户端网络输出流 相当于加了结束标记-1;
- BufferedReader bfin = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String XT = bfin.readLine(); //read()阻塞 等服务端反馈
- System.out.println("XT: "+XT);
- bffr.close();
- s.close();
- }
- }
- // 服务端: 1. 建立服务 2.获取客户端 3 读取网络输入流 4. 写入文件中 5 反馈信息“上传成功” 6.关闭资源
- class Fuwu
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(10006);
- Socket s = ss.accept();
- String IP = s.getInetAddress().getHostAddress();
- System.out.println(IP+".....已连接");
- BufferedReader bfin = new BufferedReader(new InputStreamReader(s.getInputStream()));
- PrintWriter out = new PrintWriter(new FileOutputStream("2.bmp"),true);
-
- String line = null ;
- while((line=bfin.readLine())!=null)//read()阻塞 等客户端传输数据
- {
- out.println(line);
-
- }
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- pw.println("服务器已接收,上传成功 谢谢");
- out.close();
- s.close();
- ss.close();
- }
- }
复制代码 我运行了下 发现图片字节变多了,无法打开了 - - 什么原因啊 难道是println()方法加入了 \r\n吗?:)
|
|