java下载页面的显示类,可以直接调用

来源:互联网 发布:淘宝客服绩效在哪里看 编辑:程序博客网 时间:2024/09/21 09:02
public static void download(String path, HttpServletResponse response) {try {// path是指欲下载的文件的路径。File file = new File(path);// 取得文件名。String filename = file.getName();// 以流的形式下载文件。InputStream fis = new BufferedInputStream(new FileInputStream(path));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();// 设置response的HeaderLong newDate=new Date().getTime();response.addHeader("Content-Disposition", "attachment;filename="+java.net.URLEncoder.encode("顺义区律师值班表"+newDate+".xls", "UTF-8"));response.addHeader("Content-Length", "" + file.length());OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/vnd.ms-excel;charset=gb2312");toClient.write(buffer);toClient.flush();toClient.close();} catch (IOException ex) {ex.printStackTrace();}}

0 0
原创粉丝点击