客户端

public class ClientTcpSend {

public static void main(String[] args) {

int length = 0;

byte[] sendByte = null;

Socket socket = null;

DataOutputStream dout = null;

FileInputStream fin = null;

try {

try {

socket = new Socket();

socket.connect(new InetSocketAddress("127.0.0.1", 33456),10 * 1000);

dout = new DataOutputStream(socket.getOutputStream());

File file = new File("E:\\TU\\DSCF0320.JPG");

fin = new FileInputStream(file);

sendByte = new byte[1024];

dout.writeUTF(file.getName());

while((length = fin.read(sendByte, 0, sendByte.length))>0){

dout.write(sendByte,0,length);

dout.flush();

}

} catch (Exception e) {

} finally{

if (dout != null)

dout.close();

if (fin != null)

fin.close();

if (socket != null)

socket.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

服务端

public class ServerTcpListener implements Runnable {

@Override

public void run() {

}

public static void main(String[] args) {

try {

final ServerSocket server = new ServerSocket(33456);

Thread th = new Thread(new Runnable() {

@Override

public void run() {

while (true) {

try {

System.out.println("开始监听。。。");

Socket socket = server.accept();

System.out.println("有链接");

receiveFile(socket);

} catch (Exception e) {

e.printStackTrace();

}

}

}

});

th.run();

} catch (Exception ex) {

ex.printStackTrace();

}

}

public static void receiveFile(Socket socket) throws IOException {

byte[] inputByte = null;

int length = 0;

DataInputStream din = null;

FileOutputStream fout = null;

try {

din = new DataInputStream(socket.getInputStream());

fout = new FileOutputStream(new File("E:\\"+din.readUTF()));

inputByte = new byte[1024];

System.out.println("开始接收数据...");

while (true) {

if (din != null) {

length = din.read(inputByte, 0, inputByte.length);

}

if (length == -1) {

break;

}

System.out.println(length);

fout.write(inputByte, 0, length);

fout.flush();

}

System.out.println("完成接收");

} catch (Exception ex) {

ex.printStackTrace();

} finally {

if (fout != null)

fout.close();

if (din != null)

din.close();

if (socket != null)

socket.close();

}

}

}

dout java,java socket 发送文件相关推荐

  1. javasocket连续给服务器发送消息,Java通过Socket发送和接收多条消息

    我们需要实现一个Socket客户端,它应该连接到一个接受TCP连接的服务器.如果我通过netcap与服务器进行通信,我会立即得到它的响应(通过命令行).Java通过Socket发送和接收多条消息 的工 ...

  2. python怎么发送代码文件_python 通过 socket 发送文件的实例代码

    目录结构: client: #!/usr/bin/env python # -*-coding:utf-8 -*- import socket, struct, json download_dir = ...

  3. java通过socket传输文件

    客户端代码 package 基于socket的文件传输;import java.io.DataInputStream; import java.io.DataOutputStream; import ...

  4. socket发送文件夹

    作者:梦幻极光  http://ww.w.l.blog.163.com/blog/static/162589962007516103850603/ 发送文件夹有很多方法,但是网上的代码很少,VC只有飞 ...

  5. java调用mq发送文件_谁有mq发送接收文件的java代码

    mq安装目录下有简单的发送和接收代码,具体在samp目录下有一些不同语言的源代码,可以借鉴. 类介绍: SendMSG:消息发送类. Main():主方法. SendMSG():消息发送方法. 方法描 ...

  6. java模拟post发送文件

    String result="";CloseableHttpClient httpClient = null;try {// 创建HttpClienthttpClient = Ht ...

  7. 模拟微信发送文件给好友/群

    JAVA模拟微信发送文件给好友/群 通过google开发者模式抓取https://file2.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json ...

  8. Java Socket发送与接收HTTP消息简单实现

    在上次Java Socket现实简单的HTTP服务我 们实现了简单的HTTP服务,它可以用来模拟HTTP服务,用它可以截获HTTP请求的原始码流,让我们很清楚的了解到我们向服务发的HTTP消息的结 构 ...

  9. java socket安全策略文件

    获取策略文件的Java服务器端代码import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOExce ...

最新文章

  1. 双击.exe文件出现Debug Error: abort() has been called解决办法(之一)
  2. 理性预期学派(Rational Expectation School)
  3. 有没有可以搜PHP作业的软件,有没有可以搜计算机题库的软件?
  4. sticky-footer实现记录
  5. 使用IDM下载,不适用默认浏览器下载
  6. 数据结构--链式线性表
  7. Linux namespace之:uts namespace
  8. CodeIgniter的伪静态配置
  9. 浅谈ThreadPoolExecutor线程池底层源码
  10. ITIL学习笔记——核心流程之:变更管理
  11. 在计算机操作中粘贴的快捷键是什么,键盘按什么键复制粘贴 键盘上复制粘贴快捷键是哪个键...
  12. 华为“吐出”的高端市场,小米 Ov 却吃不下
  13. iOS-仿网易云音乐控制器
  14. Linux 自旋锁 使用
  15. Java随机双色球号码Demo
  16. 广联达需要的计算机系统配置,广联达bim安装算量软件对电脑要求配置高吗?
  17. 企业订单管理软件|移讯云订货宝系统功能介绍
  18. Java中Colletcions.sort方法详解以及Comparable和Comparator两个接口的区别
  19. 绿色版的PDF格式转换软件
  20. 三年开发经验,字节跳动抖音组离职后,一口气拿到15家公司Offer

热门文章

  1. Eggplant 依托人工智能技术,改造软件自动化测试
  2. 曝 iPhone 13 系列定价有望下调:起售价或低于 5499 元;TikTok 成为全球收入最高 App|极客头条...
  3. 跨过虚拟化技术浪潮,这家企业快步入局云数据管理​
  4. 放弃机器学习,Python 如何进行物体检测?
  5. 使用虚幻引擎4年,我想再谈谈他的网络架构
  6. 一文详解 Java 的几把 JVM 级锁
  7. 我们教授编程的方式统统不对!
  8. 2020 年,开启现代库的基建学习 —— 从项目演进看前端工程化发展
  9. “技术驱动创新”正成为企业发展新方向,开发者该如何修炼自己?
  10. 他是哈佛计算机博士,却成落魄画家,后逆袭为硅谷创业之父 |人物志