下载blob并保存到文件

                                            actionListener="#{backingBeanScope.BlobBean.downLoadFile}"/>

//af:table中的列

 

    public void downLoadFile(ActionEvent actionEvent) {
        FacesCtrlHierNodeBinding f = (FacesCtrlHierNodeBinding)this.tablebinding.getSelectedRowData();
        Row row = f.getRow();
        BlobDomain fileContent = (BlobDomain)row.getAttribute("Fl");
        String fileName = row.getAttribute("Blobname").toString();
        System.out.println(fileName+"文件名");
        String fileType = "aplication/octet-stream";
        if (fileName.endsWith(".pdf")) {
            fileType = "application/PDF";
        } else if (fileName.endsWith(".doc")) {
            fileType = "aplication/msword";
        } else if (fileName.endsWith(".txt")) {
            fileType = "text/plain";
        } else if (fileName.endsWith(".ppt")) {
            fileType = "application/vnd.ms-powerpoint";
        } else if (fileName.endsWith(".rar")) {
            fileType = "aplication/octet-stream";
        } else if (fileName.endsWith(".zip")) {
            fileType = "aplication/zip";
        } else if (fileName.endsWith(".jpg")) {
            fileType = "aplication/jpg";
        } else {
            fileType = "aplication/octet-stream";
        }
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext extContext = facesContext.getExternalContext();
        Long length = fileContent.getLength();

        HttpServletResponse response = (HttpServletResponse)extContext.getResponse();
//        response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");//这让做中文显示乱码

        try {
            response.setHeader( "Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) );
        } catch (UnsupportedEncodingException e) {
            System.out.println("下载异常!");
        }
        response.setContentLength((int)length.intValue());

        response.setBufferSize(1024);
        response.setContentType(fileType);

        try {
            writeBlobDomainToOutputStream(fileContent, response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

        facesContext.responseComplete();
    }

 

 

    public void writeBlobDomainToOutputStream(BlobDomain blobDomain, OutputStream out) throws SQLException,
                                                                                              IOException {
        InputStream in = blobDomain.getBinaryStream();
        writeInputStreamToOutputStream(in, out);
        blobDomain.closeInputStream();
        in.close();
        out.close();
    }

    public void writeInputStreamToOutputStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[8192];
        int bytesRead = 0;
        while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
    }

 

 

 

你可能感兴趣的:(ADF)