5

GWT アプリケーションでPandaを使用しようとしています。を使用して、ビデオをパンダサーバーに直接アップロードできます

POST MY_PANDA_SERVER/videos/MY_VIDEO_ID/upload

ただし、パンダ サーバーを J2EE (グラスフィッシュ) サーバーの背後に隠したいと考えています。私はこれを達成したいと思います:

  1. J2EE サーバー上のサーブレットへのアップロードを開始します
  2. ユーザーを認証する
  3. サーブレットへのアップロード中にファイルをパンダサーバーに POST します

理想的には、J2EE サーバーにファイルを保存せず、パンダ サーバーにアクセスするためのプロキシとして使用するだけです。

4

4 に答える 4

7

Commons FileUpload は素晴らしいですが、あなたの場合は十分ではありません。ファイル項目 (およびストリーム) を提供する前に、メモリ内の本文全体を解析します。個々のアイテムには興味がありません。基本的には、リクエスト本文を変更したりメモリに保存したりせずに、一方から他方に透過的にストリーミングしたいだけです。FileUpload はリクエスト本文を解析して「使用可能な」Java オブジェクトにするだけで、HttpClient はそれらの Java オブジェクトに基づいてまったく同じリクエスト本文を再度作成するだけです。これらの Java オブジェクトもメモリを消費します。

これにはライブラリは必要ありません(または、を使用してループをワンライナーに置き換えるにはCommons IOでなければなりません)。基本的な Java NET と IO API だけで十分です。キックオフの例を次に示します。forIOUtils#copy()

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

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
        ...
    }
}

次に、HttpClientまたはHttpComponentsを使用してPOSTを実行できます。ここに例があります。

于 2010-03-23T10:54:23.073 に答える
0

最善の解決策は、apache-camel サーブレット コンポーネントを使用することです: http://camel.apache.org/servlet.html

于 2015-12-22T00:11:20.953 に答える
0

apache commons file uploadを使用してファイルを受け取ることができます。次に、http クライアントを使用して、POST でファイルを panda サーバーにアップロードできます。Apache Commons ファイルのアップロードを使用すると、ファイルをメモリ内で処理できるため、ファイルを保存する必要がありません。

于 2010-03-18T17:52:58.467 に答える