| New file |
| | |
| | | package cn.gistack.by.nettyUpload.utils; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import io.netty.buffer.Unpooled; |
| | | import io.netty.channel.ChannelFutureListener; |
| | | import io.netty.channel.ChannelHandlerContext; |
| | | import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| | | import io.netty.handler.codec.http.FullHttpResponse; |
| | | import io.netty.handler.codec.http.HttpResponseStatus; |
| | | import io.netty.util.CharsetUtil; |
| | | |
| | | import java.util.Map; |
| | | |
| | | import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE; |
| | | import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; |
| | | |
| | | /** |
| | | * response工具 |
| | | * @author chenhaijian |
| | | * @date 2020-05-21 17:11 |
| | | */ |
| | | public class HttpResponseUtils { |
| | | |
| | | /** |
| | | * 向客户端发送消息提示 |
| | | * @param ctx |
| | | * @param status |
| | | */ |
| | | public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { |
| | | FullHttpResponse response = new DefaultFullHttpResponse( |
| | | HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); |
| | | response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); |
| | | |
| | | ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| | | } |
| | | |
| | | /** |
| | | * 向客户端发送消息提示 |
| | | * @param ctx |
| | | * @param status |
| | | * @param message |
| | | */ |
| | | public static void sendResponseJson(ChannelHandlerContext ctx, HttpResponseStatus status, String message) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("code", status.code()); |
| | | jsonObject.put("msg", message); |
| | | |
| | | FullHttpResponse response = new DefaultFullHttpResponse( |
| | | HTTP_1_1, status, Unpooled.copiedBuffer(jsonObject.toJSONString(), CharsetUtil.UTF_8)); |
| | | response.headers().set(CONTENT_TYPE, "application/json; charset=UTF-8"); |
| | | |
| | | ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| | | } |
| | | |
| | | public static void sendResponseJson2(ChannelHandlerContext ctx, HttpResponseStatus status, |
| | | String message,Map<String, Object> map) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("code", status.code()); |
| | | jsonObject.put("msg", message); |
| | | jsonObject.put("data", map); |
| | | |
| | | FullHttpResponse response = new DefaultFullHttpResponse( |
| | | HTTP_1_1, status, Unpooled.copiedBuffer(jsonObject.toJSONString(), CharsetUtil.UTF_8)); |
| | | response.headers().set(CONTENT_TYPE, "application/json; charset=UTF-8"); |
| | | |
| | | ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| | | } |
| | | } |