各个浏览器下载乱码问题
2015-12-29 16:04
411 查看
<pre name="code" class="java">public static void downloadAttachment(AttachmentContent attachment) { InputStream is = null; OutputStream out = null; try { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); String fileType = attachment.getType(); if (StringUtils.hasLength(fileType)) { response.setContentType(fileType); } else { response.setContentType(MimeTypeConstants.MIMETYPE_STREAM); } /* String fileName = attachment.getName(); fileName = URLEncoder.encode(fileName, Constants.DEFAULT_ENCODING); response.addHeader(Content_Disposition, attachment_filename + fileName);*/ /** * fileName = new String(fileName.getBytes(Constants.DEFAULT_ENCODING),"ISO8859-1");谷歌、360极速浏览器 * fileName = URLEncoder.encode(fileName, Constants.DEFAULT_ENCODING); IE浏览器 * fileName = new String(fileName.getBytes("GBK"),"ISO8859-1"); IE11浏览器 */ String fileName = attachment.getName(); if (RequestUtils.getRequest().getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) { fileName = URLEncoder.encode(fileName, Constants.DEFAULT_ENCODING); }else if(RequestUtils.getRequest().getHeader("User-Agent").toUpperCase().indexOf("RV:11.0") > 0){ fileName = new String(fileName.getBytes("GBK"),"ISO8859-1"); } else{ fileName = new String(fileName.getBytes(Constants.DEFAULT_ENCODING),"ISO8859-1"); } //response.addHeader(Content_Disposition,attachment_filename + fileName); response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName)); // 文件名外的双引号处理firefox的空格截断问题 out = response.getOutputStream(); if(attachment!=null && attachment.getContent()!=null){ is = new ByteArrayInputStream(attachment.getContent()); IOUtils.copy(is, out); } // out.flush(); } catch (Exception e) { logger.error(e.getMessage(), e); throw new SystemException(e.getMessage(), e); } finally { FileUtils.close(is); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Manifest 用法
- Android学习笔记(二九):嵌入浏览器
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- Scrapy的架构介绍
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器