[java] view plain copy
 print?
  1. /* 
  2. *给服务端发送文件,主要是IO流。 
  3. */  
  4. import java.io.*;  
  5. import java.net.*;  
  6. class  send2  
  7. {  
  8.     public static void main(String[] args) throws Exception  
  9.     {  
  10.         Socket s = new Socket("192.168.33.1",10005);//建立服务  
  11.         BufferedReader bufr = new BufferedReader(new FileReader("io.java"));//读取IO.JAVA文件  
  12.         PrintWriter pw = new PrintWriter(s.getOutputStream(),true);//将读到的写入服务端  
  13.         String line = null;  
  14.         while((line = bufr.readLine())!=null)  
  15.         {  
  16.             pw.println(line);  
  17.         }  
  18. //      pw.println("over");//标记结束位置  
  19.         s.shutdownOutput();  
  20.         BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));//读取服务端返回的数据  
  21.         String str = bufin.readLine();  
  22.         System.out.println(str);  
  23.         bufr.close();  
  24.         s.close();  
  25.     }  
  26. }  
  27. class  rece2  
  28. {  
  29.     public static void main(String[] args) throws Exception  
  30.     {  
  31.         ServerSocket ss = new ServerSocket(10005);//建立服务  
  32.         Socket s = ss.accept();//接收数据  
  33.         BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));//读取接收到的数据  
  34.         PrintWriter out = new PrintWriter(new FileWriter("io2.txt"),true);//写入到IO.TXT文本  
  35.         String line = null;  
  36.         while((line = bufin.readLine())!= null)//读一行写入一行  
  37.         {  
  38. //          if("over".equals(line))  
  39. //              break;  
  40.             out.println(line);  
  41.         }  
  42.         PrintWriter pw = new PrintWriter(s.getOutputStream(),true);  
  43.         pw.println("上传成功!");  
  44.         out.close();  
  45.         ss.close();  
  46.         s.close();  
  47.     }  
  48. }  

结果

Java给服务端发送文件


转载:http://blog.csdn.net/chaoyu168/article/details/49889007

相关文章:

  • 2021-12-09
  • 2021-10-25
  • 2021-11-12
  • 2022-12-23
  • 2022-12-23
  • 2021-10-20
  • 2021-08-31
  • 2022-12-23
猜你喜欢
  • 2022-02-08
  • 2021-07-26
  • 2022-12-23
  • 2021-11-30
  • 2021-12-27
  • 2021-11-15
  • 2022-12-23
相关资源
相似解决方案