<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);
}
}
各个浏览器下载乱码问题
最新推荐文章于 2022-05-29 01:05:33 发布