1.action类
public String doDownLoad() throws InstantiationException, IllegalAccessException, IOException{
String fid=request.getParameter("fid");//从页面获取文件id如果为空返回异常;
if(fid==null){
return Action.ERROR;
}
FileBean fileBean= new FileSevice().getFile(fid);//从数据库获取文件bean如果为空返回异常;
System.out.println("fileBean:"+fileBean);
if(fileBean==null){
return Action.ERROR;
}
String path = fileBean.getPath();//判断路径和文件名称是否为空,如果为空返回异常;
System.out.println("FileName:"+fileBean.getFileName());
this.fileFileName=new String(fileBean.getFileName().getBytes(),"ISO8859-1");
if(path==null||this.fileFileName==null){
return Action.ERROR;
}
File files = new File(path);
if(!files.exists()){
return Action.ERROR;
}
this.fileFileContentType = fileFileName.substring(fileFileName.lastIndexOf("."));
System.out.println("fileFileContentType:"+this.fileFileContentType);
inputStream = new FileInputStream(files);
this.fileFileName = fileBean.getFileName();
contentLength = inputStream.available();
return Action.SUCCESS;
}
}
2.struct2配置
<action name="downProjectFile" class="com.ipvr.edu.Action.ProjectFile" method="doDownLoad">
<result type="stream">
<param name="inputstream">inputStream</param>
<param name="contentDisposition">attachment;fileName="${fileFileName}"</param>
<param name="contentType">application/octet-stream</param>
<param name="bufferSize">2048</param>
</result>
<result name="error">error.html</result>
</action>
3.输出
fileBean:FileBean [fileId=73, fileName=总体设计文档.docx, type=file, path=C:apache-tomcat-7.0.70webappsTx4.0fileprojectFile20170515192926248.docx, upTime=2017-05-15 19:29:26.0, visble=1+contentnull]
FileName:总体设计文档.docx
fileFileContentType:.docx