9月14日

来源:互联网 发布:安利云服务软件下载 编辑:程序博客网 时间:2024/04/28 04:49

补充:

上传文件

客户端:
1.建立socket客户端,连接服务,要有目标ip和端口
2.读取本地文件的内容
3.输出到服务器端:通过socket的输出流
4.传输结束:socket的shutdownOutput()
5.接受服务端传过来的上传成功提示:通过socket的输入流
6.关闭资源。

服务器端

1.建立ServerSocket服务,监听一个端口
2.通过accept方法产生一个socket对象,与客户端建立通道。
3.通过socket的输入流读取客户端穿过来信息。
4.将获取到的信息保存到文件。
5.向客户端发出上传成功提示。
6.关闭资源

 

 

 

今天学习了URL类的使用

package com.hbsi.tcp;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class TextNet3 {
 public static void main(String[] args) throws IOException{
  URL url = new URL("http://sina.com/");
  InputStreamReader isr = new InputStreamReader(url.openStream());
  BufferedReader in = new BufferedReader(isr);
  String inputLine;
  FileOutputStream fos = new FileOutputStream("d:\\mydownloadhtml.html");
  while ((inputLine = in.readLine())!=null){
   fos.write(inputLine.getBytes());
   
  }
  in.close();
 }
}

 

 

原创粉丝点击