ssh2框架文件下载

1、xml

<action name="downContractFile" class="ContractConfirmAction" method="downFile" >
            <result name="downloadProjectFile" type="stream">
                <param name="contentType">application/octet-stream</param>
                <!-- inputName 流对象名 —— 比如这里写inputStream,它就会自动去找Action中的getInputStream方法。 -->
                <param name="inputName">inputStream</param>
                <!-- contentDisposition 使用经过转码的文件名作为下载文件名 —— 默认格式是attachment;filename="${fileName}",将调用该Action中的getFileName方法。 -->
                <param name="contentDisposition">attachment;fileName=${fileName}</param>
                <!-- bufferSize 下载文件的缓冲大小 -->
                <param name="bufferSize">4096</param>
            </result>
        </action>

2、action.java

// 下载专用
    private String fileName;
    private String filePath;
public String downFile()
    {
        TWebuser SUser = (TWebuser) getSession().getAttribute("WebUser");
        try
        {
            String contractId = getRequest().getParameter("contractId");
            String hqlFindFile = "from TContractFile where TContractOrganization.TContract.id = '" + contractId + "' order by operatorTime DESC ";
            List<TContractFile> resultList = daoFactory.getHibernateFactory().find(hqlFindFile);
            TContractFile contractFile = null;
            StringBuilder filePathSB = null;
            if(!Convert.isEmpty(resultList))
            {
                contractFile = (TContractFile) resultList.get(0);
                // 组装文件路径
                filePathSB = new StringBuilder();
                filePathSB.append(getSession().getServletContext().getRealPath("/"))
                .append(contractFile.getFileRootFolder()).append("/")
                .append(contractFile.getFileTypeFolder()).append("/")
                .append(contractFile.getFileYearFolder()).append("/")
                .append(contractFile.getFileMonthFolder()).append("/")
                .append(contractFile.getFileDayFolder()).append("/")
                .append(contractFile.getFileUuidName()).append(contractFile.getFileSuffix());

                this.fileName =  contractFile.getFileName() + contractFile.getFileSuffix();
            }
            File file = new File(filePathSB.toString());
            FileInputStream fis = new FileInputStream(file);

            inputStream = fis;
        }
        catch (Exception e)
        {
            logger.error("ContractConfirmAction.downContractFile() error", e);
            ItemUtil.saveLogs(SUser, "执行审核合同操作失败, exception : " +e.getMessage(), this.daoFactory, getRequest());
            return ERROR;
        }

        return "downloadProjectFile";
    }

获取文件名:

public String getFileName()
    {
        String returnFileName = "";
        try 
        {
            returnFileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");

        } 
        catch (Exception e)
        {
            e.printStackTrace();
        } 
        return returnFileName;
    }

注意中文文件名乱码问题,经过上面的转换,中文名称可以正常显示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值