沈汉学

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.实现文件的上传

客户端:

 1 //客户端
 2 public class TCPClient {
 3     public static void main(String[] args) throws UnknownHostException, IOException {
 4         //创建客户端对象,明确要连接的服务器端ip和端口号
 5         Socket socket = new Socket("192.168.1.171",9000);
 6         //创建字节输入流,明确数据源
 7         FileInputStream fis = new FileInputStream("D:\\io0429\\a.png");
 8         //获取字节输出流,目的地是服务器
 9         OutputStream out = socket.getOutputStream();
10         //开始复制
11         byte[] bytes = new byte[1024];
12         int len = 0;
13         while((len = fis.read(bytes)) != -1){
14             out.write(bytes,0,len);
15         }
16         //告知服务器结束
17         socket.shutdownOutput();
18         //接受服务器端的回复
19         InputStream in = socket.getInputStream();
20         len = in.read(bytes);
21         System.out.println(new String(bytes,0,len));
22         //释放资源
23         socket.close();
24         fis.close();
25     }
26 }

服务器端:

 1 //服务器端
 2 public class TCPServer {
 3     public static void main(String[] args) throws IOException {
 4         //创建服务器端对象,明确端口号
 5         ServerSocket server = new ServerSocket(9000);
 6         //创建与客户端的链接,并获取与之连接的客户端对象
 7         Socket socket = server.accept();
 8         //明确目的地
 9         File file = new File("D:\\upload");
10         //如果文件夹不存在
11         if(!file.exists()){
12             //则创建文件夹
13             file.mkdir();
14         }
15         String filename = "oralce"+System.currentTimeMillis()+new Random().nextInt(999999)+".png";
16         FileOutputStream fos = new FileOutputStream(file+File.separator+filename);
17         //明确数据源
18         InputStream in = socket.getInputStream();
19         byte[] bytes = new byte[1024];
20         //开始复制
21         int len = 0;
22         while((len = in.read(bytes)) != -1){
23             fos.write(bytes,0,len);
24         }
25         
26         //回复客户端
27         //获取目的地是客户端的字节输出流
28         OutputStream out = socket.getOutputStream();
29         out.write("上传成功".getBytes());
30         //释放资源
31         server.close();
32         fos.close();
33     }
34 }

 

2.循环接收文件

服务器端代码

 1 public class Upload implements Runnable {
 2     private Socket socket;
 3     public Upload(){}
 4     public Upload(Socket socket){
 5         this.socket = socket;
 6     }
 7     public void run() {
 8         try {
 9             //明确目的地
10             File file = new File("D:\\upload");
11             //如果文件夹不存在
12             if(!file.exists()){
13                 //则创建文件夹
14                 file.mkdir();
15             }
16             String filename = "oralce"+System.currentTimeMillis()+new Random().nextInt(999999)+".png";
17             FileOutputStream fos = new FileOutputStream(file+File.separator+filename);
18             //明确数据源
19             InputStream in = socket.getInputStream();
20             byte[] bytes = new byte[1024];
21             //开始复制
22             int len = 0;
23             while((len = in.read(bytes)) != -1){
24                 fos.write(bytes,0,len);
25             }
26             
27             //回复客户端
28             //获取目的地是客户端的字节输出流
29             OutputStream out = socket.getOutputStream();
30             out.write("上传成功".getBytes());
31         } catch (IOException e) {
32             // TODO Auto-generated catch block
33             e.printStackTrace();
34         }
35     }
36 }

测试端

 1 public class demo01 {
 2     public static void main(String[] args) throws IOException {
 3         //创建服务器对象
 4         ServerSocket server = new ServerSocket(8000);
 5         while(true){
 6             //创建客户端链接,并获取与之连接 的客户端对象
 7             Socket socket = server.accept();
 8             //创建线程任务对象
 9             Upload u = new Upload(socket);
10             Thread t = new Thread(u);
11             t.start();
12         }
13     }
14     
15 }

客户端

 1 //客户端
 2 public class TCPClient {
 3     public static void main(String[] args) throws UnknownHostException, IOException {
 4         //创建客户端对象,明确要连接的服务器端ip和端口号
 5         Socket socket = new Socket("192.168.1.171",8000);
 6         //创建字节输入流,明确数据源
 7         FileInputStream fis = new FileInputStream("D:\\io0429\\a.png");
 8         //获取字节输出流,目的地是服务器
 9         OutputStream out = socket.getOutputStream();
10         //开始复制
11         byte[] bytes = new byte[1024];
12         int len = 0;
13         while((len = fis.read(bytes)) != -1){
14             out.write(bytes,0,len);
15         }
16         //告知服务器结束
17         socket.shutdownOutput();
18         //接受服务器端的回复
19         InputStream in = socket.getInputStream();
20         len = in.read(bytes);
21         System.out.println(new String(bytes,0,len));
22         //释放资源
23         socket.close();
24         fis.close();
25     }
26 }

 

posted on 2021-07-31 12:03  沈汉学  阅读(66)  评论(0编辑  收藏  举报