下载blob并保存到文件

这篇博客介绍了如何在Java中通过监听事件下载Blob对象,并根据文件扩展名设置正确的MIME类型,最终将Blob内容保存为文件。具体实现包括获取Blob数据,设置响应头信息,以及将输入流写入输出流的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<af:commandLink text="#{row.Blobname}" id="cl1"
                                            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);
        }
    }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值