利用java Socket发送,接收文件.
2014-05-15 23:07
369 查看
客户端.
package net.demo;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class SendFile {
public static void main(String[] args) throws Exception {
new SendFile().sendFile(null, "D:\\a.jpg", 8821);
}
public void sendFile(String romteUrl, String path, int port)
throws Exception {
File file = getFile(path);
Socket scket = new Socket("localhost",port);
DataOutputStream dos = new DataOutputStream(scket.getOutputStream());
DataInputStream dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(path)));
int size = 1024;
byte[] bufArry = new byte[size];
dos.writeUTF(file.getName());
dos.flush();
dos.writeLong(file.length());
dos.flush();
while (true) {
int read = 0;
if (dis != null) {
read = dis.read(bufArry);
}
if (read == -1) {
break;
}
dos.write(bufArry, 0, read);
}
dos.flush();
close(dis, dos, scket, null);
}
public void close(DataInputStream dis, DataOutputStream dos,
Socket sockect, ServerSocket ss) {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (sockect != null) {
try {
sockect.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public File getFile(String path){
File file = new File(path);
return file;
}
}
服务端
package net.demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class RecFile {
public static void main(String[] args) throws Exception {
new RecFile().recFile("F:\\", "localhost", 8821);
}
public void recFile(String savePath, String ip, int port) throws Exception {
ServerSocket socket = new ServerSocket(port);
Socket s = socket.accept();
DataInputStream dis = new DataInputStream(new BufferedInputStream(
s.getInputStream()));
int size = 1024;
byte[] buf = new byte[size];
int passedlen = 0;
long len = 0;
savePath += dis.readUTF();
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)));
len = dis.readLong();
System.out.println("len:" + len +" KB");
System.out.println("开始接收!");
while(true){
int read = 0;
if(dis != null){
read = dis.read(buf);
}
passedlen += read;
if(read == -1){
break;
}
System.out.println("文件接收了" + (passedlen * 100 / len) + "%");
fileOut.write(buf, 0, read);
}
System.out.println("接收完成..." + savePath);
fileOut.close();
}
}
package net.demo;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class SendFile {
public static void main(String[] args) throws Exception {
new SendFile().sendFile(null, "D:\\a.jpg", 8821);
}
public void sendFile(String romteUrl, String path, int port)
throws Exception {
File file = getFile(path);
Socket scket = new Socket("localhost",port);
DataOutputStream dos = new DataOutputStream(scket.getOutputStream());
DataInputStream dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(path)));
int size = 1024;
byte[] bufArry = new byte[size];
dos.writeUTF(file.getName());
dos.flush();
dos.writeLong(file.length());
dos.flush();
while (true) {
int read = 0;
if (dis != null) {
read = dis.read(bufArry);
}
if (read == -1) {
break;
}
dos.write(bufArry, 0, read);
}
dos.flush();
close(dis, dos, scket, null);
}
public void close(DataInputStream dis, DataOutputStream dos,
Socket sockect, ServerSocket ss) {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (sockect != null) {
try {
sockect.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public File getFile(String path){
File file = new File(path);
return file;
}
}
服务端
package net.demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class RecFile {
public static void main(String[] args) throws Exception {
new RecFile().recFile("F:\\", "localhost", 8821);
}
public void recFile(String savePath, String ip, int port) throws Exception {
ServerSocket socket = new ServerSocket(port);
Socket s = socket.accept();
DataInputStream dis = new DataInputStream(new BufferedInputStream(
s.getInputStream()));
int size = 1024;
byte[] buf = new byte[size];
int passedlen = 0;
long len = 0;
savePath += dis.readUTF();
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)));
len = dis.readLong();
System.out.println("len:" + len +" KB");
System.out.println("开始接收!");
while(true){
int read = 0;
if(dis != null){
read = dis.read(buf);
}
passedlen += read;
if(read == -1){
break;
}
System.out.println("文件接收了" + (passedlen * 100 / len) + "%");
fileOut.write(buf, 0, read);
}
System.out.println("接收完成..." + savePath);
fileOut.close();
}
}
相关文章推荐
- Java实现Socket发送和接收文件的代码
- Java实现Socket发送和接收文件
- Java 小例子:通过 Socket 发送和接收文件
- Java 小例子:通过 Socket 发送和接收文件
- Java 小例子:通过 Socket 发送和接收文件
- Java实现Socket发送和接收文件的代码(java 粘包个人联系)
- Java利用socket连接到一台主机并向主机发送文件
- Java 小例子:通过 Socket 发送和接收文件
- Java利用socket连接到一台主机并向主机发送文件
- java,利用一个socket发送多个文件
- [Java]利用DOM解析DOM文件|利用socket发送XML DOM
- Java利用HttpURLConnection发送post请求上传文件
- Socket多人聊天(文字+图片+多文件发送和接收)
- Socket 发送或接收文件
- Java Socket发送与接收HTTP消息简单实现
- Java Socket发送与接收HTTP消息简单实现
- C#网络Socket的数据发送与接收处理(利用异步)的模板(模式)
- Java Socket发送与接收HTTP消息简单实现
- 在silverlight中利用socket发送图片或文件
- C#网络Socket的数据发送与接收处理(利用异步)的模板(模式)