java web导出文件
2015-12-16 17:47
323 查看
@RequestMapping(value = "exportTerminalFile/{id}") public void exportTerminalFile(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) throws Exception{ TerminalImportFile file = this.terminalService.getTerminalImportFile(id); if (file == null) { throw new BusinessException("import.terminal.file.notFound"); } String fileName = file.getName(); final String userAgent = request.getHeader("USER-AGENT"); String finalFileName = null; if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器 finalFileName = URLEncoder.encode(fileName,"UTF8"); }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器 finalFileName = new String(fileName.getBytes(), "ISO8859-1"); } else { finalFileName = URLEncoder.encode(fileName,"UTF8");//其他浏览器 } response.setContentType("application/xml; charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=\"" + finalFileName + "\""); ServletOutputStream os = response.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(os); InputStream is = file.getData().getBinaryStream(); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.flush(); bos.close(); os.flush(); os.close(); }
相关文章推荐
- JAVA中使用Formatter类进行输出格式的控制
- java 单例设计模式
- Myeclipse *.link用法
- Java基本数据类型
- Java安全管理器——SecurityManager
- MyEclipse 下用link 方式安装插件
- 在struts2中使用freemarker添加jsp标签支持
- spring配置双数据源
- Struts2 标签库讲解
- ibatis(学习2)-- spring + ibatis的事务管理
- java.lang.Class.getDeclaredMethod()方法
- java中的值传递和引用传递到底有什么区别
- spring保存一条数据库记录
- Java泛型详解
- Java与数据库数据类型对应表
- 用eclipse进行系统开发资料整理
- springMVC两种方式实现多文件上传及效率比较
- Spring 框架的设计理念与设计模式分析
- java基础(多线程)
- Java获得硬盘和主板的序列号代码