Ali
Ali

Reputation: 267077

How to copy an uploaded file in Play Framework?

I'm trying to do a basic file upload in Play 2.0.4. The file upload itself works, and I manage to get a File object. However, I want to copy this file into the /public/images/ directory. How can I do that? The following code produces no result or errors:

    FilePart picture = body.getFile("file");
    File file = picture.getFile();
    try
    {
        File dest = new File("../../public/images/new.png");
        Logger.debug("Got dest: " + dest.getAbsolutePath() );
        file.renameTo(dest);
        return ok( file.getAbsolutePath() );
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return ok(images.render(false, e.toString()));
    }

Upvotes: 0

Views: 1219

Answers (1)

irreputable
irreputable

Reputation: 45443

File.renameTo() returns false if it falis, but there's no detailed error information so it sucks.

Try java.nio.file.Files.move(source, target, StandardCopyOption.ATOMIC_MOVE); it'll throw an exception if it fails, so you'll know what's wrong.

Upvotes: 1

Related Questions