Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think this is possible in Java, but I've managed to put together a simple Scala version. You can still use Java for all other parts of you project.</p> <p>But be aware that it's just a crude demo, focussing on the "simultaneous up- and download" part and ignoring everything else. I'm not even sure that this is the <em>right</em> way to do it, but it seems to work.</p> <p>Basically, what you want to do is to use <code>Concurrent.broadcast</code> to create a input/output stream pair. The output part (<code>Enumeratee</code>) is streamed to the client. Then, you need a custom <code>PartHandler</code> which takes the uploaded data as it arrives, and feeds it into the the input part (<code>Concurrent.Channel</code>).</p> <p>Please not that, in order for this example to work, you have to go to the download page first, <strong>then</strong> start uploading a file. </p> <h2>sample Application.scala</h2> <pre><code>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 -&gt; (fileStream, fileInput) Ok.stream { fileStream.onDoneEnumerating(streams -= id) } } def upload = Action(parse.multipartFormData(myPartHandler)) { request =&gt; Ok("banana!") } def myPartHandler: BodyParsers.parse.Multipart.PartHandler[MultipartFormData.FilePart[Result]] = { parse.Multipart.handleFilePart { case parse.Multipart.FileInfo(partName, filename, contentType) =&gt; val (thisStream, thisInput) = streams(partName) Iteratee.fold[Array[Byte], Concurrent.Channel[Array[Byte]]](thisInput) { (inp, data) =&gt; inp.push(data) inp }.mapDone { inp =&gt; inp.push(Input.EOF) Ok("upload Done") } } } } </code></pre> <h2>sample index.scala.html</h2> <pre><code>&lt;p&gt; &lt;a href="@routes.Application.download("123456")"&gt;Download&lt;/a&gt; &lt;/p&gt; &lt;form action="@routes.Application.upload" method="post" enctype="multipart/form-data"&gt; &lt;input type="file" name="123456"&gt; &lt;input type="submit"&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload