file upload to remote server using play framework

1,695 views
Skip to first unread message

sowmya yalamchili

unread,
Jun 14, 2016, 3:01:38 AM6/14/16
to play-framework

Hi,
Am new to scala & play. Can someone help me for file upload to remote server. Am able to upload file, but looking for uploading file to remote server.

I am trying to use WSClient as per below link, but am not succesful yet
https://www.playframework.com/documentation/2.5.x/ScalaWS

working examples will help please. Am using play 2.2.0 with Scala 2.10.2 and sbt 0.13.11

Greg Methvin

unread,
Jun 14, 2016, 4:46:36 AM6/14/16
to play-framework
If you're new to Scala and Play, I'd suggest starting with Play 2.5, since now multipart file upload is fully supported in Play WS. If you can't do that, this comment describes one possible solution. You may need to adapt that code to Play 2.2 though.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/d1f80a8e-e7a9-474b-be62-d75562a9a79b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

sowmya

unread,
Jun 14, 2016, 5:13:31 AM6/14/16
to play-framework
Hi Greg,

Thanks for the response.
 
Sure i will continue further discussion in this thread only.

yes, you are right. 've used example from play 2.5 documentation. Actually am using play plugin 2.2.2 from sbt version 0.13.11. Am not sure if 've to install anyother version of sbt to use play 2.5 without installing play.



Greg Methvin

unread,
Jun 14, 2016, 5:22:35 AM6/14/16
to play-framework
Do you need to use play 2.2 because you have an existing application developed in it? Or are you starting a new app from scratch?

SBT 0.13.11 should be fine. You can use activator to create a new app from a template, using "activator new". The "play" executable has been replaced by "activator" since 2.3, but once you create your project you can just use sbt.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

sowmya

unread,
Jun 14, 2016, 6:46:04 AM6/14/16
to play-framework
Am starting new app from scratch, so i will upgrade to play 2.5 and try. Thanks for the help.

sowmya

unread,
Jun 15, 2016, 3:12:52 AM6/15/16
to play-framework

Hi Greg,

   Actually requirement is to upload a file received from play web service directly to hdfs server. any docs or sample code to help ? 

Will Sargent

unread,
Jun 15, 2016, 4:54:07 AM6/15/16
to play-fr...@googlegroups.com
The documentation link you have is for 2.5, so the link for 2.2.0 is:


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

sowmya

unread,
Jun 15, 2016, 5:56:27 AM6/15/16
to play-framework
Thanks for the document Will.
 
Actually i did upgrade to play 2.5.0 & scala 2.11, when i try to compile project gettign the following errors:
[fl] $ compile
[info] Updating {file:/scratch/sowm/gnosis/fl/}root...
[info] Resolving jline#jline;2.12.1 ...
[info]  [SUCCESSFUL ] com.typesafe.play#play_2.11;2.5.0!play_2.11.jar (2518ms)

rta...@twitter.com

unread,
Jun 15, 2016, 12:12:32 PM6/15/16
to play-framework
Are you otherwise able tor each repo1.maven.org?  That's probably a local network issue.

Try 'ping repo1.maven.org' and report back the results.

sowmya

unread,
Jun 15, 2016, 2:05:03 PM6/15/16
to play-framework
yeah it worked after setting JAVA_OPTS. Thanks.

sowmya

unread,
Jun 16, 2016, 10:47:15 AM6/16/16
to play-framework
Finally i got the play 2.5.0 with activator and trying to upload a file to a remote server using Scala WS, i just tried this and it throwing not found : value source error

val request1: WSRequest = ws.url("actual url").post(Source(FilePart("hello", "hello.txt", Option("text/plain"), FileIO.fromFile(tmpFile)) :: DataPart("key", "value") :: List()))

Am not sure if am missing something.

Greg Methvin

unread,
Jun 16, 2016, 11:21:01 AM6/16/16
to play-framework
import akka.stream.scaladsl.Source?

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

sowmya

unread,
Jun 16, 2016, 11:37:32 AM6/16/16
to play-framework
Thanks.

i did above said import and that issue is resolved and now not found: value FilePart ,

Again missing something.

Greg Methvin

unread,
Jun 16, 2016, 11:44:26 AM6/16/16
to play-framework
That is play.api.mvc.MultipartFormData.FilePart. You probably should import akka.stream.scaladsl._ since FileIO is also there. If you have an IDE you can usually search for those classes and find them in Akka or Play.

The documentation at https://www.playframework.com/documentation/2.5.x/ScalaWS#Submitting-multipart/form-data should probably include an example with all the imports

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

sowmya

unread,
Jun 16, 2016, 12:16:07 PM6/16/16
to play-framework

yeah i did, one more clarification please, Actually am trying to understand the below syntax and 

 1.) "hello.txt" is the file to be uploaded right?.

 2.) Whats the tmpFile?.

Christian Schmitt

unread,
Jun 16, 2016, 1:26:04 PM6/16/16
to play-framework
Actually tmpFile is the java.io.File object you want to upload.

sowmya

unread,
Jun 16, 2016, 2:27:17 PM6/16/16
to play-framework
Thanks.

