如何从NETTY 4中的发布请求中获取文件数据?

时间:2014-06-09 03:27:25

标签: java file-upload netty

我可以将这个带有jpg文件的POST请求发送到我的服务器

POST /formpostmultipart HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 621551
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2031.2 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryY1m4URqQ5ydALOrQ
Referer: http://localhost:8080/upload
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4

我必须做什么才能从此请求获取文件并保存到磁盘? 附:抱歉我的英文不好

2 个答案:

答案 0 :(得分:3)

我用HttpPostRequestDecoder解决了这个问题。例如

if (request.getMethod().equals(HttpMethod.POST)) {
                    decoder = new HttpPostRequestDecoder(dataFactory, request);
                    decoder.setDiscardThreshold(0);
if (decoder != null) {
        if (msg instanceof HttpContent) {
            HttpContent chunk = (HttpContent) msg;
            decoder.offer(chunk);
            readChunk(channelHandlerContext);
            if (chunk instanceof LastHttpContent) {
                resetPostRequestDecoder();
            }
        }
    }
private void readChunk(ChannelHandlerContext ctx) throws IOException {
    while (decoder.hasNext()) {
        InterfaceHttpData data = decoder.next();
        if (data != null) {
            try {
                switch (data.getHttpDataType()) {
                    case Attribute:
                        break;
                    case FileUpload:    
                        FileUpload fileUpload = (FileUpload) data;
                        File file = new File("C:\\test\\" + fileUpload.getName);
                        if (!file.exists()) {
                            file.createNewFile();
                        }
                        try (FileChannel inputChannel = new FileInputStream(fileUpload.getFile()).getChannel(); FileChannel outputChannel = new FileOutputStream(file).getChannel()) {
                            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
                            sendSimpleResponse(ctx,CREATED,"file name: " +file.getAbsolutePath());
                        }
                        break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                data.release();
            }
        }
    }
}

private void resetPostRequestDecoder() {
    request = null;
    decoder.destroy();
    decoder = null;
}

答案 1 :(得分:0)

以下是使用Netty 4.1.3.Final的示例。一些摘录取自mechanikos'回答。

管道如下:

pipeline.addLast(new HttpRequestDecoder());
pipeline.addLast(new HttpResponseEncoder());
pipeline.addLast(new MyHttUploadServerHandler());

频道阅读处理:

protected void channelRead0(final ChannelHandlerContext ctx, final HttpObject httpObject)
    throws Exception {
    if (httpObject instanceof HttpRequest) {
      httpRequest = (HttpRequest) httpObject;
      final URI uri = new URI(httpRequest.uri());

      System.out.println("Got URI " + uri);
      if (httpRequest.method() == POST) {
        httpDecoder = new HttpPostRequestDecoder(factory, httpRequest);
        httpDecoder.setDiscardThreshold(0);
      } else {
        sendResponse(ctx, METHOD_NOT_ALLOWED, null);
      }
    }
    if (httpDecoder != null) {
      if (httpObject instanceof HttpContent) {
        final HttpContent chunk = (HttpContent) httpObject;
        httpDecoder.offer(chunk);
        readChunk(ctx);

        if (chunk instanceof LastHttpContent) {
          resetPostRequestDecoder();
        }
      }
    }
  }

  private void readChunk(ChannelHandlerContext ctx) throws IOException {
    while (httpDecoder.hasNext()) {
      InterfaceHttpData data = httpDecoder.next();
      if (data != null) {
        try {
          switch (data.getHttpDataType()) {
            case Attribute:
              break;
            case FileUpload:
              final FileUpload fileUpload = (FileUpload) data;
              final File file = new File(FILE_UPLOAD_LOCN + fileUpload.getFilename());
              if (!file.exists()) {
                file.createNewFile();
              }
              System.out.println("Created file " + file);
              try (FileChannel inputChannel = new FileInputStream(fileUpload.getFile()).getChannel();
                   FileChannel outputChannel = new FileOutputStream(file).getChannel()) {
                outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
                sendResponse(ctx, CREATED, "file name: " + file.getAbsolutePath());
              }
              break;
          }
        } finally {
          data.release();
        }
      }
    }
}

可以找到完整的要点here