java文件下载

来源:互联网 发布:网络摄像头服务器搭建 编辑:程序博客网 时间:2024/06/04 01:30

html:

<a  href="javascript:;" type='button'  onclick="importExcel()">数据导出</a>

js:

function importExcel(){     var url = '';
     var form = $("<form></form>").attr("action", url).attr("method", "post");     form.append($("<input></input>").attr("type", "hidden").attr("name", "duration").attr("value", test));     form.appendTo('body').submit().remove();}

java:

/** * 输出文件 *  * @param response * @throws IOException */protected static void print(HttpServletResponse response, HSSFWorkbook workbook) throws IOException {String fileNameGb = new String(fileName.getBytes("gb2312"), "ISO8859-1");ByteArrayOutputStream baos = new ByteArrayOutputStream();workbook.setSelectedTab(0);workbook.write(baos);response.setContentType("application/x-download;charset=utf-8");response.addHeader("Content-Disposition", "attachment;filename=" + fileNameGb + ".xls");OutputStream os = response.getOutputStream();ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());byte[] b = new byte[1024];while ((bais.read(b)) > 0) {os.write(b);}bais.close();os.flush();os.close();}

拼接excel数据就不贴出来了。

原创粉丝点击