也許這可以幫助你。 這實際上是如何通過表單發送和存儲MP3(base64)到PLAY框架。我希望這能幫助你一點:
main.scala.html
<!-- AUDIO -->
<section id="sound">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<form action="**@routes.Application.upload()**" name="addProductForm" id="addProductForm" enctype="multipart/form-data" method="post">
<label>Select file.
<input name="base64" type="text" accept="*/*" value="data:audio/mp3;base64,//sQxAAABKh...JZsdf2y/FOtQ==" >
<!-- START This is actually not needed.Because you dont want use the uploader itself -->
<input name="picture" type="file" accept="*/*" >
<!-- ENDE This is actually not needed.Because you dont want use the uploader itself -->
</label>
<input name="submit" type="submit" size="50" value="senden">
</form>
</div>
</div>
</div>
</section>
<!-- AUDIO -->
非常重要的,使用文件實用程序在Java中只需安裝org.apache.common.io.FileUtils(!!!)
應用。 java的
public static Result upload(){
System.out.println("Entered the upload() method !!!");
play.mvc.Http.MultipartFormData body = request().body().asMultipartFormData();
DynamicForm dynamicForm = Form.form().bindFromRequest();
play.mvc.Http.MultipartFormData.FilePart picture = body.getFile("picture");
String str = dynamicForm.get("base64");
// example: "data:audio/mp3;base64,//sQxAAABKhrG...f2y/FOtQ==";
String[] strsplited = str.split(",");
String b64 = strsplited[1];
if (picture != null) {
File file = picture.getFile();
String fileName = file.getName();
System.out.println("\n\n"+file.getPath()+"/"+fileName);
try {
/* START This is actually not needed.Because you dont want use the uploader itself */
File destination = new File("/Applications/MAMP/htdocs/play_old/myapp/public/upload/", file.getName());
FileUtils.moveFile(file,destination);
/* ENDE This is actually not needed.Because you dont want use the uploader itself */
java.security.SecureRandom random = new java.security.SecureRandom();
String newFileName = new BigInteger(130, random).toString(32);
byte[] data = Base64.getDecoder().decode(b64);
File dest = new File("/Applications/MAMP/htdocs/play_old/myapp/public/upload/", newFileName+".mp3");
FileUtils.writeByteArrayToFile(dest,data);
System.out.println("Byte size: "+data.length);
System.out.println("Create new mp3 file :"+bi+".mp3");
} catch(IOException e){
e.getMessage();
}
} else {
flash("error", "Missing file");
return badRequest();
}
return ok("File uploaded");
}
路線
POST /upload controllers.Application.upload()