[Play 2.5][JAVA] Moving files from temp folder in controller

49 views
Skip to first unread message

MJ

unread,
Mar 11, 2017, 8:39:09 PM3/11/17
to Play Framework
I tried to implement files uploading. Which then have to be moved to different folder from temp folder in controller.
I managed to move them with java.nio , but this is blocking operation. How should I implement it in controller?

If I define controller like this, then everything is working,but it is blocking operation.

public Result processUpload(){ Http.MultipartFormData body = request().body().asMultipartFormData();
 
if (body!=null) {
   
Http.MultipartFormData.FilePart<File> picture = body.getFile("picture");
   
Http.MultipartFormData.FilePart<File> pdf = body.getFile("pdf");
   
try {
        filesService
.copy(picture,pdf);
       
return ok(Json.toJson(new TResult(true, messages.at("upload.sucess")))));
     
} catch (ServiceExcpetion ex) {
       
return internalServerError(Json.toJson(new TResult(false,ex.translate(messages)))));
     
}
 
}
 
return internalServerError(Json.toJson(new TResult(false, messages.at("upload.fail"))));
}

Then I tried to define it like this, but my frontend fetch is always receiving 500 - Internal Server Error this way

public Result processUpload(){ Http.MultipartFormData body = request().body().asMultipartFormData();
 
if (body!=null) {
   
Http.MultipartFormData.FilePart<File> picture = body.getFile("picture");
   
Http.MultipartFormData.FilePart<File> pdf = body.getFile("pdf");
CompletableFuture.supplyAsync(() -> { try { if (filesService.copy(picture,pdf)) { return true; } else { return false; } } catch (Exception e) { throw new CompletionException(e); } },ec.current()).thenApply(result -> { if (result) { return ok(Json.toJson(new TResult(true, messages.at("upload.sucess")))); } else { return internalServerError(Json.toJson(new TResult(false,messages.at("upload.fail")))); } }).exceptionally(throwable -> { if (throwable.getCause() instanceof ServiceExcpetion) { return badRequest(Json.toJson(new TResult(false,((ServiceExcpetion)throwable.getCause()).translate(messages)))); } else { return internalServerError(Json.toJson(new TResult(false,messages.at("akcia.chybaKomunikacie")))); } } );
 }
 
return CompletableFuture.completedFuture(internalServerError(Json.toJson(new TResult(false, messages.at("upload.fail")))));
}


Can someone provide me example how to implement blocking file copy?

MJ

unread,
Mar 11, 2017, 8:44:52 PM3/11/17
to Play Framework
I forgot to add , then my frontend is always receiving 500 - Internal Server Error , but when debugging controller never returns internal server error ,but clearly gets to "return ok()". Frontend recieve this error when I enter CompletableFuture.supplyAsync() block. 

Will Sargent

unread,
Mar 13, 2017, 4:44:52 PM3/13/17
to play-fr...@googlegroups.com
Have you looked at adding something to the Accumulator?

There's an example template in https://github.com/playframework/play-java-fileupload-example that may be helpful.

--
Will Sargent
Engineer, Lightbend, Inc.


On Sat, Mar 11, 2017 at 5:44 PM, MJ <mdon...@gmail.com> wrote:
I forgot to add , then my frontend is always receiving 500 - Internal Server Error , but when debugging controller never returns internal server error ,but clearly gets to "return ok()". Frontend recieve this error when I enter CompletableFuture.supplyAsync() block. 

--
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-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/bfaa6599-c2ae-47c3-8cad-f4c1242ac706%40googlegroups.com.

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

Reply all
Reply to author
Forward
0 new messages