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?