JavaTCP上传图片代码实例
作者:凌晨两点半的太阳v 时间:2022-02-08 19:05:18
1.客户端代码
public class UploadPicClient {
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
//1,创建客户端socket
Socket s = new Socket("localhost",10088);
//2,读取客户端要上传的图片文件
FileInputStream fis = new FileInputStream("D:\\workspace\\day2019.1.17\\lanjing.jpg");
//3,获取Socket输出流,将读到的图片的数据发送到服务端
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1021];
int len =0;
while((len=fis.read(buf))!=-1){
out.write(buf,0,len);
}
//告诉服务端说:这边的数据发送完毕让服务端停止读取
s.shutdownOutput();
//读取服务端发回的内容
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int lenIn = in.read(buf);
String text = new String (buf,0,lenIn);
System.out.println(text);
//关闭资源
fis.close();
s.close();
}
}
2.服务端代码
public class UploadPicSever {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建tcp的socket服务端
ServerSocket ss = new ServerSocket(10088);
//获取客户端
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
//读取客户端发来的数据
InputStream in = s.getInputStream();
//将读取到的数据存储到一个文件中。
File dir = new File("D:\\workspace\\day2019.1.17");
if(!dir.exists()){
dir.mkdirs();
}
File file = new File(dir,"blue.jpg");
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while ((len=in.read(buf))!=-1){
fos.write(buf,0,len);
}
//获取socket输出流,将上传成功字样发送给客户端
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
ss.close();
}
上传后和上传前的图片:
来源:https://blog.csdn.net/lzq1326253299/article/details/86528524
标签:java,javatcp,上传图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot JPA实现增删改查、分页、排序、事务操作等功能示例
2023-06-27 23:54:05
C#生成随机验证码代码分享
2023-07-17 21:22:26
使用@Autowired注解有错误提示的解决
2023-11-09 20:44:57
![](https://img.aspxhome.com/file/2023/9/62399_0s.png)
基于swing实现窗体拖拽和拉伸
2023-11-12 22:32:40
![](https://img.aspxhome.com/file/2023/2/69082_0s.gif)
详解Spring Boot Security工作流程
2023-12-17 12:23:52
![](https://img.aspxhome.com/file/2023/8/62668_0s.jpg)
Java Socket实现多人聊天系统
2023-08-08 04:44:35
![](https://img.aspxhome.com/file/2023/2/65322_0s.jpg)
java同步之如何写一个锁Lock
2023-04-23 08:10:54
![](https://img.aspxhome.com/file/2023/3/63843_0s.png)
Java异常处理机制try catch流程详解
2022-09-23 08:51:09
![](https://img.aspxhome.com/file/2023/4/62404_0s.png)
Unity 百度AI实现Logo商标识别
2023-06-09 17:04:26
![](https://img.aspxhome.com/file/2023/5/84465_0s.jpg)
C#中Convert.ToDecimal()报错问题的解决
2022-05-09 06:05:56
Spring boot集成redis lettuce代码实例
2022-08-03 12:21:19
java9中gc log参数迁移
2022-06-28 03:21:01
导入maven项目各个注解均报错的解决方案
2022-02-23 12:35:09
![](https://img.aspxhome.com/file/2023/0/71820_0s.jpg)
Spring JDK动态 代理实现过程详解
2023-11-16 19:42:15
![](https://img.aspxhome.com/file/2023/2/59392_0s.png)
Java实现的微信图片处理工具类【裁剪,合并,等比例缩放等】
2022-03-26 11:32:59
Java使用组合模式实现表示公司组织结构功能示例
2022-07-31 08:03:37
![](https://img.aspxhome.com/file/2023/4/75404_0s.png)
java 使用BigDecimal进行货币金额计算的操作
2023-12-05 23:19:34
![](https://img.aspxhome.com/file/2023/9/90019_0s.jpg)
MyBatis传入集合 list 数组 map参数的写法
2022-12-02 20:21:45
Mybatis使用JSONObject接收数据库查询的方法
2023-01-17 05:10:43
![](https://img.aspxhome.com/file/2023/7/62647_0s.png)
Java读文件修改默认换行符的实现
2023-11-29 08:24:32