Example usage for io.netty.handler.codec.http.multipart DiskFileUpload getFilename

List of usage examples for io.netty.handler.codec.http.multipart DiskFileUpload getFilename

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.multipart DiskFileUpload getFilename.

Prototype

@Override
    public String getFilename() 

Source Link

Usage

From source file:cc.blynk.core.http.handlers.UploadHandler.java

License:Apache License

private String finishUpload() throws Exception {
    String pathTo = null;//from  w  w  w.ja v  a2  s .  com
    try {
        while (decoder.hasNext()) {
            InterfaceHttpData data = decoder.next();
            if (data != null) {
                if (data instanceof DiskFileUpload) {
                    DiskFileUpload diskFileUpload = (DiskFileUpload) data;
                    Path tmpFile = diskFileUpload.getFile().toPath();
                    String uploadedFilename = diskFileUpload.getFilename();
                    String extension = "";
                    if (uploadedFilename.contains(".")) {
                        extension = uploadedFilename.substring(uploadedFilename.lastIndexOf("."),
                                uploadedFilename.length());
                    }
                    String finalName = tmpFile.getFileName().toString() + extension;

                    //this is just to make it work on team city.
                    Path staticPath = Paths.get(staticFolderPath, uploadFolder);
                    if (!Files.exists(staticPath)) {
                        Files.createDirectories(staticPath);
                    }

                    Files.move(tmpFile, Paths.get(staticFolderPath, uploadFolder, finalName),
                            StandardCopyOption.REPLACE_EXISTING);
                    pathTo = uploadFolder + finalName;
                }
            }
        }
    } catch (EndOfDataDecoderException endOfData) {
        //ignore. that's fine.
    } finally {
        // destroy the decoder to release all resources
        decoder.destroy();
        decoder = null;
    }

    return pathTo;
}

From source file:org.apache.flink.runtime.webmonitor.HttpRequestHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    try {/*from w w w  . j  ava2s  .c  o m*/
        if (msg instanceof HttpRequest) {
            currentRequest = (HttpRequest) msg;
            currentRequestPath = null;

            if (currentDecoder != null) {
                currentDecoder.destroy();
                currentDecoder = null;
            }

            if (currentRequest.getMethod() == HttpMethod.GET
                    || currentRequest.getMethod() == HttpMethod.DELETE) {
                // directly delegate to the router
                ctx.fireChannelRead(currentRequest);
            } else if (currentRequest.getMethod() == HttpMethod.POST) {
                // POST comes in multiple objects. First the request, then the contents
                // keep the request and path for the remaining objects of the POST request
                currentRequestPath = new QueryStringDecoder(currentRequest.getUri()).path();
                currentDecoder = new HttpPostRequestDecoder(DATA_FACTORY, currentRequest);
            } else {
                throw new IOException("Unsupported HTTP method: " + currentRequest.getMethod().name());
            }
        } else if (currentDecoder != null && msg instanceof HttpContent) {
            // received new chunk, give it to the current decoder
            HttpContent chunk = (HttpContent) msg;
            currentDecoder.offer(chunk);

            try {
                while (currentDecoder.hasNext()) {
                    InterfaceHttpData data = currentDecoder.next();

                    // IF SOMETHING EVER NEEDS POST PARAMETERS, THIS WILL BE THE PLACE TO HANDLE IT
                    // all fields values will be passed with type Attribute.

                    if (data.getHttpDataType() == HttpDataType.FileUpload) {
                        DiskFileUpload file = (DiskFileUpload) data;
                        if (file.isCompleted()) {
                            String name = file.getFilename();

                            File target = new File(tmpDir, UUID.randomUUID() + "_" + name);
                            file.renameTo(target);

                            QueryStringEncoder encoder = new QueryStringEncoder(currentRequestPath);
                            encoder.addParam("filepath", target.getAbsolutePath());
                            encoder.addParam("filename", name);

                            currentRequest.setUri(encoder.toString());
                        }
                    }

                    data.release();
                }
            } catch (EndOfDataDecoderException ignored) {
            }

            if (chunk instanceof LastHttpContent) {
                HttpRequest request = currentRequest;
                currentRequest = null;
                currentRequestPath = null;

                currentDecoder.destroy();
                currentDecoder = null;

                // fire next channel handler
                ctx.fireChannelRead(request);
            }
        }
    } catch (Throwable t) {
        currentRequest = null;
        currentRequestPath = null;

        if (currentDecoder != null) {
            currentDecoder.destroy();
            currentDecoder = null;
        }

        if (ctx.channel().isActive()) {
            byte[] bytes = ExceptionUtils.stringifyException(t).getBytes(ENCODING);

            DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));

            response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, "utf-8");
            response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
            response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());

            ctx.writeAndFlush(response);
        }
    }
}