---
title: "文件下载"
author: "CodeToMyLaw"
date: "2012-03-31T14:48:11Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2012-03-31T14:47:55Z"
id: 1471594
url: "https://codetomylaw.iteye.com/blog/1471594"
---

```java

	/**
	 * 评论文档文件下载
	 * @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;
	}

```