java web项目实现文件下载实例代码[收藏]

    File file = new File(path);// path是根据日志路径和文件名拼接出来的
    String filename = file.getName();// 获取日志文件名称
    InputStream fis = new BufferedInputStream(new FileInputStream(path));
    byte[] buffer = new byte[fis.available()];
    fis.read(buffer);
    fis.close();
    response.reset();
    // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
    response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("gbk"),"iso8859-1"));
    response.addHeader("Content-Length", "" + file.length());
    OutputStream os = new BufferedOutputStream(response.getOutputStream());
    response.setContentType("application/octet-stream");
    os.write(buffer);// 输出文件
    os.flush();
    os.close();

posted @ 2014-06-03 18:13  魏朝辉  阅读(377)  评论(0编辑  收藏  举报