0

I am trying to load multiple files at the same time with Play 2 and build some thumbnails for them with asynchronous api. Uploading is work quite ok and great troubles come when image processing starts. The problem is as follows(a bit of desciption): files, which were fetched from request and collected into list are disappearing while image processing(take a lot of time).

And some samples here:

//part of controller
public static Result addPictures2(Long galleryId) {


        MultipartFormData body              = request().body().asMultipartFormData();
        final List<FilePart> fileList       = body.getFiles(); 
        final Long fGalleryId               = galleryId;

        Promise<Boolean> promiseOfBool = Akka.future( 
                new Callable<Boolean>() {
                    public Boolean call() {

                        Gallery gallery = Gallery.find.byId(fGalleryId);

                        for(FilePart part : fileList) {
                            File    picFile     = part.getFile();
                            Logger.debug("picFile: " + picFile.exists());
                            String  extension   = FilenameUtils.getExtension(part.getFilename());
                            GalleryItem item    = new GalleryItem("", "", picFile, extension);
                            gallery.addItem(item);
                        }

                        gallery.update();
                        return true;
                    }
                }
        );



        return async(
            promiseOfBool.map(
                new Function<Boolean, Result>() {
                    public Result apply(Boolean b) {
                        return redirect(
                                controllers.backend.routes.GalleryContentController.showGalleryItemsPage(fGalleryId)
                            );
                    }
                }
            )
        );


    }

So

gallery.addItem(item);

takes a lot of time, and next call for

Logger.debug("picFile: " + picFile.exists());

says that picFile do not exists. I know, this is because those files are temporary... but should they exists emm a little bit longer? And the question is: how to solve this trouble. Should I look at temporary files in java?

4

1 回答 1

0

看起来解决方案非常简单。我应该将文件保存在我自己的缓存中,直到我在 Akka worker 中使用它们。用 Akka 处理后,我可以自己删除它。这种行为的原因是不浪费文件描述符。从请求中获取的文件将在 Akka 启动之前被删除。无论如何,这只是一个猜测。我现在就试试。

PS是的。这儿存在一个问题。解决方案是将这些文件保留为临时文件,仅在处理后删除。

于 2012-07-01T17:08:05.737 回答