资源简介

Netty+H5实现实时进度条文件上传,支持断点续传。 1、WebSocketServer:服务启动类 2、服务启动后,浏览器访问http://localhost:9999 3、FileWebSocketFrameHandler类SERVER_SAVE_PATH常量为文件上传保存路经

资源截图

代码片段和文件信息

/**
 * Created on 2018年7月20日 by caiming
 */
package com.netty.fileupload.handler;

import java.io.File;
import java.io.RandomAccessFile;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.DefaultFileRegion;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketframe;
import io.netty.handler.codec.http.websocketx.CloseWebSocketframe;
import io.netty.handler.codec.http.websocketx.TextWebSocketframe;
import io.netty.handler.codec.http.websocketx.WebSocketframe;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedNioFile;

/**
 * @title
 * @Description
 * @author caiming
 * @version 1.0
 * @修改记录
 * @修改序号,修改日期,修改人,修改内容
 */
public class FileWebSocketframeHandler extends SimpleChannelInboundHandlerject> {
//文件上传路经
private final static String SERVER_SAVE_PATH = “D:/“;

private static final String WEBSOCKET_PATH = “/ws“;

private final ChannelGroup group;

private WebSocketServerHandshaker handshaker;

private String fileName = “test“;

private static final File INDEX;

static {
URL location = HTTPRequestHandler.class.getProtectionDomain().getCodeSource().getLocation();// 当前class的绝对路径
String path;
try {
path = location.toURI() + “index.html“;
path = !path.contains(“file:“) ? path : path.substring(5);
INDEX = new File(path);
} catch (URISyntaxException e) {
throw new IllegalStateException(“Unable to locate index.html“ e);
}

}

public FileWebSocketframeHandler(ChannelGroup group) {
this.group = group;
}

private void handleHttpRequest(ChannelHandlerContext ctx HttpRequest req) throws Exception {
// 100-continue 是用于客户端在发送 post 数据给服务器时,征询服务器情况,看服务器是否处理 post
// 的数据,如果不处理,客户端则不上传 post 是数据,反之则上传。在实际应用中,通过 post 上传大数据时,才会使用到
// 100-continue 协议。
if (HttpUtil.is100ContinueExpected(req)) {
send100Continue(ctx);
}
RandomAccessFile file = new RandomAccessFile(INDEX “r“);
HttpResponse response = new DefaultHttpResponse(req.protocolVersion() HttpResponseStatus.OK);
response.headers().set(HttpHeaderNames.CONTENT_TYPE “text/h

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-07-23 00:51  FileUpload\
     文件        1397  2018-07-23 00:51  FileUpload\.classpath
     文件         527  2018-07-23 00:52  FileUpload\pom.xml
     文件         539  2018-07-23 00:51  FileUpload\.project
     目录           0  2018-07-23 00:51  FileUpload\target\
     目录           0  2018-07-23 00:51  FileUpload\.settings\
     文件         238  2018-07-23 00:51  FileUpload\.settings\org.eclipse.jdt.core.prefs
     文件          86  2018-07-23 00:51  FileUpload\.settings\org.eclipse.m2e.core.prefs
     目录           0  2018-07-23 00:51  FileUpload\src\
     目录           0  2018-07-23 00:51  FileUpload\target\test-classes\
     目录           0  2018-07-23 00:55  FileUpload\target\classes\
     文件        4339  2018-07-23 00:55  FileUpload\target\classes\index.html
     目录           0  2018-07-23 00:51  FileUpload\src\test\
     目录           0  2018-07-23 00:51  FileUpload\src\main\
     目录           0  2018-07-23 00:53  FileUpload\target\classes\com\
     目录           0  2018-07-23 00:51  FileUpload\src\test\resources\
     目录           0  2018-07-23 00:51  FileUpload\src\test\java\
     目录           0  2018-07-23 00:55  FileUpload\src\main\resources\
     文件        4339  2018-07-20 06:16  FileUpload\src\main\resources\index.html
     目录           0  2018-07-23 00:53  FileUpload\src\main\java\
     目录           0  2018-07-23 00:53  FileUpload\target\classes\com\netty\
     目录           0  2018-07-23 00:53  FileUpload\src\main\java\com\
     目录           0  2018-07-23 00:53  FileUpload\target\classes\com\netty\fileupload\
     目录           0  2018-07-23 00:53  FileUpload\src\main\java\com\netty\
     目录           0  2018-07-23 00:56  FileUpload\target\classes\com\netty\fileupload\server\
     文件         710  2018-07-23 00:56  FileUpload\target\classes\com\netty\fileupload\server\WebSocketServer$1.class
     文件        1614  2018-07-23 00:55  FileUpload\target\classes\com\netty\fileupload\server\WebSocketServerInitializer.class
     文件        3268  2018-07-23 00:56  FileUpload\target\classes\com\netty\fileupload\server\WebSocketServer.class
     目录           0  2018-07-23 00:54  FileUpload\target\classes\com\netty\fileupload\handler\
     文件        4122  2018-07-23 00:54  FileUpload\target\classes\com\netty\fileupload\handler\TextWebSocketframeHandler.class
     文件        5972  2018-07-23 00:54  FileUpload\target\classes\com\netty\fileupload\handler\HTTPRequestHandler.class
............此处省略9个文件信息

评论

共有 条评论