Below error is thrown @  request.body.file("picture").map { picture =>

 type mismatch;
found : Option[play.api.mvc.Result] required: play.api.mvc.Result

@Singleton
class Application @Inject() extends Controller {


Controller:   Please check what am i doing wrong here.
----------------
    def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

    def upload = Action(parse.multipartFormData) { request =>
    request.body.file("picture").map { picture =>
     val filename = picture.filename

     WS.url("url").post(Source(FilePart("hello", "$filename", Option("text/plain"), FileIO.fromFile(picture.ref.file)) :: DataPart("key", "value") :: List()))
     Ok("File uploaded")

  }
 }

}

sowmya

unread,
Jun 19, 2016, 2:56:11 PM6/19/16
to play-framework
Finally all the comoilation issues have been resolved, but hitting "Missing File". I think only filename is passed instead of filename with path, psting controller code here, please help me to uplaod the file
 
@Singleton
class Application @Inject()(ws: WSClient) extends Controller {



    def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

    def upload = Action(parse.multipartFormData) { request =>
  request.body.file("file").map { file =>
    import java.io.File
    val filename = file.filename
    val contentType = file.contentType
    val url = "url"
    val user = "abcd";
    val password = "xxxxx";



    println(filename)
    println(file.ref.file)

    ws.url("url")
      .post(Source(FilePart("file", filename, Option("text/plain"), FileIO.fromFile(file.ref.file)) :: DataPart("key", "value") :: List())).map { response =>
       println(response.body)
         Ok(response.body)
       }

  
    Ok("File uploaded")
  }.getOrElse {
     Ok( "Missing file")
  }

}

sowmya

unread,
Jun 20, 2016, 5:16:28 AM6/20/16
to play-framework
Am still not thru, please help me to resolve the issue. whats wrong in the above code. 

sowmya

unread,
Jun 20, 2016, 1:50:49 PM6/20/16
to play-framework
Finally am getting response status 200, but the file is not uploaded. I can see the temporary file with the correct data, but file is not uploaded. Looks like am really missing something, Please help:

Here is upload def:

 def upload = Action(BodyParsers.parse.multipartFormData) { request =>
  request.body.file("file").map { file =>

    val filename = file.filename
    val contentType = file.contentType
    val url = s"http://<host>:9000/"
    val tmpFile = file.ref.file
    //println("file: "+filename)
    //println("tmpFile: "+tmpFile)

      ws.url(url)
        .post(Source(FilePart("file", filename, Option("text/plain"), FileIO.fromFile(tmpFile)) :: DataPart("key", "value") :: List())).map { response =>
         println("X:  "+ response.status)
         println("X**********************:  "+ response.body)
         Ok(response.body)
       }

    Ok("File uploaded 1")
  }.getOrElse {
      Redirect(routes.Application.index).flashing(
      "error" -> "Missing file")
     //Ok( "Missing file 1")
  }

}

Christian Schmitt

unread,
Jun 20, 2016, 2:27:58 PM6/20/16
to play-framework
Should probably be:

 def upload = Action(BodyParsers.parse.multipartFormData).async { request =>
  request.body.file("file").map { file =>

    val filename = file.filename
    val contentType = file.contentType
    val url = s"http://<host>:9000/"
    val tmpFile = file.ref.file

      ws.url(url)
        .post(Source(FilePart("file", filename, Option("text/plain"), FileIO.fromFile(tmpFile)) :: DataPart("key", "value") :: List())).map { response =>
         println("X:  "+ response.status)
         println("X**********************:  "+ response.body)
         Ok(response.body)
       }
  }.getOrElse {
      Future.successful(Redirect(routes.Application.index).flashing("error" -> "Missing file"))
  }

}

Also you should probably take a little bit more time in learning scala and looking at the play! docs.
Especially: https://www.playframework.com/documentation/2.5.x/ScalaAsync and http://docs.scala-lang.org/overviews/core/futures.html

sowmya

unread,
Jun 21, 2016, 6:43:08 AM6/21/16
to play-framework
Thanks for the details.
 
Am very new to Scala, play and Java too.. definitely need to read. 

 I did change the upload def as suggested by you & with little reading: while testing it (size of sample file to be uploaded is 4KB)

1. Prompts for the input file
2. once the file has been selected and clicked 'submit'
3. Progress indicator shows in progress and finally  times out and throws 'Request timeout to www-proxy.xx.yyy.com/xxx.xx.xx.xx:80 after 120000ms'
4 When in progress i see so many tmp files of the file to be uploaded are getting populated in host:/tmp/playtempxxxxx..... folder

Now doubts are:

1. why there are so many copies of tmp files have been created continuously?. - in my case nearly 1310 temp files have been created with the same content of the file to be uploaded



Controller code:
class Application @Inject()(ws: WSClient) extends Controller {



    def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

    def upload = Action.async(BodyParsers.parse.multipartFormData) { implicit request =>


        val file = request.body.file("file").head
        val filename = file.filename
        val contentType = file.contentType
        val url = s"http:/<host>:9000/"
        val tmpFile = file.ref.file

      ws.url(url)
        .post(Source(FilePart("file", filename, Option("text/plain"), FileIO.fromFile(tmpFile)) :: DataPart("key", "value") :: List())).map(response => Ok("Got result: " + response.body))
                .recover{ case e: Exception => InternalServerError(e.getMessage) }

       
   }
}



Reply all
Reply to author
Forward
0 new messages