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;
}
注意中文文件名乱码问题,经过上面的转换,中文名称可以正常显示。