java response 下载文件

1:废话少说直接上源码:

   File file = new File(rootPath+"files/summary/"+zipName); //要下载的文件绝对路径
   InputStream ins = new BufferedInputStream(new FileInputStream(rootPath+"files/summary/"+zipName));
   byte [] buffer = new byte[ins.available()];
   ins.read(buffer);
   ins.close();
   
   HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
   response.reset();
   response.addHeader("Content-Disposition", "attachment;filename=" + new String(zipName.getBytes()));
   response.addHeader("Content-Length", "" + file.length());
   OutputStream ous = new BufferedOutputStream(response.getOutputStream());
   response.setContentType("application/octet-stream");
   ous.write(buffer);
   ous.flush();
   ous.close();

你可能感兴趣的:(java,String,File,buffer,byte)