日期:2014-05-20 浏览次数:20764 次
public static void downLoadZipAndRar(HttpServletRequest request, HttpServletResponse response, String fileName) throws Exception, NullPointerException { String prjId = request.getParameter("prjId"); if(prjId!=null||!"".equals(prjId)){ PubDataManager.downloadAdd(prjId); StringBuffer redirectString = new StringBuffer(); redirectString.append(PubDataManager.getPubDataManager().getPubResourceDesc(Constants.RSC_DOWNLOAD_HOST)) .append("prjInfo/") .append(fileName); response.sendRedirect(response.encodeRedirectURL(redirectString.toString())); return; } String filePath = request.getSession().getServletContext().getRealPath( "/download/prjInfo"); String codePrjStr = request.getParameter("codePrjId"); if(codePrjStr != null && !"".equals(codePrjStr)) { long codePrjId = Long.parseLong(request.getParameter("codePrjId")); String code_prj_flag = request.getParameter("code_prj_flag"); if(code_prj_flag != null && !"".equals(code_prj_flag) && code_prj_flag.equals("cpflag")) { String rootPath = request.getSession().getServletContext().getRealPath("/"); filePath = rootPath.replaceAll("ruancu", "ruanko"); filePath += WebappUtil.getCodePrjZipDir(codePrjId); } } BufferedOutputStream bos = null; BufferedInputStream bis = null; StringBuffer sb = new StringBuffer(50); sb.append("attachment; filename="); sb.append(fileName); try { if (fileName != null) { response.reset(); response.setContentType("application/msword"); response.setHeader("Content-Disposition", new String(sb .toString().getBytes(), "ISO8859-1")); String tmp = filePath + "/" + fileName; File file = new File(tmp); bis = new BufferedInputStream( new FileInputStream(tmp)); int i; byte buffer[] = new byte[1024]; if (file.exists()) { bos = new BufferedOutputStream(response.getOutputStream()); while ((i = bis.read(buffer)) != -1) { bos.write(buffer, 0, i); } bos.flush(); } } } catch (Exception e) { if(log.isDebugEnabled()){ log.debug(fileName + " download was interruppt!", e); } }finally{ try { if (bis != null) { bis.close(); } } catch (Exception e) { } try { if (bos != null) { bos.close(); } } catch (Exception e) { } } }