Java 基于tcp协议实现文件上传
作者:Hateyes 时间:2022-04-14 04:01:29
服务端
package lesson02;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务端接收文件
*/
public class TcpServerDemo2 {
public static void main(String[] args) throws IOException {
//1、创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2、监听客户端的连接
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
//3、获取输入流
InputStream is = socket.getInputStream();
//4、文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
fos.write(buffer, 0, len);
}
//通知客户端我接收完毕了
OutputStream os = socket.getOutputStream();
os.write("我接收完毕了,你可以断开了".getBytes());
//5、关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
客户端
package lesson02;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
/**
* 客户端上传文件
*/
public class TcpClientDemo2 {
public static void main(String[] args) throws Exception {
//1、创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 90000);
//2、创建一个输出流
OutputStream os = socket.getOutputStream();
//3、读取文件
FileInputStream fis = new FileInputStream(new File("toux.jpg"));
//4、写出文件
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1){
os.write(buffer, 0, len);
}
//通知服务器,我已经结束了
socket.shutdownOutput(); //我已经传输完了
//确定服务器接收完毕,才能够断开连接
InputStream inputStream = socket.getInputStream();
//Sting byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2 = inputStream.read(buffer2)) != -1){
baos.write(buffer2, 0, len2);
}
System.out.println(baos.toString());
//5、关闭资源
fis.close();
os.close();
socket.close();
}
}
来源:https://www.cnblogs.com/ming2/p/14006811.html
标签:java,tcp,文件上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用jenkins部署springboot项目的方法步骤
2022-06-05 05:18:31
![](https://img.aspxhome.com/file/2023/0/62760_0s.png)
MyBatis-Plus联表查询以及分页代码实例
2023-11-26 01:51:32
![](https://img.aspxhome.com/file/2023/8/60028_0s.png)
JavaMail实现邮件发送的方法
2023-08-18 06:37:38
Java使用DualPivotQuicksort排序
2022-05-22 20:58:12
Java排序的那些事之sort方法的使用详解
2023-01-22 00:18:35
![](https://img.aspxhome.com/file/2023/4/61014_0s.png)
JAVA抛出异常的三种形式详解
2022-06-26 22:44:32
Java枚举学习之定义和基本特性详解
2022-07-23 20:29:44
![](https://img.aspxhome.com/file/2023/5/64525_0s.png)
java数据结构-堆实现优先队列
2023-11-25 08:30:20
![](https://img.aspxhome.com/file/2023/5/59335_0s.jpg)
SpringBoot项目集成Flyway进行数据库版本控制的详细教程
2023-11-24 05:20:33
![](https://img.aspxhome.com/file/2023/0/59390_0s.png)
Java新手环境搭建 JDK8安装配置教程
2023-11-25 17:23:10
![](https://img.aspxhome.com/file/2023/1/60311_0s.jpg)
Java中synchronized关键字修饰方法同步的用法详解
2022-03-05 07:11:22
Spring Boot调用 Shell 脚本实现看门狗功能
2023-09-14 18:52:07
mybatis plus新增(insert)数据获取主键id的问题
2023-08-09 10:50:52
Spring Cloud中FeignClient实现文件上传功能
2023-06-23 07:57:09
java中如何截取字符串最后一位
2023-11-27 00:51:16
![](https://img.aspxhome.com/file/2023/6/60376_0s.png)
java Apache poi 对word doc文件进行读写操作
2023-09-23 02:13:15
![](https://img.aspxhome.com/file/2023/7/62987_0s.png)
Spring Boot常用注解(经典干货)
2023-11-24 22:29:10
使用WebSocket实现即时通讯(一个群聊的聊天室)
2023-11-29 03:00:46
如何安装系统认证签名过的APK
2023-07-24 21:35:40
SpringBoot全局异常处理与定制404页面的方法
2021-08-07 18:54:56
![](https://img.aspxhome.com/file/2023/9/64759_0s.png)