客户端:

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. Mars 是微信官方的终端基础组件,是一个使用 C++ 编写的业平台性无关的基础组件

    http://www.oschina.net/p/wechat-mars http://www.oschina.net/news/80453/wewechat-open-source-plan

  2. C++控制程序只运行一个实例

    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { ...

  3. MongoDB直接执行js脚本

    有时候很大一段命令要执行,中间有错就得重新写,这样超麻烦的,不妨存放于js文件里,然后通过shell命令执行,重复利用率高,修改方便. 比如创建test.js print('=========WECO ...

  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. ArcGIS Engine开发之旅03--ArcGIS Engine中的控件

    原文:ArcGIS Engine开发之旅03--ArcGIS Engine中的控件 制图控件,如MapControl.PageLayoutControl,其中MapControl控件主要用于地理数据的 ...

  6. (leetcode)Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  7. ar1020 驱动移植 无效

    移植ar1020的spi驱动.驱动移植过来后,在原来的板子上都能够正常运行,而在新的板子却没有效果. 最后查看新旧板子的AR1020的电路,发现M2引脚连接不同.M2高电平连接的touch是5线的接口 ...

  8. 为什么一个object_id在dba_objects中为什么查不到记录?

    SQL> drop table test purge;SQL> create table test (id int,comments CLOB); SQL> select INDEX ...

  9. How to read the HTML DTD

    Contents How to read the HTML DTD 1. DTD Comments 2. Parameter Entity definitions 3. Element declara ...

  10. Android标签云控件:TagCloudLinkView

    可以完成添加.删除.选择操作的标签控件,可以自定样式(颜色等),可删除的标签末尾带有'×'符号.和Android-Cloud-TagView-Plus(见正文相关代码)类似. 1.layout in ...