本帖最后由 毕梦婷 于 2014-6-18 20:02 编辑
我写了一个客户端和一个服务端,但我怎么测试啊- class Client {
- public static void main(String[] args) throws IOException, IOException {
- System.out.println("客户端启动.....");
- Socket s = new Socket("192.168.1.253", 10004);
-
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入传输文件的路径:");
- String str = sc.next();
-
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str));
- BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
- int len = 0;
- byte[] buf = new byte[1024];
- while ((len = bis.read(buf)) != -1) {
- bos.write(buf, 0, len);
- bos.flush();
- }
- s.shutdownOutput();
- BufferedInputStream bufIs = new BufferedInputStream(s.getInputStream());
- byte[] bufIn = new byte[1024];
- int lenIn = bufIs.read(bufIn);
- System.out.println(new String(bufIn, 0, lenIn));
- bis.close();
- s.close();
- }
- }
- class Server {
- public static void main(String[] args) throws IOException {
- System.out.println("服务器端启动......");
-
- //创建服务端socket对象。
- ServerSocket ss = new ServerSocket(10004);
-
- //获取客户端对象。
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
-
- //创建一个file对象来存储传输内容
- File dir = new File("src\\com\\itheima");
- //判断是否存在,不存在的话就新建一个
- if (!dir.exists()) {
- dir.mkdirs();
- }
- BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dir, ip)));
- byte[] buf = new byte[1024];
- int len = 0;
- while ((len = bis.read(buf)) != -1) {
- bos.write(buf);
- bos.flush();
- }
- BufferedOutputStream bufos = new BufferedOutputStream(s.getOutputStream());
- bufos.write("上传成功".getBytes());
- bufos.flush();
- bos.close();
- s.close();
- ss.close();
- }
- }
复制代码
|