欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文件下载

程序员文章站 2022-06-20 09:03:02
...

/**
* 评论文档文件下载
* @return
* @throws Exception
*/
public String downloadfile()throws Exception{

try{
//存放文件的根: fileRoot = /DPIv5r1MNP_Review;
AppInfo appInfo = appInfoService.getAppInfoById(Integer.parseInt(appId));
String appName = appInfo.getAppName();
String subName = appInfo.getSubCategory().getSubCategoryName();
String srvName = appInfo.getSubCategory().getSrvCategory().getSrvCategoryName();
// 应用所在的路径
String appPath = fileRoot + "/" + srvName + "/" + subName + "/" + appName ;


String commentPath = "";
commentPath = appPath + "/Doc/comment/" + userId;
String tmpName = new String(filename.getBytes("ISO8859-1"),"UTF-8");

File file=new File(commentPath+"\\"+tmpName);
FileInputStream fis=new FileInputStream(file);
this.getResponse().setContentType("application/force-download");
this.getResponse().setHeader("content-length", String.valueOf(file.length()));
this.getResponse().setHeader("Content-disposition", "attachment;filename="+ new String(tmpName.getBytes("gb2312"),"iso8859-1"));//new String(file.getName().getBytes("utf-8"),"gb2312")
ServletOutputStream os=this.getResponse().getOutputStream();
int length=0;
while((length=fis.read())!=-1){
os.write(length);
}
if(fis!= null){
fis.close();
}
if(os!= null){
os.flush();
os.close();
}
}catch(Exception e ){

}
return null;
}