Принуждение сохранения в виде диалога из любого веб-браузера из приложения JSF

Я создал приложение JSF, и я хочу вставить ссылку на страницу, которая при нажатии вызывает поддержку bean для резервного копирования некоторого xml и принудительного открытия диалогового окна save-as download, чтобы пользователь может выбрать место для сохранения файла. Я уже написал код JAXB.

Как это делается?

Спасибо

Ответы

Ответ 1

Установите заголовок HTTP Content-Disposition на attachment. Появится диалог Сохранить как. Вы можете сделать это с помощью HttpServletResponse#setHeader(). Вы можете получить ответ сервлета HTTP из под капотом JSF ExternalContext#getResponse().

В контексте JSF вам нужно только убедиться, что вы вызываете FacesContext#responseComplete(), чтобы избежать пролета IllegalStateException.

Пример Kickoff:

public void download() throws IOException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

    response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
    response.setContentType("application/xml"); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename.
    response.setHeader("Content-disposition", "attachment; filename=\"name.xml\""); // The Save As popup magic is done here. You can give it any filename you want, this only won't work in MSIE, it will use current request URL as filename instead.

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        input = new BufferedInputStream(getYourXmlAsInputStream());
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[10240];
        for (int length; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
    } finally {
        close(output);
        close(input);
    }

    facesContext.responseComplete(); // Important! Else JSF will attempt to render the response which obviously will fail since it already written with a file and closed.
}

Ответ 2

Использовать заголовок content-disposition: attachment HTTP

Ответ 3

иногда вам нужно заставить автора отправить содержимое клиенту, вызвав response.getWriter().flush(); перед закрытием записи. Это вызвало сохранение как всплывающее окно в моем случае.