Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've been able to stream data to my third-party API using the following Scala controller code:</p> <pre><code>def uploadFile() = Action( parse.multipartFormData(myPartHandler) ) { request =&gt; Ok("Done") } def myPartHandler: BodyParsers.parse.Multipart.PartHandler[MultipartFormData.FilePart[Result]] = { parse.Multipart.handleFilePart { case parse.Multipart.FileInfo(partName, filename, contentType) =&gt; //Still dirty: the path of the file is in the partName... String path = partName; //Set up the PipedOutputStream here, give the input stream to a worker thread val pos:PipedOutputStream = new PipedOutputStream(); val pis:PipedInputStream = new PipedInputStream(pos); val worker:UploadFileWorker = new UploadFileWorker(path,pis); worker.contentType = contentType.get; worker.start(); //Read content to the POS Iteratee.fold[Array[Byte], PipedOutputStream](pos) { (os, data) =&gt; os.write(data) os }.mapDone { os =&gt; os.close() Ok("upload done") } } } </code></pre> <p>The UploadFileWorker is a really simple Java class that contains the call to the thrid-party API.</p> <pre><code>public class UploadFileWorker extends Thread { String path; PipedInputStream pis; public String contentType = ""; public UploadFileWorker(String path, PipedInputStream pis) { super(); this.path = path; this.pis = pis; } public void run() { try { myApi.store(pis, path, contentType); pis.close(); } catch (Exception ex) { ex.printStackTrace(); try {pis.close();} catch (Exception ex2) {} } } </code></pre> <p>}</p> <p>It's not completely perfect because I would have preferred to recover the path as a parameter to the Action but I haven't been able to do so. I thus have added a piece of javascript that updates the name of the input field (and thus the partName) and it does the trick.</p>
    singulars
    1. This table or related slice is empty.
    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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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