您的位置:首页 > 编程语言 > Java开发

各个浏览器下载乱码问题

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);

}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息