随手做一个多线程的 CS架构的 文件传输Demo
试一下JDK5的多线程编程,附件是代码,下载后改一下后缀名为.tgz
测试环境
OSX 10.5.6
JDK6
JUnit4.5
参考
Java基于Socket文件传输示例
Java5 多线程实践
Test 图
Server 接口
客户端实现
客户端测试类/** * * @author rikugun */public class ClientTest { static Properties prop = new Properties(); static ServerImpl server; Client instance; public ClientTest() { } @BeforeClass public static void setUpClass() throws Exception { FileInputStream fis = new FileInputStream("conf.properties"); prop.load(fis); System.out.println("Load prop success!"); server = new ServerImpl(prop);// server.startup(); new Thread(server).start(); if (server.isRunning()) { System.out.println("Server is running..."); }else{ System.out.println("Server start failed!"); } } @AfterClass public static void tearDownClass() throws Exception { server.stop(); } @Before public void setUp() { instance = (Client) new ClientImpl(prop); } @After public void tearDown() { } /** * Test of getFiles method, of class Client. */ @Test public void testGetFiles() { System.out.println("getFiles"); String[] file_names = new String[]{"2.txt", "3.txt", "4.txt", "5.txt"}; boolean expResult = true; boolean result = instance.getFiles(file_names); assertEquals(expResult, result); } /** * Test of getFile method, of class Client. */ @Test public void testGetFile() { System.out.println("getFile"); String file_name = "1.txt"; boolean expResult = true; boolean result = instance.getFile(file_name); assertEquals(expResult, result); }}