List of usage examples for io.netty.handler.codec.http.multipart MixedFileUpload MixedFileUpload
public MixedFileUpload(String name, String filename, String contentType, String contentTransferEncoding, Charset charset, long size, long limitSize)
From source file:de.ocarthon.core.network.HttpClient.java
License:Apache License
public synchronized String postRequest(String query, List<Map.Entry<String, String>> postParameters, String filePostName, String fileName, ByteBuf fileData, String mime) { if (bootstrap == null) { setupBootstrap();//from www. j a va 2 s . c o m } if (channel == null || forceReconnect) { ChannelFuture cf = bootstrap.connect(host, port); forceReconnect = false; cf.awaitUninterruptibly(); channel = cf.channel(); channel.pipeline().addLast("handler", new SimpleChannelInboundHandler<HttpObject>() { @Override protected void messageReceived(ChannelHandlerContext ctx, HttpObject msg) throws Exception { if (msg instanceof HttpResponse) { HttpResponse response = ((HttpResponse) msg); String connection = (String) response.headers().get(HttpHeaderNames.CONNECTION); if (connection != null && connection.equalsIgnoreCase(HttpHeaderValues.CLOSE.toString())) forceReconnect = true; } if (msg instanceof HttpContent) { HttpContent chunk = (HttpContent) msg; String message = chunk.content().toString(CharsetUtil.UTF_8); if (!message.isEmpty()) { result[0] = message; synchronized (result) { result.notify(); } } } } }); } boolean isFileAttached = fileData != null && fileData.isReadable(); HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, scheme + "://" + host + ":" + port + "/" + query); HttpPostRequestEncoder bodyReqEncoder; try { bodyReqEncoder = new HttpPostRequestEncoder(httpDataFactory, request, isFileAttached); for (Map.Entry<String, String> entry : postParameters) { bodyReqEncoder.addBodyAttribute(entry.getKey(), entry.getValue()); } if (isFileAttached) { if (mime == null) mime = "application/octet-stream"; MixedFileUpload mfu = new MixedFileUpload(filePostName, fileName, mime, "binary", null, fileData.capacity(), DefaultHttpDataFactory.MINSIZE); mfu.addContent(fileData, true); bodyReqEncoder.addBodyHttpData(mfu); } HttpHeaders headers = request.headers(); headers.set(HttpHeaderNames.HOST, host); headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP); headers.set(HttpHeaderNames.USER_AGENT, "OcarthonCore HttpClient"); request = bodyReqEncoder.finalizeRequest(); } catch (Exception e) { throw new NullPointerException("key or value is empty or null"); } channel.write(request); if (bodyReqEncoder.isChunked()) { channel.write(bodyReqEncoder); } channel.flush(); synchronized (result) { try { result.wait(); } catch (InterruptedException e) { return null; } } return result[0]; }