C/S通讯,互传文件
上传或者下载文件客户端:
package com.et.FileClient;import java.io.*;import java.util.*;import java.net.*;public class FileUpServer {public static void main(String[] args) throws Exception {ServerSocket ss = new ServerSocket(5554);//创建网络服务器接受客户请求while (true){Socket s = ss.accept();System.out.println("accept");//创建网络输出流并提供数据包装器InputStream is=s.getInputStream();if (is==null){throw new RuntimeException("stream is null");}File myTempFile = new File("image.jpg");FileOutputStream fos = new FileOutputStream(myTempFile);byte buf[]=new byte[1024];while (true){int num = is.read(buf);if (num<=0)break;fos.write(buf, 0, num);}is.close();s.close();}}}