利用socket传递图片
2024-09-05 03:16:12
package com.company.s3; import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket; public class Server {
public static void main(String[] args) throws Exception{
byte[] byteArray=new byte[2048];
ServerSocket serverSocket=new ServerSocket(8088);
Socket socket=serverSocket.accept(); InputStream inputStream=socket.getInputStream();
int readLength=inputStream.read(byteArray); FileOutputStream pngoutputstream=new FileOutputStream(new File("e:\\2.jpg"));
while (readLength!=-1){
pngoutputstream.write(byteArray,0,readLength);
readLength=inputStream.read(byteArray);
}
pngoutputstream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
package com.company.s3; import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.Socket; public class Client {
public static void main(String[] args) throws Exception{
String pngFile="e:\\1.jpg";
FileInputStream pngstream=new FileInputStream(new File(pngFile));
byte[] byteArray=new byte[2048]; System.out.println("socket begin "+System.currentTimeMillis());
Socket socket=new Socket("localhost",8088);
System.out.println("socket end"+System.currentTimeMillis()); OutputStream outputStream=socket.getOutputStream(); int readLength=pngstream.read(byteArray);
while (readLength!=-1){
outputStream.write(byteArray,0,readLength);
readLength=pngstream.read(byteArray);
}
outputStream.close();
pngstream.close();
socket.close(); }
}
最新文章
- es6小白学习笔记(一)
- jquery 回到顶部,简洁大方
- iOS请求服务器数据去空NSNull
- F - Tree
- Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心
- Delphi下的RTTI函数大全
- ##DAY9 UITabBarController
- PL/SQL database character set(AL32UTF8) and Client character set(ZHS16GBK) are different
- Bayan 2015 Contest Warm Up D题(GCD)
- 设置Nginx+php-fpm显示错误信息
- C++中的继承(2)类的默认成员
- Mac_mac下使用ll等指令
- 一天一个Linux命令--dhclient
- Java 生态核心知识点整理
- About certificate
- springBoot(4)---热部署,配置文件使用
- [Day8] eclipse
- 转角遇见——Software
- Git与GitHub的基本使用
- day20 类的约束
热门文章
- python:json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes问题解决
- sleep方法要求处理中断异常:InterruptedException
- Windows密码获取和破解(初探)
- Android 开发凉了吗!
- django补充CBV和FBV模式
- laravel .env文件的使用
- Spring Cloud进阶之路 | 一:服务注册与发现(nacos)
- 【zabbix服务】修改zabbix_server默认端口号
- feign响应Image流对象
- 一个神奇的HTML标签-----marquee