java用NIO实现文件传输_Java Nio 实现文件的传输
使用Java Nio實現文件的傳輸
1、ServerSocket.java
package ch2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel.MapMode;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class ServerSocket {
public static void main(String[] args) throws Exception {
int port = 10000;
//打開服務器套接字通道
ServerSocketChannel ssc = ServerSocketChannel.open();
//創建一個選擇器
Selector selector = Selector.open();
//設置非阻塞模式
ssc.configureBlocking(false);
InetSocketAddress address = new InetSocketAddress(port);
//綁定監聽端口
ssc.bind(address);
//注冊選擇器,保持等待模式
ssc.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("服務器已開啟,端口:"+port);
while(true){
selector.select();
//返回此選擇器的已選擇鍵集
Set keys=selector.selectedKeys();
Iterator iterKey=keys.iterator();
while(iterKey.hasNext()){
SelectionKey sk=iterKey.next();
//測試此鍵的通道是否已準備好接受新的套接字連接
if(sk.isAcceptable()){
SocketChannel sc=ssc.accept();
try {
//接收
new ReceiveAndSend().receiveFile(sc);
sc.close();
} catch (Exception e) {
}
}
}
}
}
}
ClientSocket.java
package ch2;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
public class ClientSocket {
public static void main(String[] args) throws Exception {
int port=10000;
String ip = "localhost";
//打開傳輸通道
SocketChannel sc = SocketChannel.open();
//連接
sc.connect(new InetSocketAddress(ip,port));
//發送文件
new ReceiveAndSend().sendFile(sc, "e:"+File.separator+"node.txt","node.txt");
//關閉傳輸通道
sc.close();
}
}
ReceiveAndSend.java
package ch2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
public class ReceiveAndSend {
/**
* 接收文件
* @param sc
* @throws IOException
*/
public void receiveFile(SocketChannel sc) throws IOException{
//獲取保存文件
File file=new File("e:"+File.separator+"mmm.txt");
FileOutputStream fos=new FileOutputStream(file);
//獲取通道
FileChannel foc = fos.getChannel();
//設置接收消息體緩沖區
ByteBuffer bb=ByteBuffer.allocateDirect(1024);
//設置接收消息頭緩沖區
ByteBuffer headByte=ByteBuffer.allocateDirect(32);
//接收消息頭
sc.read(headByte);
byte[] b=new byte[32];
headByte.flip();
for (int i = 0; headByte.hasRemaining(); i++) {
b[i]=headByte.get();
}
headByte.clear();
//獲取文件信息
String fileInfo=new String(b,Charset.forName("UTF-8"));
String[] strInfo=fileInfo.split("\\|");
System.out.println("文件:"+strInfo[0]+"--大小:"+strInfo[1]);
int read=sc.read(bb);
while(read!=-1){
bb.flip();
//寫入到輸出通道
foc.write(bb);
bb.clear();
read=sc.read(bb);
}
foc.close();
fos.close();
}
/**
* 發送文件
* @param sc
* @param path
* @param fileName
* @throws IOException
*/
public void sendFile(SocketChannel sc,String path,String fileName) throws IOException{
File file=new File(path);
FileInputStream fis = new FileInputStream(file);
FileChannel fic = fis.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
ByteBuffer headbb = ByteBuffer.allocateDirect(32);
int read=0;
long fileSize = file.length();
long sendSize=0;
System.out.println("文件大小:"+fileSize);
//拼接頭信息
String head=fileName+"|"+fileSize+"|;";
//將頭信息寫入緩沖區
headbb.put(head.getBytes(Charset.forName("UTF-8")));
int c=headbb.capacity()-headbb.position();
//填滿頭信息
for (int i = 0; i < c; i++) {
headbb.put(";".getBytes(Charset.forName("UTF-8")));
}
headbb.flip();
//將頭信息寫入到通道
sc.write(headbb);
do{
//將文件寫入到緩沖區
read = fic.read(bb);
sendSize+=read;
bb.flip();
//將文件寫入到通道
sc.write(bb);
bb.clear();
System.out.println("已傳輸/總大小:"+sendSize+"/"+fileSize);
}while(read!=-1&&sendSize
System.out.println("文件傳輸成功");
fic.close();
fis.close();
}
}
總結
以上是生活随笔為你收集整理的java用NIO实现文件传输_Java Nio 实现文件的传输的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 命名管道_Python:检
- 下一篇: java美元兑换,(Java实现) 美元