本文整理汇总了Java中io.netty.util.CharsetUtil类的典型用法代码示例。如果您正苦于以下问题:Java CharsetUtil类的具体用法?Java CharsetUtil怎么用?Java CharsetUtil使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CharsetUtil类属于io.netty.util包,在下文中一共展示了CharsetUtil类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: download
import io.netty.util.CharsetUtil; //导入依赖的package包/类
public static byte[] download(String url, int port, String username, String password, String remotePath,
String fileName) throws IOException {
FTPClient ftp = new FTPClient();
ftp.setConnectTimeout(5000);
ftp.setAutodetectUTF8(true);
ftp.setCharset(CharsetUtil.UTF_8);
ftp.setControlEncoding(CharsetUtil.UTF_8.name());
try {
ftp.connect(url, port);
ftp.login(username, password);// 登录
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
throw new IOException("login fail!");
}
ftp.changeWorkingDirectory(remotePath);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
try (ByteArrayOutputStream is = new ByteArrayOutputStream();) {
ftp.retrieveFile(ff.getName(), is);
byte[] result = is.toByteArray();
return result;
}
}
}
ftp.logout();
} finally {
if (ftp.isConnected()) {
ftp.disconnect();
}
}
return null;
}
示例2: sendHttpResponse
import io.netty.util.CharsetUtil; //导入依赖的package包/类
/**
* 返回http信息
* @param ctx
* @param req
* @param res
*/
private static void sendHttpResponse(
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200).
if (res.getStatus().code() != 200) {
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
HttpHeaders.setContentLength(res, res.content().readableBytes());
}
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.channel().writeAndFlush(res);
if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
示例3: getParameterMap
import io.netty.util.CharsetUtil; //导入依赖的package包/类
/**
* 获取请求参数的Map
* @param request
* @return
*/
public static Map<String, List<String>> getParameterMap(HttpRequest request){
Map<String, List<String>> paramMap = new HashMap<String, List<String>>();
HttpMethod method = request.method();
if(HttpMethod.GET.equals(method)){
String uri = request.uri();
QueryStringDecoder queryDecoder = new QueryStringDecoder(uri, CharsetUtil.UTF_8);
paramMap = queryDecoder.parameters();
}else if(HttpMethod.POST.equals(method)){
FullHttpRequest fullRequest = (FullHttpRequest) request;
paramMap = getPostParamMap(fullRequest);
}
return paramMap;
}
示例4: writeResponse
import io.netty.util.CharsetUtil; //导入依赖的package包/类
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
// Decide whether to close the connection or not.
boolean keepAlive = HttpHeaders.isKeepAlive(request);
// Build the response object.
FullHttpResponse response = new DefaultFullHttpResponse(
HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, "application/json");
if (keepAlive) {
// Add 'Content-Length' header only for a keep-alive connection.
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
// Add keep alive header as per:
// - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
// Write the response.
ctx.write(response);
return keepAlive;
}
示例5: sendHttpResponse
import io.netty.util.CharsetUtil; //导入依赖的package包/类
public void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
if (res.getStatus().code() != 200) {
ByteBuf f = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().clear();
res.content().writeBytes(f);
f.release();
}
HttpHeaders.setContentLength(res, res.content().readableBytes());
ChannelFuture f1;
f1 = ctx.channel().writeAndFlush(res);
if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
f1.addListener(ChannelFutureListener.CLOSE);
}
}
示例6: hashToBase64
import io.netty.util.CharsetUtil; //导入依赖的package包/类
public static String hashToBase64(ByteBuf objectState) {
ByteBuffer bf = objectState.internalNioBuffer(objectState.readerIndex(), objectState.readableBytes());
long h1 = LongHashFunction.farmUo().hashBytes(bf);
long h2 = LongHashFunction.xx().hashBytes(bf);
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE);
try {
buf.writeLong(h1).writeLong(h2);
ByteBuf b = Base64.encode(buf);
try {
String s = b.toString(CharsetUtil.UTF_8);
return s.substring(0, s.length() - 2);
} finally {
b.release();
}
} finally {
buf.release();
}
}
示例7: doDispatcher
import io.netty.util.CharsetUtil; //导入依赖的package包/类
/**
* 请求分发与处理
*
* @param request http协议请求
* @return 处理结果
* @throws InvocationTargetException 调用异常
* @throws IllegalAccessException 参数异常
*/
public Object doDispatcher(FullHttpRequest request) throws InvocationTargetException, IllegalAccessException {
Object[] args;
String uri = request.uri();
if (uri.endsWith("favicon.ico")) {
return "";
}
AceServiceBean aceServiceBean = Context.getAceServiceBean(uri);
AceHttpMethod aceHttpMethod = AceHttpMethod.getAceHttpMethod(request.method().toString());
ByteBuf content = request.content();
//如果要多次解析,请用 request.content().copy()
QueryStringDecoder decoder = new QueryStringDecoder(uri);
Map<String, List<String>> requestMap = decoder.parameters();
Object result = aceServiceBean.exec(uri, aceHttpMethod, requestMap, content == null ? null : content.toString(CharsetUtil.UTF_8));
String contentType = request.headers().get("Content-Type");
if (result == null) {
ApplicationInfo mock = new ApplicationInfo();
mock.setName("ace");
mock.setVersion("1.0");
mock.setDesc(" mock !!! ");
result = mock;
}
return result;
}
示例8: sendHttpResponse
import io.netty.util.CharsetUtil; //导入依赖的package包/类
private static void sendHttpResponse(ChannelHandlerContext ctx,
HttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200).
if (res.getStatus().code() != 200) {
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(),
CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
setContentLength(res, res.content().readableBytes());
}
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.writeAndFlush(res);
if (!isKeepAlive(req) || res.getStatus().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
示例9: processProtocolInitFrame
import io.netty.util.CharsetUtil; //导入依赖的package包/类
private void processProtocolInitFrame(ByteBuf buffer, List<Object> out) {
if (buffer.readableBytes() >= 8) {
CharSequence protocolName = buffer.readCharSequence(4, CharsetUtil.US_ASCII);
buffer.skipBytes(1);
byte majorVersion = buffer.readByte();
byte minorVersion = buffer.readByte();
byte revision = buffer.readByte();
if (!AMQP_PROTOCOL_IDENTIFIER.equals(protocolName)) {
out.add(new AmqpBadMessage(new IllegalArgumentException("Unknown protocol name " +
protocolName.toString())));
currentState = State.BAD_MESSAGE;
}
out.add(new ProtocolInitFrame(majorVersion, minorVersion, revision));
}
}
示例10: requestData
import io.netty.util.CharsetUtil; //导入依赖的package包/类
public void requestData(String data) {
if (ctx != null) {
try {
ChannelFuture f = ctx.writeAndFlush(Unpooled.copiedBuffer(data, CharsetUtil.UTF_8)).sync();
if (!f.isSuccess())
try {
throw f.cause();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
示例11: channelRead
import io.netty.util.CharsetUtil; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf in = (ByteBuf) msg;
String sentData = in.toString(CharsetUtil.UTF_8);
String returnee = sentData + "-::=::-" + "{}";
RequestProcessor reprocessor = EchoServer.process(sentData);
if (reprocessor != null)
returnee = sentData + "-::=::-" + reprocessor.getProcessedData();
ChannelFuture f = ctx.writeAndFlush(Unpooled.copiedBuffer(returnee, CharsetUtil.UTF_8)).sync();
if (!f.isSuccess())
try {
throw f.cause();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
示例12: end
import io.netty.util.CharsetUtil; //导入依赖的package包/类
/**
* End the response immediately
*
* @param data data to send
* @param options more options, specify the second arg of a valid encoding option, e.g `gzip`, `deflate`
*/
public void end(String data, String[]... options) {
header(CONTENT_LENGTH, Integer.toString(data.length()));
if (httpResponse == null) {
setHttpResponse(new DefaultFullHttpResponse(
HTTP_1_1,
getStatus(),
Unpooled.copiedBuffer(data, CharsetUtil.UTF_8)
));
}
writeFlush(!keepAlive);
}
示例13: httpPostJson
import io.netty.util.CharsetUtil; //导入依赖的package包/类
public byte[] httpPostJson(String url,String json) throws Exception
{
HttpURLConnection conn=buildConn(url);
try {
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type","application/json");
conn.getOutputStream().write(json.getBytes(CharsetUtil.UTF_8));
conn.getOutputStream().flush();
conn.getOutputStream().close();
return InputStreamUtils.getBytes(conn.getInputStream());
} finally {
conn.getInputStream().close();
conn.disconnect();
}
}
示例14: readString
import io.netty.util.CharsetUtil; //导入依赖的package包/类
@Override
public String readString(ByteBuf source) {
int len = readUnsignedShort(source);
String str = source.toString(source.readerIndex(), len, CharsetUtil.UTF_8);
source.readerIndex(source.readerIndex() + len);
return str;
}
示例15: readLongString
import io.netty.util.CharsetUtil; //导入依赖的package包/类
@Override
public String readLongString(ByteBuf source) {
int len = readInt(source);
String str = source.toString(source.readerIndex(), len, CharsetUtil.UTF_8);
source.readerIndex(source.readerIndex() + len);
return str;
}