- import java.net.*;
- import java.io.*;
- class PicClient
- {
- public static void main(String[] args) throws Exception
- {
- Socket s = new Socket("100.97.97.68",10008);
- FileInputStream fis = new FileInputStream("c:\\1.jpg");
- InputStream in = s.getInputStream();
- //OutputStream out =s.getOutputStream();
- BufferedOutputStream out = new BufferedOutputStream(s.getOutputStream());
- byte[] buf = new byte[1024];
- int len = 0;
- while((len=fis.read(buf))!=-1)
- {
- out.write(buf,0,len);
- //这里为什么不需要flush
- }
-
- s.shutdownOutput();
-
- byte[] bufIn = new byte[1024];
- int lenIn = in.read(bufIn);
- System.out.println(new String(bufIn,0,lenIn));
- fis.close();
- s.close();
- }
- }
- class PicServer
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(10008);
- Socket s = ss.accept();
-
- FileOutputStream fos = new FileOutputStream("Server.jpg");
-
- InputStream in = s.getInputStream();
- // OutputStream out = s.getOutputStream();
- BufferedOutputStream out = new BufferedOutputStream(s.getOutputStream());
-
- byte[] buf = new byte[1024];
- int len = 0;
- while((len=in.read(buf))!=-1)
- {
- fos.write(buf,0,len);
- }
-
- out.write("上传成功".getBytes());
- out.flush();// ?这里为什么要刷一下
- fos.close();
- s.close();
- ss.close();
- }
- }
复制代码
代码在这里 |