5

我正在尝试将Panda与我的 GWT 应用程序一起使用。我可以使用直接将视频上传到我的熊猫服务器

POST MY_PANDA_SERVER/videos/MY_VIDEO_ID/upload

但是我想将我的熊猫服务器隐藏在我的 J2EE (glassfish) 服务器后面。我想实现这一点:

  1. 开始上传到我的 J2EE 服务器上的一些 servlet
  2. 验证用户
  3. 将文件发布到我的熊猫服务器,同时仍然上传到 servlet

理想情况下,我不想将文件存储在 J2EE 服务器上,而只是将其用作代理以访问 panda 服务器。

4

4 回答 4

7

Commons FileUpload 很好,但在您的情况下还不够。在提供文件项(和流)之前,它将解析内存中的整个正文。您对单个项目不感兴趣。您基本上只想将请求正文从一侧透明地流式传输到另一侧,而无需以任何方式更改或将其存储在内存中。FileUpload 只会将请求主体解析为一些“可用”的 Java 对象,而 HttpClient 只会基于这些 Java 对象再次创建相同的请求主体。这些 Java 对象也会消耗内存。

您不需要为此使用库(或者它必须是Commons IOfor才能用 oneliner替换循环IOUtils#copy())。只需基本的 Java NET 和 IO API 就足够了。这是一个启动示例:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    URLConnection connection = new URL("http://your.url.to.panda").openConnection();
    connection.setDoOutput(true); // POST.
    connection.setRequestProperty("Content-Type", request.getHeader("Content-Type")); // This one is important! You may want to check other request headers and copy it as well.

    // Set streaming mode, else HttpURLConnection will buffer everything.
    int contentLength = request.getContentLength();
    if (contentLength > -1) {
        // Content length is known beforehand, so no buffering will be taken place.
        ((HttpURLConnection) connection).setFixedLengthStreamingMode(contentLength);
     } else {
        // Content length is unknown, so send in 1KB chunks (which will also be the internal buffer size).
        ((HttpURLConnection) connection).setChunkedStreamingMode(1024);
    }

    InputStream input = request.getInputStream();
    OutputStream output = connection.getOutputStream();
    byte[] buffer = new byte[1024]; // Uses only 1KB of memory!

    for (int length = 0; (length = input.read(buffer)) > 0;) {
        output.write(buffer, 0, length);
        output.flush();
    }

    output.close();
    connection.getInputStream(); // Important! It's lazily executed.
}
于 2010-03-23T14:01:51.733 回答
0

您可以使用apache commons 文件上传来接收文件。然后您可以使用http 客户端通过 POST 将文件上传到您的熊猫服务器。使用 apache commons 文件上传,您可以在内存中处理文件,因此您不必存储它。

于 2010-03-18T17:52:58.467 回答
0

基于 Enrique 的回答,我还建议使用 FileUpload 和 HttpClient。FileUpload 可以为您提供上传文件的

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();

// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
    FileItemStream item = iter.next();
    String name = item.getFieldName();
    InputStream stream = item.openStream();
    if (item.isFormField()) {
        System.out.println("Form field " + name + " with value "
            + Streams.asString(stream) + " detected.");
    } else {
        System.out.println("File field " + name + " with file name "
            + item.getName() + " detected.");
        // Process the input stream
        ...
    }
}

然后,您可以使用HttpClientHttpComponents来执行 POST。你可以在这里找到一个例子。

于 2010-03-23T10:54:23.073 回答
0

The best solution is to use apache-camel servlet component: http://camel.apache.org/servlet.html

于 2015-12-22T00:11:20.953 回答