我最近做一个多线程多文件传输的东西,在构件框架上想请教下,该如何判断每1个文件结束了,也就是如何判断一次传输结束。还有个奇怪的问题,我用nio的write到channel里了i个字节,但是那边读出来的所有字节不够i个,而且只是中间一部分传输随机发生。下面是部分代码 ,有好的程序最好~~谢谢各位拉。 能帮忙加下qq176781788最好~ 网卡。。打网页慢。。
////////////////////////////////////////////////////////////////////////////server
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
it.remove();
if (key.isAcceptable()) {
System.out.println("Key is Acceptable");
ServerSocketChannel ssc = (ServerSocketChannel) key
.channel();
socket = (SocketChannel) ssc.accept();
socket.configureBlocking(false);
SelectionKey another = socket.register(sel,
SelectionKey.OP_READ | SelectionKey.OP_WRITE);
}
if (key.isReadable()) {
System.out.println("Key is readable");
readMessage(key); }public String readMessage(SelectionKey key) {
int nBytes = 0;
ByteBuffer temp = ByteBuffer.allocate(1);
temp.put((byte) 1);
temp.flip();
socket = (SocketChannel) key.channel();
File folder = new File("d:\\"
+ socket.socket().getInetAddress().toString());
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdir();
}
ByteBuffer buf = ByteBuffer.allocate(0x500000 + 16);
byte[] head = new byte[16];
try {
long length=0;
File f = new File(folder, "1");
f.createNewFile();
FileChannel fc = new FileOutputStream(f).getChannel();
buf.clear();
while((nBytes = socket.read(buf))>0){
length+=nBytes;
System.out.println("nbyte=" + nBytes);
buf.flip();
int i = fc.write(buf);
System.out.println("write=" + i);
buf.clear();
}
FileInputStream fis = new FileInputStream(f);
fis.read(head);
System.out.println("name="+new String(head));
System.out.println(socket.write(temp));
fc.close();
fis.close();
key.cancel();
f.renameTo(new File(folder,new String(head)));
System.out.println("length: "+length);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
///////////////////////////////////////////client
private class SendThread extends Thread {
File ff;
int nBytes;
ByteBuffer temp = ByteBuffer.allocate(1);
SendThread(File ff) {
this.ff = ff;
start();
} public void run() {
try {
makeConnection();
ByteBuffer bytebuf = ByteBuffer.allocate(0x500000 + 16);
FileChannel fc1 = new FileInputStream(ff).getChannel();
fc1.read(bytebuf);
bytebuf.flip();
nBytes=client.write(bytebuf);
bytebuf.clear();
while(true){
if(client.read(temp)>0)break;
}
System.out.println(nBytes);
fc1.close();
client.close();
} catch (IOException e) {
new RuntimeException(e);
}
}
}
每个文件分块都把本机ip和文件名加在头部一起传输
////////////////////////////////////////////////////////////////////////////server
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
it.remove();
if (key.isAcceptable()) {
System.out.println("Key is Acceptable");
ServerSocketChannel ssc = (ServerSocketChannel) key
.channel();
socket = (SocketChannel) ssc.accept();
socket.configureBlocking(false);
SelectionKey another = socket.register(sel,
SelectionKey.OP_READ | SelectionKey.OP_WRITE);
}
if (key.isReadable()) {
System.out.println("Key is readable");
readMessage(key); }public String readMessage(SelectionKey key) {
int nBytes = 0;
ByteBuffer temp = ByteBuffer.allocate(1);
temp.put((byte) 1);
temp.flip();
socket = (SocketChannel) key.channel();
File folder = new File("d:\\"
+ socket.socket().getInetAddress().toString());
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdir();
}
ByteBuffer buf = ByteBuffer.allocate(0x500000 + 16);
byte[] head = new byte[16];
try {
long length=0;
File f = new File(folder, "1");
f.createNewFile();
FileChannel fc = new FileOutputStream(f).getChannel();
buf.clear();
while((nBytes = socket.read(buf))>0){
length+=nBytes;
System.out.println("nbyte=" + nBytes);
buf.flip();
int i = fc.write(buf);
System.out.println("write=" + i);
buf.clear();
}
FileInputStream fis = new FileInputStream(f);
fis.read(head);
System.out.println("name="+new String(head));
System.out.println(socket.write(temp));
fc.close();
fis.close();
key.cancel();
f.renameTo(new File(folder,new String(head)));
System.out.println("length: "+length);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
///////////////////////////////////////////client
private class SendThread extends Thread {
File ff;
int nBytes;
ByteBuffer temp = ByteBuffer.allocate(1);
SendThread(File ff) {
this.ff = ff;
start();
} public void run() {
try {
makeConnection();
ByteBuffer bytebuf = ByteBuffer.allocate(0x500000 + 16);
FileChannel fc1 = new FileInputStream(ff).getChannel();
fc1.read(bytebuf);
bytebuf.flip();
nBytes=client.write(bytebuf);
bytebuf.clear();
while(true){
if(client.read(temp)>0)break;
}
System.out.println(nBytes);
fc1.close();
client.close();
} catch (IOException e) {
new RuntimeException(e);
}
}
}
每个文件分块都把本机ip和文件名加在头部一起传输
解决方案 »
- 制作Java安装包,如何精简Jre
- J2SE中,使用带参方法的好处是什么?
- 问一个关于正则表达式的问题, 谢谢大家!
- 编译器报无法从静态上下文中引用非静态方法是什么原因?
- 如何在JLable上显示BMP图片?
- JInternalFrame最大化后怎么才能在菜单上出现它的关闭、最小化、还原按钮?
- 请大家推荐一体学习JAVA语言的入门电子图书及资料,让后人好少走弯路!!
- How to create an object of a protected inner class of another package?
- 选课了!大家给意见。给分!!
- 谁能告诉我吗?
- acegi是什么的缩写呢?
- 如何解决java多线程问题
while((nBytes = socket.read(buf))>0){
length+=nBytes;
System.out.println("nbyte=" + nBytes);
buf.flip();
int i = fc.write(buf);
System.out.println("write=" + i);
buf.clear();
}
谁帮忙解决下
"DATA"是个文本标志,占4字节
"数据长度"占32个字节
接收数据的时候,首先解析这个串,验证下"数据长度"是否和"数据块"长度相同,当接收到数据长度为0时,视数据发送完成。
略改下还可以做到断点续传和数据校验。