Play2 を使用して、ファイルの POST を受信し、受信したファイルを処理し、POST がまだファイルを転送している間に別のユーザーからの GET 要求に結果を送信するサーバーを実装したいと考えています。Play2でこれを行うことは可能ですか? 私はすでに Play2 を使用してユーザーを認証しているので、このトランザクションの認証を Play2 で処理したいと考えています。私は Java を使用していますが、必要に応じて Scala を使用できます。もしそうなら、私は何を調べるべきですか?そうでない場合は、いくつかの Java NIO フレームワークを調べて、別のサーバーを実装する必要があると思います。
1 に答える
1
これは Java では不可能だと思いますが、簡単な Scala バージョンをまとめることができました。プロジェクトの他のすべての部分には引き続き Java を使用できます。
ただし、これは単なる大雑把なデモであり、「同時のアップとダウンロード」の部分に焦点を当て、他のすべてを無視していることに注意してください。これが正しい方法かどうかはわかりませんが、うまくいくようです。
基本的に、やりたいことはConcurrent.broadcast
、入力/出力ストリームのペアを作成するために使用することです。出力部分 ( Enumeratee
) がクライアントにストリーミングされます。PartHandler
次に、アップロードされたデータが到着したときに取得し、それを入力部分 ( ) にフィードするカスタムが必要ですConcurrent.Channel
。
この例を機能させるには、まずダウンロード ページに移動してから、ファイルのアップロードを開始する必要があります。
サンプル Application.scala
package controllers
import play.api._
import libs.iteratee.{Enumerator, Input, Iteratee, Concurrent}
import play.api.mvc._
import scala.collection.concurrent
object Application extends Controller {
val streams = new concurrent.TrieMap[String,(Enumerator[Array[Byte]], Concurrent.Channel[Array[Byte]])]
def index = Action {
Ok(views.html.index())
}
def download(id: String) = Action {
val (fileStream, fileInput) = Concurrent.broadcast[Array[Byte]]
streams += id -> (fileStream, fileInput)
Ok.stream {
fileStream.onDoneEnumerating(streams -= id)
}
}
def upload = Action(parse.multipartFormData(myPartHandler)) {
request => Ok("banana!")
}
def myPartHandler: BodyParsers.parse.Multipart.PartHandler[MultipartFormData.FilePart[Result]] = {
parse.Multipart.handleFilePart {
case parse.Multipart.FileInfo(partName, filename, contentType) =>
val (thisStream, thisInput) = streams(partName)
Iteratee.fold[Array[Byte], Concurrent.Channel[Array[Byte]]](thisInput) { (inp, data) =>
inp.push(data)
inp
}.mapDone { inp =>
inp.push(Input.EOF)
Ok("upload Done")
}
}
}
}
サンプル index.scala.html
<p>
<a href="@routes.Application.download("123456")">Download</a>
</p>
<form action="@routes.Application.upload" method="post" enctype="multipart/form-data">
<input type="file" name="123456">
<input type="submit">
</form>
于 2013-02-06T20:17:37.947 に答える