日期:2014-05-19 浏览次数:20725 次
/** * 下载文件 * @param filePath 文件路径(物理路径) * @param fileName 源文件名称 */ public void downLoadFile(String filePath, String fileName) { File file = new File(filePath); if (!file.exists() || file.isDirectory()) { return; } InputStream input =null; OutputStream output = null; try { input = new FileInputStream(file); output = getResponse().getOutputStream(); getResponse().setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); byte[] buffer = new byte[1024]; int i = 0; while ((i = input.read(buffer)) != -1) { output.write(buffer, 0, i); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(null !=output) { output.flush(); output.close(); } if(null !=input) { input.close(); } } catch (Exception e) { e.printStackTrace(); } } }