Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf &引用;“内容处置”;标题在对话框窗口中显示错误的文件名;另存为;_Jsf_Primefaces_Jsf 2.2_Content Disposition - Fatal编程技术网

Jsf &引用;“内容处置”;标题在对话框窗口中显示错误的文件名;另存为;

Jsf &引用;“内容处置”;标题在对话框窗口中显示错误的文件名;另存为;,jsf,primefaces,jsf-2.2,content-disposition,Jsf,Primefaces,Jsf 2.2,Content Disposition,下载文件时,“另存为”对话框窗口中显示的文件名是视图的名称(Page2.pdf),而不是文件名“cyn.pdf” 标题内容配置位于FileUploadController的第161行,并在第2页的第34行“下载”其调用。xhtml <h:body> <h2>Acceso Alumno</h2> <h1>Alumnos registrados </h1> <h:form> <h:

下载文件时,“另存为”对话框窗口中显示的文件名是视图的名称(Page2.pdf),而不是文件名“cyn.pdf”

标题内容配置位于FileUploadController的第161行,并在第2页的第34行“下载”其调用。xhtml

<h:body>
    <h2>Acceso Alumno</h2>
    <h1>Alumnos registrados </h1>

    <h:form>
        <h:dataTable id ="tabla"  value ="#{alumno.getListaAlumno()}" var= "var" border="1">

            <h:column>
                <f:facet name="header">Id </f:facet>
                <h:outputText value = "#{var.id}"/>
            </h:column>

            <h:column>
                <f:facet name="header">Nombre y Apellidos </f:facet>
                <h:outputText value = "#{var.nombreApellidos}"/>
            </h:column>

            <h:column>
                <f:facet name="header">Matricula </f:facet>
                <h:outputText value = "#{var.matricula}"/>
            </h:column>

            <h:column>
                <f:facet name="header">ProductBox </f:facet>
                <h:commandButton action="#{fileUploadController.downLoad}" value="Guardar Archivo"  >
                    <f:setPropertyActionListener target="#{fileUploadController.ruta}" value="#{var.pdf}" />
                </h:commandButton>
            </h:column>
        </h:dataTable>
    </h:form>

    <h:form>
        <h:commandButton action="home?faces-redirect=true" value="Home" />
    </h:form>
    <!-- Cerrar Sesion -->
    <a href="logout">Cerrar Sesion</a>
</h:body>
</html>
FileUploadController Bean

@ManagedBean(name = "fileUploadController")
@SessionScoped
public class FileUploadController implements Serializable {

    private static final long serialVersionUID = 1L;
    //tama�o del buffer
    private static final int DEFAULT_BUFFER_SIZE = 10240;
    private byte[] archi = new byte[0];
    public String nombre;
    public String ruta;
    public String nombreArchivo;

    public byte[] getArchi() {
        return archi;
    }

    public void setArchi(byte[] archi) {
        this.archi = archi;
    }

    // Nombre del Archivo
    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getRuta() {
        return ruta;
    }

    public void setRuta(String ruta) {
        this.ruta = ruta;
    }

    // ruta fisica del archivo
    public String getRealPath() {
        FacesContext aFacesContext = FacesContext.getCurrentInstance();
        ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext();
        return context.getRealPath("/");
    }

    /* subida del archivo con sus atributos */
    public void fileUpload(FileUploadEvent event, String nombre, String type, String directorio) {
        try {
            this.nombre = "cyn" + type;

            this.ruta = directorio + getNombre();
            this.archi = getFileContents(event.getFile().getInputstream());

            File file = new File(directorio);
            nombreArchivo = file.getName();
                // Crea el directorio, incluidos subdirectorios q no existan

            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (Exception ex) {
            System.out.println("Error en la subida " + ex);
        }
    }

    private byte[] getFileContents(InputStream in) {
        byte[] bytes = null;
        try {
            // write the inputStream to a FileOutputStream
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int read = 0;
            bytes = new byte[1024];

            while ((read = in.read(bytes)) != -1) {
                bos.write(bytes, 0, read);
            }
            bytes = bos.toByteArray();
            in.close();
            in = null;
            bos.flush();
            bos.close();
            bos = null;
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return bytes;
    }

    // Guardar el archivo
    public void guardar() {
        try {
            FileOutputStream out;

            out = new FileOutputStream(ruta);
            System.out.print("out" + out);

            out.write(archi);
            System.out.print(archi);
            out.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    /*-------------------- Servlet Descarga Archivos----------------------------------------------*/
    public void downLoad() throws IOException, ServletException {
        FacesContext contexto = FacesContext.getCurrentInstance();

        HttpServletResponse response = (HttpServletResponse) contexto.getExternalContext().getResponse();
        //ruta de los archivos
        System.out.println("paso parametro ruta  " + ruta);
        File file = new File(ruta);
        System.out.println("paso parametro ruta a file " + file);
        /*Validacion*/
        if (!file.exists()) {
            System.out.println("El archivo no existe!");
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        response.reset();
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setContentType("application/pdf");
        response.setHeader("Content-Length", String.valueOf(file.length()));

        /*Ventana de abrir/guardar*/

        /*attachment: muestra la ventana*/
        response.setHeader("Content-Disposition", "attachment; Nombre del Archivo=\"" + nombreArchivo + "\"");
        System.out.println("NOmbre archivo en download" + nombreArchivo);
        //inicializo el input y el output
        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;

            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
        } finally {
            //cierro
            input.close();
            output.close();
        }
        contexto.responseComplete();
    }
}
查看:第2页.xhtml

<h:body>
    <h2>Acceso Alumno</h2>
    <h1>Alumnos registrados </h1>

    <h:form>
        <h:dataTable id ="tabla"  value ="#{alumno.getListaAlumno()}" var= "var" border="1">

            <h:column>
                <f:facet name="header">Id </f:facet>
                <h:outputText value = "#{var.id}"/>
            </h:column>

            <h:column>
                <f:facet name="header">Nombre y Apellidos </f:facet>
                <h:outputText value = "#{var.nombreApellidos}"/>
            </h:column>

            <h:column>
                <f:facet name="header">Matricula </f:facet>
                <h:outputText value = "#{var.matricula}"/>
            </h:column>

            <h:column>
                <f:facet name="header">ProductBox </f:facet>
                <h:commandButton action="#{fileUploadController.downLoad}" value="Guardar Archivo"  >
                    <f:setPropertyActionListener target="#{fileUploadController.ruta}" value="#{var.pdf}" />
                </h:commandButton>
            </h:column>
        </h:dataTable>
    </h:form>

    <h:form>
        <h:commandButton action="home?faces-redirect=true" value="Home" />
    </h:form>
    <!-- Cerrar Sesion -->
    <a href="logout">Cerrar Sesion</a>
</h:body>
</html>

Acceso校友
注册校友
身份证件
诺姆布雷·阿佩利多斯
母体
产品箱

尝试使用以下
内容配置

response.setHeader("Content-disposition", "attachment;filename=cyn.pdf");

请仅包括相关代码。