我现在能够把文件的内容发送到服务器上了,可是不是预期的效果。我想把文件上传到服务器(是文件!!)而不是文件的内容。
以下是handler:
package netty_4;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.RandomAccessFile;
import java.util.Date;
public class TimeServerHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
System.out.println("服务器读取到客户端请求...");
ByteBuf buf=(ByteBuf) msg;
byte[] req=new byte[buf.readableBytes()];
buf.readBytes(req);
String body=new String(req);
//这部分可以无视掉
RandomAccessFile randomFile = new RandomAccessFile("e://poem.txt", "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
// 将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(body);
randomFile.close();
//到这里
//打印出的是文件的内容
System.out.println(+body);
String curentTime="QUERY TIME ORDER".equalsIgnoreCase(body)?new Date(System.currentTimeMillis()).toString():"BAD ORDER";
ByteBuf resp=Unpooled.copiedBuffer(curentTime.getBytes());
ctx.write(resp);
System.out.println("服务器做出了响应");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
System.out.println("服务器readComplete 响应完成");
ctx.close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
ctx.close();
System.out.println("服务器异常退出"+cause.getMessage());
}
}
上传文件不就是上传内容吗?你在服务器端把内容写到和你上传文件同名的文件里,不就是上传完成了######不是说要用到FileRegion么
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。