java下载代码

xiaoxiao2021-02-28  80

 /*   * 下载

  * param:filepath文件绝对路径   */  public void downLoad(HttpServletRequest request,HttpServletResponse response) throws IOException{      String   file = request.getParameter("filepath");      int in = file.lastIndexOf("/");      String   fileName = file.substring(in+1, file.length());   BufferedInputStream     bis = null;   BufferedOutputStream    bos = null;      try {       response.setContentType("application/octet-stream");        response.setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));       bis=new  BufferedInputStream(new FileInputStream(file));    bos=new  BufferedOutputStream(response.getOutputStream());    byte[]  by=new byte[2048];    int     bytesRead;    while(-1!=(bytesRead=bis.read(by,0,by.length))){     bos.write(by, 0, bytesRead);    }    bis.close();    bos.flush();     bos.close();   } catch (IOException e) {    e.printStackTrace();   }finally {    if(bis!=null){     bis.close();     }    if(bos!=null){     bos.close();    }         }     }

转载请注明原文地址: https://www.6miu.com/read-96928.html

最新回复(0)