服務端:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class TransferToServer {
public static void main(String[] args) throws IOException {
// 創建socket channel
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
ServerSocket ss = serverSocketChannel.socket();
ss.setReuseAddress(true);// 地址重用
ss.bind(new InetSocketAddress("localhost", 9026));// 綁定地址
System.out.println("監聽端口 : "
+ new InetSocketAddress("localhost", 9026).toString());
// 分配一個新的字節緩衝區
ByteBuffer dst = ByteBuffer.allocate(4096);
// 讀取數據
while (true) {
SocketChannel channle = serverSocketChannel.accept();// 接收數據
System.out.println("Accepted : " + channle);
channle.configureBlocking(true);// 設置阻塞,接不到就停
int nread = 0;
while (nread != -1) {
try {
nread = channle.read(dst);// 往緩衝區裏讀
byte[] array = dst.array();//將數據轉換爲array
//打印
String string = new String(array, 0, dst.position());
System.out.print(string);
dst.clear();
} catch (IOException e) {
e.printStackTrace();
nread = -1;
}
}
}
}
}
客戶端:
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
public class TransferToClient {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
// 打開socket的nio管道
SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress("localhost", 9026));// 綁定相應的ip和端口
sc.configureBlocking(true);// 設置阻塞
// 將文件放到channel中
FileChannel fc = new FileInputStream("C:/tasks.txt").getChannel();// 打開文件管道
//做好標記量
long size = fc.size();
int pos = 0;
int offset = 4096;
long curnset = 0;
long counts = 0;
//循環寫
while (pos<size) {
curnset = fc.transferTo(pos, 4096, sc);// 把文件直接讀取到socket chanel中,返回文件大小
pos+=offset;
counts+=curnset;
}
//關閉
fc.close();
sc.close();
//打印傳輸字節數
System.out.println(counts);
// 打印時間
System.out.println("bytes send--" + counts + " and totaltime--"
+ (System.currentTimeMillis() - start));
}
}
jar pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cloudDay05</groupId>
<artifactId>cloudDay05</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 指明編譯源代碼時使用的字符編碼,maven編譯的時候默認使用的GBK編碼, 通過project.build.sourceEncoding屬性設置字符編碼,告訴maven這個項目使用UTF-8來編譯 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.24.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.12.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.4</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>