Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Using custom multipart body parser that uses specified dir to save files

View Application.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
package controllers;
 
import java.io.File;
import java.util.List;
 
import play.mvc.BodyParser;
import play.mvc.BodyParser.Of;
import play.mvc.Controller;
import play.mvc.Http.MultipartFormData;
import play.mvc.Http.MultipartFormData.FilePart;
import play.mvc.Http.RequestBody;
import play.mvc.Result;
import utils.CustomParsers;
import views.html.index;
 
public class Application extends Controller {
 
public static class PermanentMultipartFormData implements BodyParser {
 
//location to save the files
private final File dir = new File("data");
 
@Override
public play.api.mvc.BodyParser<RequestBody> parser(int maxLength) {
return CustomParsers.permanentMultipartFormData(dir, maxLength);
}
}
 
@Of(PermanentMultipartFormData.class)
public static Result upload() {
MultipartFormData md = request().body().asMultipartFormData();
//do your thing
}
 
public static Result index() {
return ok(index.render("Your new application is ready."));
}
 
}
View Application.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
package utils
 
import java.io.File
import play.api.mvc._
import play.mvc.Http.RequestBody
import play.api.mvc.BodyParsers._
import parse._
import play.core.j.JavaParsers.DefaultRequestBody
import play.api.libs.iteratee.Iteratee
import play.api.mvc.MultipartFormData._
import play.api.libs.Files.TemporaryFile
import java.io.FileOutputStream
import scala.collection.JavaConverters._
 
object CustomParsers {
 
case class MultipartRequestBody(multipart: MultipartFormData[File]) extends RequestBody {
override lazy val asMultipartFormData = {
new play.mvc.Http.MultipartFormData {
lazy val asFormUrlEncoded = {
multipart.asFormUrlEncoded.mapValues(_.toArray).asJava
}
lazy val getFiles = {
multipart.files.map { file =>
new play.mvc.Http.MultipartFormData.FilePart(
file.key, file.filename, file.contentType.orNull, file.ref)
}.asJava
}
}
}
}
private def orDefault(maxLength: Int) = if (maxLength < 0) parse.DEFAULT_MAX_TEXT_LENGTH else maxLength
 
type PartHandler[A] = PartialFunction[Map[String, String], Iteratee[Array[Byte], A]]
 
def permanentMultipartFormData(dir: File, maxLength: Int): BodyParser[RequestBody] = {
parse.maxLength(orDefault(maxLength), parse.multipartFormData(handleFilePartAsPermanentFile(dir))).map {
_.fold(
_ => DefaultRequestBody(isMaxSizeExceeded = true),
multipart =>
MultipartRequestBody(multipart))
}
}
 
def handleFilePartAsPermanentFile(dir: File): PartHandler[FilePart[File]] = {
Multipart.handleFilePart {
case Multipart.FileInfo(partName, filename, contentType) =>
val file = new File(dir, filename)
Iteratee.fold[Array[Byte], FileOutputStream](new java.io.FileOutputStream(file)) { (os, data) =>
os.write(data)
os
}.map { os =>
os.close()
file
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.