| | |
| | | |
| | | @Override |
| | | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { |
| | | HttpServletRequest request = (HttpServletRequest) servletRequest; |
| | | HttpServletResponse response = (HttpServletResponse) servletResponse; |
| | | String reqUri = request.getRequestURI(); |
| | | synchronized (this) { |
| | | HttpServletRequest request = (HttpServletRequest) servletRequest; |
| | | HttpServletResponse response = (HttpServletResponse) servletResponse; |
| | | String reqUri = request.getRequestURI(); |
| | | |
| | | String hostUrl = SettingKey.getHostUrl(request); |
| | | String queryString = request.getQueryString(); |
| | | String hostUrl = SettingKey.getHostUrl(request); |
| | | String queryString = request.getQueryString(); |
| | | |
| | | HttpSession session = request.getSession(); |
| | | String rootPath = session.getServletContext().getRealPath("/"); |
| | | ImageUrlParametersEntity imageUrlParametersEntity = getImageUrlParameters(reqUri); |
| | | File file = new File(rootPath.substring(0, rootPath.length() - 1) + reqUri); |
| | | try { |
| | | if (file != null && file.exists()) { |
| | | filterChain.doFilter(request, response); |
| | | return; |
| | | } |
| | | |
| | | MailFileIfc mailFileIfc= (MailFileIfc) FactoryBean.getBean("MailFileImpl"); |
| | | ImageEntity image = mailFileIfc.getImageFile( |
| | | imageUrlParametersEntity.getUnids(), |
| | | imageUrlParametersEntity.getWidth(), |
| | | imageUrlParametersEntity.getHeight(), |
| | | (imageUrlParametersEntity.getWidth() == null |
| | | && imageUrlParametersEntity.getHeight() == null ? true : false), |
| | | request, imageUrlParametersEntity.getAttachmentPath(), imageUrlParametersEntity.getFormidPath()); |
| | | if (image == null) { |
| | | System.out.println("输出缩略图时出错,出错文件reqUri:" + hostUrl + reqUri + (queryString != null && !"".equals(queryString) ? "?" + queryString : "")); |
| | | return; |
| | | } |
| | | |
| | | if (image != null && image.getOriginalFileName() != null && ( |
| | | image.getOriginalFileName().toLowerCase().endsWith(".pdf") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".doc") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".xls") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".ppt") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".docx") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".xlsx") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".pptx") |
| | | )) { |
| | | //不是图片格式,附件打开预览会自动下载,在这里处理直接跳过下面的生成输出流 by danaus 2023-07-10 09:49 |
| | | filterChain.doFilter(request, response); |
| | | return; |
| | | } else { |
| | | //图片格式,直接显示 |
| | | response.setStatus(HttpServletResponse.SC_OK); |
| | | InputStream is = null; |
| | | try { |
| | | is = new FileInputStream(image.getFile()); |
| | | //设置页面另存为时的文件名 |
| | | response.setHeader("Content-Disposition", "inline; filename=" |
| | | + (image != null && image.getOriginalFileName() != null ? new String(image.getOriginalFileName().getBytes("utf-8"), "ISO_8859_1") : "")); |
| | | String type = "image/"; |
| | | //输出流 |
| | | response.setContentType(type + image.getFileType()); |
| | | OutputStream output = response.getOutputStream(); |
| | | try { |
| | | FileCopyUtils.copy(is, output); |
| | | } catch (ClientAbortException | IllegalStateException e) { |
| | | System.out.println(getClass() + " reqUri:" + reqUri + ", Error:" + e.getMessage()); |
| | | } |
| | | HttpSession session = request.getSession(); |
| | | String rootPath = session.getServletContext().getRealPath("/"); |
| | | ImageUrlParametersEntity imageUrlParametersEntity = getImageUrlParameters(reqUri); |
| | | File file = new File(rootPath.substring(0, rootPath.length() - 1) + reqUri); |
| | | try { |
| | | if (file != null && file.exists()) { |
| | | filterChain.doFilter(request, response); |
| | | return; |
| | | // 这里不能再执行 chain.doFilter(request, response) , 因为是新生成的文件,如果用 chain.doFilter 输出,则浏览器收到 404 文件不存的错误 , |
| | | // 为了解决这个问题,使用 response 流 Stream 输出解决 |
| | | //chain.doFilter(request, response); |
| | | } catch (Exception e) { |
| | | System.out.println("输出缩略图时出错,出错文件reqUri:" + hostUrl + reqUri + (queryString != null && !"".equals(queryString) ? "?" + queryString : "") + ",错误原因:" + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage())); |
| | | e.printStackTrace(); |
| | | } finally { |
| | | } |
| | | |
| | | MailFileIfc mailFileIfc = (MailFileIfc) FactoryBean.getBean("MailFileImpl"); |
| | | ImageEntity image = mailFileIfc.getImageFile( |
| | | imageUrlParametersEntity.getUnids(), |
| | | imageUrlParametersEntity.getWidth(), |
| | | imageUrlParametersEntity.getHeight(), |
| | | (imageUrlParametersEntity.getWidth() == null |
| | | && imageUrlParametersEntity.getHeight() == null ? true : false), |
| | | request, imageUrlParametersEntity.getAttachmentPath(), imageUrlParametersEntity.getFormidPath()); |
| | | if (image == null) { |
| | | System.out.println("输出缩略图时出错,出错文件reqUri:" + hostUrl + reqUri + (queryString != null && !"".equals(queryString) ? "?" + queryString : "")); |
| | | return; |
| | | } |
| | | |
| | | if (image != null && image.getOriginalFileName() != null && ( |
| | | image.getOriginalFileName().toLowerCase().endsWith(".pdf") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".doc") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".xls") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".ppt") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".docx") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".xlsx") |
| | | || image.getOriginalFileName().toLowerCase().endsWith(".pptx") |
| | | )) { |
| | | //不是图片格式,附件打开预览会自动下载,在这里处理直接跳过下面的生成输出流 by danaus 2023-07-10 09:49 |
| | | filterChain.doFilter(request, response); |
| | | return; |
| | | } else { |
| | | //图片格式,直接显示 |
| | | response.setStatus(HttpServletResponse.SC_OK); |
| | | InputStream is = null; |
| | | try { |
| | | if (is != null) is.close(); |
| | | } catch (IOException e) { |
| | | is = new FileInputStream(image.getFile()); |
| | | //设置页面另存为时的文件名 |
| | | response.setHeader("Content-Disposition", "inline; filename=" |
| | | + (image != null && image.getOriginalFileName() != null ? new String(image.getOriginalFileName().getBytes("utf-8"), "ISO_8859_1") : "")); |
| | | String type = "image/"; |
| | | //输出流 |
| | | response.setContentType(type + image.getFileType()); |
| | | OutputStream output = response.getOutputStream(); |
| | | try { |
| | | FileCopyUtils.copy(is, output); |
| | | } catch (ClientAbortException | IllegalStateException e) { |
| | | System.out.println(getClass() + " reqUri:" + reqUri + ", Error:" + e.getMessage()); |
| | | } |
| | | return; |
| | | // 这里不能再执行 chain.doFilter(request, response) , 因为是新生成的文件,如果用 chain.doFilter 输出,则浏览器收到 404 文件不存的错误 , |
| | | // 为了解决这个问题,使用 response 流 Stream 输出解决 |
| | | //chain.doFilter(request, response); |
| | | } catch (Exception e) { |
| | | System.out.println("输出缩略图时出错,出错文件reqUri:" + hostUrl + reqUri + (queryString != null && !"".equals(queryString) ? "?" + queryString : "") + ",错误原因:" + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage())); |
| | | e.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | if (is != null) is.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | servletResponse.setContentType("text/html;charset=utf-8"); |
| | | PrintWriter out = servletResponse.getWriter(); |
| | | out.print(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
| | | out.flush(); |
| | | out.close(); |
| | | } |
| | | } catch (Exception e) { |
| | | servletResponse.setContentType("text/html;charset=utf-8"); |
| | | PrintWriter out = servletResponse.getWriter(); |
| | | out.print(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
| | | out.flush(); |
| | | out.close(); |
| | | } |
| | | } |
| | | |