您的位置:首页 > 编程语言 > Java开发

文件下载

2013-10-24 10:40 302 查看
/**
* 评论文档文件下载
* @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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 文档