HttpServletResponse触发浏览器下载文件

Java代码例子

public static void download(HttpServletResponse response, String fileName, byte[] content) throws Exception {
    OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
    response.reset();
    response.setContentType("application/octet-stream");
    response.setBufferSize(1024);
    response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"),"iso8859-1"));
    response.addHeader("Content-Length", String.valueOf(content.length));
    toClient.write(content);
    toClient.flush();
    toClient.close();
}

MIME类型application/octet-stream可以表示任意二进制数据 HTTP头Content-Disposition加上attachement可以触发浏览器下载 注意:使用AJAX方式,无法触发浏览器下载