客户端:

package tt;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.net.Socket; 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();
}
} }

服务端:

package test;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket; 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();
}
} }

java socket 发送文件的更多相关文章

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

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

  2. JAVA Socket:文件传输

    客户端:读取文件(FileInputStream),发送文件(OutputStream) 服务器端:接收文件(InputStream),写文件(FileOutputStream) 客户端代码: pac ...

  3. Socket发送文件

    .Net.cs using System; using System.Collections.Generic; using System.IO; using System.Linq; using Sy ...

  4. android开发,socket发送文件,read阻塞,得不到文件尾-1

    这是我的接收文件代码:开始可以读取到-1,但是现在又读取不到了,所以才加上红色字解决的(注释的代码) File file = new File(mfilePath,"chetou." ...

  5. Python -- 网络编程 -- Socket发送文件

    客户端如果直接send两次,一次发文件名,一次发文件内容 服务端接受的时候会一起接收,不知怎么分开发送,或者分开接收, 或者全部接收再解析内容 今天发现传送mp3文件的时候没问题,传送文本文件的话,以 ...

  6. python 通过 socket 发送文件

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

  7. java socket发送xml报文

    ServerRun.java import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; pub ...

  8. java socket 多线程网络传输多个文件

    http://blog.csdn.net/njchenyi/article/details/9072845 java socket 多线程网络传输多个文件 2013-06-10 21:26 3596人 ...

  9. (转)JAVA socket 进行十六进制报文交互测试

    import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io. ...

随机推荐

  1. 在Windows 环境下编译Qt静态库(QT5.32)

    参考链接 Qt5.3 Tools and Versions MinGW ICU ActivePerl Qt 安装MinGW工具链环境 这里在Win32环境下要安装一个MinGW工具链,这里最好是先安装 ...

  2. 非Controller类无法使用Service bean解决方案

      尝试方案: 1 在Spring的配置文件springmvc.xml中,增加扫描项base-package="zxs.ssm.util",增加你需要使用service的类所在的包 ...

  3. Asp.net MVC 版本简史

    http://www.dotnet-tricks.com/Tutorial/mvc/XWX7210713-A-brief-history-of-Asp.Net-MVC-framework.html A ...

  4. PHP运行错最有效解决办法Fatal error: Out of memory (allocated 786432) (tried to allocate 98304 bytes) in H:\freehost\zhengbao2\web\includes\lib_common.php on line 744

    原文 PHP运行错最有效解决办法Fatal error: Out of memory (allocated 6029312) Fatal error: Out of memory (allocated ...

  5. Magento打印(配送单、退款单、发票)时PDF中的乱码问题

    我使用Magento1.4.2,在其自带的TTF文件不能很好地解析中文字符,TTF文件的位置在网站根目录下的/lib/LinLibertineFont/中.打印的中文字符都是这样的 解决方法: 1.在 ...

  6. Caused by: java.lang.OutOfMemoryError: PermGen space.

    现在eclipse需要加载4个项目同时运行了,所以当服务启动的时候,出现Caused by: java.lang.OutOfMemoryError: PermGen space.空间不足错误,我说一下 ...

  7. httpd.conf

    修改配置文件-时会弹出一个文本式的文件 1.搜索:#LoadModule rewrite_module modules/mod_rewrite.so,去掉前面的# 2.全部替换AllowOverrid ...

  8. A Guide to Creating a Quality Project Schedule

    Successful projects start with a good quality project schedule. Creating a schedule is one of the fi ...

  9. MFC之向导页、消息框、文件选择、字体、颜色(三)

    属性页对话框的分类 属性页对话框想必大家并不陌生,XP系统中桌面右键点属性,弹出的就是属性页对话框,它通过标签切换各个页面.另外,我们在创建MFC工程时使用的向导对话框也属于属性页对话框,它通过点击“ ...

  10. Apple Developer Program Roles Overview

    Apple Developer Program Roles Overview There are three roles that can be assigned to Apple Developer ...