62,636
社区成员




发送方
dos.writeInt(files.size());//文件个数
for(File f:files){
dos.writeUTF(f.getName());//文件名
dos.writeLong(f.length());//文件长度
dos.write(...);//文件内容
}
接收方
int n = dis.readInt();//文件个数
for(int i=0;i<n;i++){
String filename = dis.readUTF();//文件名
long length = dis.readLong();//文件长度
dis.read(...);//读取length长度的字节
...//保存文件
}
package nioserver;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
public class ChannelClient {
private SocketChannel sc;
private String hostIp;
private int hostPort;
public ChannelClient(String hostIp, int port) {
this.hostIp = hostIp;
this.hostPort = port;
}
private void setUpConnection() {
try {
SocketAddress remote = new InetSocketAddress(hostIp, hostPort);
sc = SocketChannel.open();
sc.connect(remote);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void sendFiles(File[] files) {
FileChannel fileChannel = null;
try {
for (int i = 0;i < files.length; i++) {
byte[] namebyte = files[i].getName().getBytes("UTF-8");
long size = files[i].length();
int nameLength = namebyte.length;
fileChannel = new FileInputStream(files[i]).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.clear();
buffer.putInt(4+8+nameLength);
buffer.putInt(nameLength);
buffer.put(namebyte);
buffer.putLong(size);
buffer.flip();
while(buffer.hasRemaining()){
sc.write(buffer);
}
long count = 1024*1024;
long read = 0L;
while(read < size){
if(size - read < count)
count = size - read;
read += fileChannel.transferTo(0+read, count, sc);
System.out.println("read:"+read);
}
fileChannel.close();
if(i < files.length -1){
sc.write(ByteBuffer.wrap(new byte[]{1}));
System.out.println(1);
}
else
sc.write(ByteBuffer.wrap(new byte[]{0}));
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ChannelClient client = new ChannelClient("127.0.0.1", 3000);
client.setUpConnection();
File[] files = new File("D:\\send").listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.isFile();
}
});
client.sendFiles(files);
}
}
package nioserver;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class FilesChannelServer {
private String fileName = null;
private long fileSize = 0L;
private int hostPort;
private ServerSocketChannel ssl = null;
private SocketChannel clientChannel = null;
private ByteBuffer buffer = null;
public FilesChannelServer(int port) {
this.hostPort = port;
}
private void setUpConnection() {
try {
ssl = ServerSocketChannel.open();
SocketAddress address = new InetSocketAddress("127.0.0.1", hostPort);
ssl.socket().bind(address);
System.out.println("bind.....");
} catch (IOException e) {
e.printStackTrace();
}
}
private void parseHead(int headlength) {
buffer = ByteBuffer.allocate(headlength);
try {
while(buffer.position() < buffer.capacity())
clientChannel.read(buffer);
buffer.flip();
byte[] filenamebyte = new byte[buffer.getInt()];
buffer.get(filenamebyte);
fileName = new String(filenamebyte,"UTF-8");
fileSize = buffer.getLong();
} catch (IOException e) {
e.printStackTrace();
}
}
private void parseBody(long size) {
long read = 0L;
long count = 8192;
FileChannel fileChannel = null;
try {
fileChannel = new FileOutputStream("D:\\receive\\" + fileName)
.getChannel();
System.out.println(fileName);
while (read < size) {
if (size - read < count)
count = size - read;
read += fileChannel
.transferFrom(clientChannel, 0 + read, count);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fileChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private int parse() {
int i = 0;
try {
buffer = ByteBuffer.allocate(4);
while(buffer.position() < buffer.capacity())
clientChannel.read(buffer);
buffer.flip();
parseHead(buffer.getInt());
parseBody(fileSize);
buffer = ByteBuffer.allocate(1);
while(buffer.position() < buffer.capacity())
clientChannel.read(buffer);
buffer.flip();
i = buffer.get();
} catch (IOException e) {
e.printStackTrace();
}
return i;
}
private void acceptConnection() {
while (true) {
try {
clientChannel = ssl.accept();
int j = 1;
while(j != 0){
j = parse();
System.out.println(j);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (clientChannel != null)
clientChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
FilesChannelServer cs = new FilesChannelServer(3000);
cs.setUpConnection();
cs.acceptConnection();
}
}
package net;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author Life
*发送文件的客户端
*/
public class FileSendClient {
protected String hostIP;
protected int hostPort;
protected OutputStream socketInS;
private Socket client;
public FileSendClient(String ip, int portNumber) {
this.hostIP = ip;
this.hostPort = portNumber;
}
/**
* 建立连接
*/
public void setUpConnection() {
try {
client = new Socket(hostIP, hostPort);
socketInS = client.getOutputStream();
} catch (UnknownHostException e) {
throw new RuntimeException(e.toString());
} catch (IOException e) {
throw new RuntimeException(e.toString());
}
}
/**
* @param files
* 用zip流发送多个文件
*
*/
public void sendFile(File[] files) {
BufferedInputStream fileReader = null;
ZipOutputStream zos = new ZipOutputStream(socketInS);
BufferedOutputStream socketWriter = new BufferedOutputStream(zos);
byte[] buff = new byte[8192];
int c = 0;
try {
for (File file : files) {
fileReader = new BufferedInputStream(new FileInputStream(file));
zos.putNextEntry(new ZipEntry(file.getName()));
while ((c = fileReader.read(buff)) != -1) {
socketWriter.write(buff, 0, c);
}
socketWriter.flush();
try {
if (fileReader != null)
fileReader.close();
} catch (IOException e) {
System.out.println("Error closing fileReader" + e);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socketWriter != null )
socketWriter.close();
} catch (IOException e) {
System.out.println("Error closing socketWriter" + e);
}finally{
if(client != null && client.isConnected()){
try {
client.close();
} catch (IOException e) {
System.out.println("Error closing socket" + e);
}
}
}
}
}
public static void main(String[] args) {
FileSendClient fileSendClient = new FileSendClient("127.0.0.1", 3000);
fileSendClient.setUpConnection();
File[] files = new File("D:\\send").listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.isFile();
}
});
fileSendClient.sendFile(files);
}
}
package net;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author Life
*文件接收服务器
*/
public class FileReceiveServer {
protected int listenPort;
private int maxConnection;
public FileReceiveServer(int listenPort,int maxConnection) {
this.listenPort = listenPort;
this.maxConnection = maxConnection;
}
/**
* 建立监听端口
*/
public void setUpConnection(){
for(int i = 0; i < maxConnection; i++){
new Thread(new FileReceiverHandle(),"handle"+i).start();
}
}
/**
* 接收文件客户端的连接
*/
public void acceptConnection() {
try {
ServerSocket server = new ServerSocket(listenPort);
Socket incomingSocket = null;
while (true) {
incomingSocket = server.accept();
handlerConnection(incomingSocket);
}
} catch (BindException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param incomingSocket
* 处理接收到的连接(socket)
*/
private void handlerConnection(Socket incomingSocket) {
FileReceiverHandle.processConnection(incomingSocket);
}
public static void main(String[] args) {
FileReceiveServer fileReceiveServer = new FileReceiveServer(3000,5);
fileReceiveServer.setUpConnection();
fileReceiveServer.acceptConnection();
}
}
package net;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* @author Life
*文件接收处理程序
*/
public class FileReceiverHandle implements Runnable {
private static List<Socket> socketsPool = new LinkedList<Socket>();
private Socket connection = null;
/**
* @param incomingSocket
* 降接收到的连接放到一个池中
*
*/
public static void processConnection(Socket incomingSocket) {
synchronized (socketsPool) {
socketsPool.add(socketsPool.size(), incomingSocket);
socketsPool.notifyAll();
}
}
/**
* 按zip流解码获取接收到的流
*/
public void handlerConnection() {
InputStream inputFromSocket = null;
BufferedOutputStream fileWriter = null;
BufferedInputStream socketReader = null;
byte[] buff = new byte[8192];
int c = 0;
try {
inputFromSocket = connection.getInputStream();
ZipInputStream zis = new ZipInputStream(inputFromSocket);
socketReader = new BufferedInputStream(zis);
ZipEntry e = null;
while ((e = zis.getNextEntry()) != null) {
System.out.println(e.getName());
fileWriter = new BufferedOutputStream(new FileOutputStream(
new File("D:\\receive\\" + e.getName())));
while ((c = socketReader.read(buff)) != -1) {
fileWriter.write(buff, 0, c);
}
try {
fileWriter.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socketReader != null) {
socketReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(connection != null){
connection.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
/*
* 处理池中的连接
*/
public void run() {
while (true) {
synchronized (socketsPool) {
while (socketsPool.isEmpty()) {
try {
socketsPool.wait();
} catch (InterruptedException e) {
return;
}
}
connection = socketsPool.remove(socketsPool.size() - 1);
}
handlerConnection();
}
}
}