/** * 文件下载 * @param fileUrl 要下载的文件路径 * */ public void downLoad(HttpServletResponse response, String headName, String fileUrl) { if (headName.endsWith(".txt")) { response.setContentType("text/plain"); response.addHeader("Content-disposition","attachment; filename=" + headName); } else { response.setContentType("application/x-msdownload"); response.addHeader("Content-disposition","attachment; filename=" + headName); }
String fileURL = fileUrl; if (fileURL == null) { return; }
try { File file = new File(fileUrl); FileInputStream bis = new FileInputStream(file); OutputStream bos = response.getOutputStream();
byte[] buff = new byte[1024]; int readCount = 0; int i = 0; readCount = bis.read(buff); while (readCount != -1) { bos.write(buff, 0, readCount); readCount = bis.read(buff); } if (bis != null) { bis.close(); } if (bos != null) { bos.close(); }
} catch (Exception e) { PubServers.getInstance().getMyLogInterface().myInfo("****** 下载出错了! ******"); }
}