zoukankan      html  css  js  c++  java
  • javaWeb页面实现下载

    jsp页面发送请求,后台实现指定路径下的文件,在浏览器上完成下载功能。
    @RequestMapping(value = "downloadFile")
    public void downloadFile(HttpServletResponse response) throws Exception {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    InputStream is = null;
    String zipFileName = "test.zip";
    String zipFilePath = "D:\";
    File file = new File(zipFilePath + zipFileName);
    try {
    is = new FileInputStream(file);
    response.reset();
    response.setContentType("application/x-msdownload");
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setHeader(
    "Content-disposition",
    "attachment; filename="
    + new String(zipFileName.getBytes("GBK"),
    "ISO8859-1"));
    bis = new BufferedInputStream(is);
    bos = new BufferedOutputStream(response.getOutputStream());
    byte[] buff = new byte[2048];
    int bytesRead;
    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    bos.write(buff, 0, bytesRead);
    }
    } catch (Exception e) {
    e.printStackTrace();
    response.setContentType("text/html");
    response.getWriter().print("download failed");
    } finally {
    try {
    if (is != null)
    is.close();
    if (bis != null)
    bis.close();
    if (bos != null)
    bos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    知识改变命运,代码改变生活。
  • 相关阅读:
    带参的方法
    类的无参方法
    类和对象
    关于线程间的通信的几个解决事例
    一个简单的邮件发送
    关于process
    关于java的static语句块
    关于struts2拦截器获取页面参数
    hexo github pages 首页不展示,出现代码怎么办
    使用Hexo搭建个人博客(三)
  • 原文地址:https://www.cnblogs.com/xubb/p/8487189.html
Copyright © 2011-2022 走看看