How to execute code after returning Result in java Play! Framework?

I am using Play! Framework 2.2.1, and I want to transfer the file created on the go. After the full stream of the file, I want to clear it, but I have no context for this.

Is there any annotation or feedback for this kind of operation?

+4
source share
1 answer

You can copy the contents of the file to FileInputStream, delete the file and then return the stream, in any case you need to set some headers based on the source file, i.e. if you create ZIP files, it could be (pseudo code!):

public static Result file() {

    File tmpFile = new File("/path/to/your/generated.zip");

    FileInputStream fin = null;
    try {
        fin = new FileInputStream(tmpFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    response().setHeader("Content-disposition", "attachment;filename=" + tmpFile.getName());
    response().setHeader(CONTENT_TYPE, "application/zip");
    response().setHeader(CONTENT_LENGTH, tmpFile.length() + "");

    tmpFile.delete();

    return ok(fin);
}

- ok(file);, , , n / x .

+2

Source: https://habr.com/ru/post/1545734/


All Articles