1、下载单个文件

public JsonView downPrintFile(){
		//要下载的文件名 从前台传来
		String fileNameNeedDown = request.getParameter("fileName");
                //这里的路径是要下载的文件所在路径
		String realPath = request.getServletContext().getRealPath("/")+"printController\\";
		//要下载的文件路径+文件名
		String aFilePath = realPath + fileNameNeedDown;
		File file = null;
		file = new File(aFilePath);
		
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			String aFileName = null;
			request.setCharacterEncoding("UTF-8");
			String agent = request.getHeader("User-Agent").toUpperCase();
			if ((agent.indexOf("MSIE") > 0)
					|| ((agent.indexOf("RV") != -1) && (agent
							.indexOf("FIREFOX") == -1)))
				aFileName = URLEncoder.encode(fileNameNeedDown, "UTF-8");
			else {
				aFileName = new String(fileNameNeedDown.getBytes("UTF-8"), "ISO8859-1");
			}
			response.setContentType("application/octet-stream");//octet-stream为要下载文件是exe类型或看该文档http://www.w3school.com.cn/media/media_mimeref.asp
			response.setHeader("Content-disposition", "attachment; filename="
					+ aFileName);
			response.setHeader("Content-Length", String.valueOf(file.length()));
			bis = new BufferedInputStream(new FileInputStream(new File(aFilePath)));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
				bos.write(buff, 0, bytesRead);
			System.out.println("success");
			bos.flush();
		} catch (Exception e) {
			System.out.println("失败!");
		} finally {
			try {
				if (bis != null) {
					bis.close();
				}
				if (bos != null) {
					bos.close();
				}
//				file.delete();
			} catch (Exception e) {
			}
		}
		return new JsonView();
	}

2、下载多个文件,原理是依次下载每个文件,下载完一个加入压缩包,再将压缩包下载下来。最终下载下来的文件是个压缩包。

//下载打印的exe文件至本地
    public void downPrintLodopFile() throws Exception{
//    	String property = System.getProperty("os.arch");//amd64
    	//获得exe文件路径
    	String realPath = request.getServletContext().getRealPath("/")+File.separator+"printFiles\\";
    	String setup_fileName = "Setup.exe";
    	String install32_fileName = "install32.exe";
    	String install64_fileName = "install64.exe";
    	//执行down
    	down(realPath, new String[]{realPath+setup_fileName , realPath+install32_fileName , realPath+install64_fileName }, request, response);
		
	}
public void down(String path, String[] files, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// path 压缩文件初始设置 
		String base_name = "printFiles";
		String fileZip = base_name + ".zip"; // 拼接zip文件,之后下载下来的压缩文件的名字
		String filePath = path + fileZip;// 之后用来生成zip文件

		// 创建临时压缩文件
		try {
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
			ZipOutputStream zos = new ZipOutputStream(bos);
			ZipEntry ze = null;
			for (int i = 0; i < files.length; i++) {// 将所有需要下载的文件都写入临时zip文件
				BufferedInputStream bis = new BufferedInputStream(
						new FileInputStream(files[i]));
				ze = new ZipEntry(
						files[i].substring(files[i].lastIndexOf("\\")));
				zos.putNextEntry(ze);
				int s = -1;
				while ((s = bis.read()) != -1) {
					zos.write(s);
				}
				bis.close();
			}
			zos.flush();
			zos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 以上,临时压缩文件创建完成

		// 进行浏览器下载
		// 获得浏览器代理信息
		String agent = request.getHeader("User-Agent").toUpperCase();
		// 判断浏览器代理并分别设置响应给浏览器的编码格式
		String finalFileName = null;
		if ((agent.indexOf("MSIE") > 0)
				|| ((agent.indexOf("RV") != -1) && (agent.indexOf("FIREFOX") == -1)))
			finalFileName = URLEncoder.encode(fileZip, "UTF-8");
		else {
			finalFileName = new String(fileZip.getBytes("UTF-8"), "ISO8859-1");
		}
		response.setContentType("application/x-download");// 告知浏览器下载文件,而不是直接打开,浏览器默认为打开
		response.setHeader("Content-Disposition", "attachment;filename=\""
				+ finalFileName + "\"");// 下载文件的名称
		//输出到本地
		ServletOutputStream servletOutputStream = response.getOutputStream();
		DataOutputStream temps = new DataOutputStream(servletOutputStream);

		DataInputStream in = new DataInputStream(new FileInputStream(filePath));// 浏览器下载临时文件的路径
		byte[] b = new byte[2048];
		File reportZip = new File(filePath);// 之后用来删除临时压缩文件
		try {
			while ((in.read(b)) != -1) {
				temps.write(b);
			}
			temps.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (temps != null)
				temps.close();
			if (in != null)
				in.close();
			if (reportZip != null)
				reportZip.delete();// 删除服务器本地产生的临时压缩文件
			servletOutputStream.close();
		}
	}