Ie下图片预览和下载,无法显示
最近Web开发中遇到ie浏览图片的时候无法显示,并且后台文件流中断,报"主机链接关闭".
根据后面研究发现,ie浏览器下图片的显示格式要跟后台响应头信息一致,不然就会出现这个问题。
(注:火狐和google没出现这个问题),
我这里用的fastdfs,
这个FileDataMetaTempDTO 类是存储文件信息的
解决代码如下
byte[] bytes=null;
ServletOutputStream out = null;
try {
FileDataMetaTempDTO fileDataMetaTempVO =null;
Map<String, String> map = new HashMap<>();
map.put("fdfsId", fastDFSId);
String downloadPath = PropertyUtils.getString("downloadFileMetaDataTemp");
String jsonString = HttpRequestUtils.get(downloadPath, map);
fileDataMetaTempVO = JSONUtils.json2Obj(jsonString, FileDataMetaTempDTO.class);
String groupName = fileDataMetaTempVO.getServerGroupName();
String filename = fileDataMetaTempVO.getFileURLMappingId();
if (StringUtils.isNotEmpty(groupName) && StringUtils.isNotEmpty(filename)) {
bytes = FastFSUtils.download(groupName, filename);
}
//文件类型如:png,gif
String imageType=fileDataMetaTempVO.getBasePath().substring(fileDataMetaTempVO.getBasePath().lastIndexOf(".") + 1, fileDataMetaTempVO.getBasePath().length());
//文件名称
String showfileName = fileDataMetaTempVO.getBasePath().substring(fileDataMetaTempVO.getBasePath().lastIndexOf(File.separator) + 1);
response.addHeader("Content-Disposition", "attachment;filename=" + new String(showfileName.getBytes("gb2312"), "iso-8859-1"));
if (fileDataMetaTempVO.getBasePath().toLowerCase().endsWith("png") || fileDataMetaTempVO.getBasePath().toLowerCase().endsWith("gif") || fileDataMetaTempVO.getBasePath().toLowerCase().endsWith("jpg")
|| fileDataMetaTempVO.getBasePath().toLowerCase().endsWith("jpeg") || fileDataMetaTempVO.getBasePath().toLowerCase().endsWith("bmp")){
if (imageType.toLowerCase().equals("jpg")){
response.setContentType("image/jpeg" );
}else if(imageType.toLowerCase().equals("bmp")){
response.setContentType("application/x-bmp" );
}else{
response.setContentType("image/"+imageType );
}
out=response.getOutputStream();
BufferedImage bufferedImage= ImageIO.read(new ByteArrayInputStream(bytes));
ImageIO.write(bufferedImage,imageType.toLowerCase(),out);
}else{
response.setContentType("application/octet-stream" );
out=response.getOutputStream();
out.write(bytes);
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
浙公网安备 33010602011771号