导航

java下载

Posted on 2013-11-04 14:24  小强有毒  阅读(178)  评论(0)    收藏  举报
 1 HttpServletResponse response = ServletActionContext.getResponse();
 2 response.addHeader("Content-Disposition","attachment; filename="+ new String(name.getBytes(),"utf-8"));
 3 response.setContentType("application/octet-stream");
 4 OutputStream out = response.getOutputStream();
 5 BufferedInputStream bin = new BufferedInputStream(new FileInputStream(path));
 6 byte[] buf = new byte[1024];
 7 int len = 0;
 8 while ((len = bin.read(buf)) > 0)
 9   out.write(buf, 0, len);
10 out.close();
11 bin.close();

path参数为文件路径。