From c665201b2ded0e181bd7c32f3de8c98a0fec1175 Mon Sep 17 00:00:00 2001 From: Mlxa0324 Date: Tue, 25 Oct 2022 23:38:04 +0800 Subject: [PATCH] 123 --- .../main/java/cn/jlw/cors/ImageUrlFilter.java | 238 +++++++------- .../java/com/ibeetl/jlw/job/ImageAutoJob.java | 304 +++++++++--------- 2 files changed, 271 insertions(+), 271 deletions(-) diff --git a/web/src/main/java/cn/jlw/cors/ImageUrlFilter.java b/web/src/main/java/cn/jlw/cors/ImageUrlFilter.java index f9334663..8727bf89 100644 --- a/web/src/main/java/cn/jlw/cors/ImageUrlFilter.java +++ b/web/src/main/java/cn/jlw/cors/ImageUrlFilter.java @@ -1,119 +1,119 @@ -package cn.jlw.cors; - -import org.apache.commons.lang3.StringUtils; - -import javax.servlet.*; -import javax.servlet.annotation.WebFilter; -import javax.servlet.annotation.WebInitParam; -import javax.servlet.http.HttpServletRequest; -import java.io.IOException; - -import static com.ibeetl.jlw.job.ImageAutoJob.booleanMap; - -/** - * 修改图片请求路由 - **/ - -@WebFilter(filterName = "ImageUrlFilter", urlPatterns = "/*", - initParams = {@WebInitParam(name = "allowOrigin", value = "*"), - @WebInitParam(name = "allowMethods", value = "GET,POST,PUT,DELETE,OPTIONS"), - @WebInitParam(name = "allowCredentials", value = "true"), - @WebInitParam(name = "allowHeaders", value = "Content-Type,X-Token,token,isWx,_sign"), - }) - - -public class ImageUrlFilter implements Filter { - @Override - public void init(FilterConfig filterConfig) throws ServletException { - } - - @Override - public void destroy() { - } - - @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - HttpServletRequest httpRequest = (HttpServletRequest)request; - - try{ - // 从 http 请求头中取出 isWx - Integer isWx = null; - if(StringUtils.isBlank(httpRequest.getHeader("isWx")) || "null".equals(httpRequest.getHeader("isWx"))){ - if(null == isWx && StringUtils.isNotBlank(httpRequest.getHeader("User-Agent"))){ - String ua = httpRequest.getHeader("User-Agent").toLowerCase(); - if (ua.indexOf("micromessenger") > -1) { - isWx = 1; - }else { - isWx = 2; - } - } - }else { - isWx = Integer.parseInt(httpRequest.getHeader("isWx")); - } - httpRequest.setAttribute("r_isWx",isWx); - }catch (Exception e){ - e.printStackTrace(); - } - - - try{ - //判断是否为手机访问 - Integer isMobile = null; - String[] mobileAgents = { "iphone", "android", "phone", "mobile", "wap", "netfront", "java", "opera mobi", - "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod", - "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma", - "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", - "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", - "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", - "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320", - "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", - "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", - "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", - "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", - "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", - "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v", - "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-", - "Googlebot-Mobile" }; - if (httpRequest.getHeader("User-Agent") != null) { - for (String mobileAgent : mobileAgents) { - if (httpRequest.getHeader("User-Agent").toLowerCase().indexOf(mobileAgent) >= 0) { - isMobile = 1; - break; - } - } - } - httpRequest.setAttribute("r_isMobile",isMobile); - }catch (Exception e){ - e.printStackTrace(); - } - - //图片转发 - boolean b = true; - if( ((Integer)1).equals(httpRequest.getAttribute("r_isWx")) || ((Integer)1).equals(httpRequest.getAttribute("r_isMobile")) ){ - if(StringUtils.isNotBlank(httpRequest.getRequestURI()) - &&(httpRequest.getRequestURI().toLowerCase().contains(".png") - || httpRequest.getRequestURI().toLowerCase().contains(".jpeg") - || httpRequest.getRequestURI().toLowerCase().contains(".jpg")) - && !httpRequest.getRequestURI().contains("compress") - && !httpRequest.getRequestURI().contains("goodsQRCode") - && !httpRequest.getRequestURI().contains("shareQRCode") - ){ - String path=httpRequest.getRequestURI(); - System.out.println(path); - path = path.replace("/filesystem/temp/","/filesystem/temp/compress/").replace("server/",""); - System.out.println(path); - String fileName = path.substring(path.lastIndexOf("/")+1); - if(null != booleanMap.get(fileName) && booleanMap.get(fileName)){ - b = false; - httpRequest.getRequestDispatcher(path).forward(request,response); - } - } - } - - if(b) { - chain.doFilter(request, response); - } - - return; - } -} \ No newline at end of file +//package cn.jlw.cors; +// +//import org.apache.commons.lang3.StringUtils; +// +//import javax.servlet.*; +//import javax.servlet.annotation.WebFilter; +//import javax.servlet.annotation.WebInitParam; +//import javax.servlet.http.HttpServletRequest; +//import java.io.IOException; +// +//import static com.ibeetl.jlw.job.ImageAutoJob.booleanMap; +// +///** +// * 修改图片请求路由 +// **/ +// +//@WebFilter(filterName = "ImageUrlFilter", urlPatterns = "/*", +// initParams = {@WebInitParam(name = "allowOrigin", value = "*"), +// @WebInitParam(name = "allowMethods", value = "GET,POST,PUT,DELETE,OPTIONS"), +// @WebInitParam(name = "allowCredentials", value = "true"), +// @WebInitParam(name = "allowHeaders", value = "Content-Type,X-Token,token,isWx,_sign"), +// }) +// +// +//public class ImageUrlFilter implements Filter { +// @Override +// public void init(FilterConfig filterConfig) throws ServletException { +// } +// +// @Override +// public void destroy() { +// } +// +// @Override +// public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { +// HttpServletRequest httpRequest = (HttpServletRequest)request; +// +// try{ +// // 从 http 请求头中取出 isWx +// Integer isWx = null; +// if(StringUtils.isBlank(httpRequest.getHeader("isWx")) || "null".equals(httpRequest.getHeader("isWx"))){ +// if(null == isWx && StringUtils.isNotBlank(httpRequest.getHeader("User-Agent"))){ +// String ua = httpRequest.getHeader("User-Agent").toLowerCase(); +// if (ua.indexOf("micromessenger") > -1) { +// isWx = 1; +// }else { +// isWx = 2; +// } +// } +// }else { +// isWx = Integer.parseInt(httpRequest.getHeader("isWx")); +// } +// httpRequest.setAttribute("r_isWx",isWx); +// }catch (Exception e){ +// e.printStackTrace(); +// } +// +// +// try{ +// //判断是否为手机访问 +// Integer isMobile = null; +// String[] mobileAgents = { "iphone", "android", "phone", "mobile", "wap", "netfront", "java", "opera mobi", +// "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod", +// "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma", +// "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", +// "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", +// "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", +// "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320", +// "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", +// "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", +// "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", +// "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", +// "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", +// "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v", +// "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-", +// "Googlebot-Mobile" }; +// if (httpRequest.getHeader("User-Agent") != null) { +// for (String mobileAgent : mobileAgents) { +// if (httpRequest.getHeader("User-Agent").toLowerCase().indexOf(mobileAgent) >= 0) { +// isMobile = 1; +// break; +// } +// } +// } +// httpRequest.setAttribute("r_isMobile",isMobile); +// }catch (Exception e){ +// e.printStackTrace(); +// } +// +// //图片转发 +// boolean b = true; +// if( ((Integer)1).equals(httpRequest.getAttribute("r_isWx")) || ((Integer)1).equals(httpRequest.getAttribute("r_isMobile")) ){ +// if(StringUtils.isNotBlank(httpRequest.getRequestURI()) +// &&(httpRequest.getRequestURI().toLowerCase().contains(".png") +// || httpRequest.getRequestURI().toLowerCase().contains(".jpeg") +// || httpRequest.getRequestURI().toLowerCase().contains(".jpg")) +// && !httpRequest.getRequestURI().contains("compress") +// && !httpRequest.getRequestURI().contains("goodsQRCode") +// && !httpRequest.getRequestURI().contains("shareQRCode") +// ){ +// String path=httpRequest.getRequestURI(); +// System.out.println(path); +// path = path.replace("/filesystem/temp/","/filesystem/temp/compress/").replace("server/",""); +// System.out.println(path); +// String fileName = path.substring(path.lastIndexOf("/")+1); +// if(null != booleanMap.get(fileName) && booleanMap.get(fileName)){ +// b = false; +// httpRequest.getRequestDispatcher(path).forward(request,response); +// } +// } +// } +// +// if(b) { +// chain.doFilter(request, response); +// } +// +// return; +// } +//} \ No newline at end of file diff --git a/web/src/main/java/com/ibeetl/jlw/job/ImageAutoJob.java b/web/src/main/java/com/ibeetl/jlw/job/ImageAutoJob.java index 485d9b45..420d75c0 100644 --- a/web/src/main/java/com/ibeetl/jlw/job/ImageAutoJob.java +++ b/web/src/main/java/com/ibeetl/jlw/job/ImageAutoJob.java @@ -1,152 +1,152 @@ -package com.ibeetl.jlw.job; - -import cn.jlw.util.ToolUtils; -import com.sun.image.codec.jpeg.JPEGCodec; -import com.sun.image.codec.jpeg.JPEGEncodeParam; -import com.sun.image.codec.jpeg.JPEGImageEncoder; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.quartz.Job; -import org.quartz.JobExecutionContext; -import org.quartz.JobExecutionException; - -import java.awt.image.BufferedImage; -import java.awt.image.ConvolveOp; -import java.awt.image.Kernel; -import java.io.File; -import java.io.FileOutputStream; -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import static cn.jlw.Interceptor.GetFile.p; -import static java.lang.Thread.sleep; - -public class ImageAutoJob implements Job { - - private final Log log = LogFactory.getLog(this.getClass()); - - private static boolean createCompress = true; - - public static MapbooleanMap = new ConcurrentHashMap(); - - public void execute(JobExecutionContext context) throws JobExecutionException { - - //图片生成缩略图 - try { - if(createCompress){ - createCompress = false; - try{ - File files = new File(p+"\\filesystem\\temp\\"); - File compressFiles = new File(p+"\\filesystem\\temp\\compress\\"); - if(files.exists() && files.canRead() && compressFiles.exists() && compressFiles.canRead()){ - File file[] = files.listFiles(); - File compressFile[] = compressFiles.listFiles(); - for(int i=0,num=0;i 2097152){ // 大于2M - param.setQuality(0.1f, true); - }else if(source_file.length() > 2097152/2){ // 大于1M - param.setQuality(0.2f, true); - }else if(source_file.length() > 2097152/4 ){// 大于512KB - param.setQuality(0.5f, true); - }else if(source_file.length() > 2097152/6 ){// 大于256KB - param.setQuality(0.6f, true); - }else if(source_file.length() > 2097152/8 ){// 大于128KB - param.setQuality(0.7f, true); - }else { - param.setQuality(0.9f, true); - } - - encoder.setJPEGEncodeParam(param); - encoder.encode(bufferedImage); - - bufferedImage.flush(); - sleep(300); - num++; - } - System.out.println(" - "+new BigDecimal(result_file.length()/1024.0).setScale(2,RoundingMode.HALF_UP)); - } - }catch (Exception e){ - if(!e.toString().contains("Width (-1) and height (-1) cannot be <= 0")){ - e.printStackTrace(); - log.error("已经遍历到"+i); - } - continue; - }finally { - try { - if(null != out){ - out.close(); - } - }catch (Exception e){} - } - } - if(b && num!=0 && num%1000 == 0){ - sleep(2000); - System.gc(); - sleep(5000); - } - } - } - }catch (Exception e){ - e.printStackTrace(); - } - } - }catch (Exception e){ - e.printStackTrace(); - log.error("商品图片生成缩略图"+e.toString()); - }finally { - createCompress = true; - } - - - } - -} \ No newline at end of file +//package com.ibeetl.jlw.job; +// +//import cn.jlw.util.ToolUtils; +//import com.sun.image.codec.jpeg.JPEGCodec; +//import com.sun.image.codec.jpeg.JPEGEncodeParam; +//import com.sun.image.codec.jpeg.JPEGImageEncoder; +//import org.apache.commons.logging.Log; +//import org.apache.commons.logging.LogFactory; +//import org.quartz.Job; +//import org.quartz.JobExecutionContext; +//import org.quartz.JobExecutionException; +// +//import java.awt.image.BufferedImage; +//import java.awt.image.ConvolveOp; +//import java.awt.image.Kernel; +//import java.io.File; +//import java.io.FileOutputStream; +//import java.math.BigDecimal; +//import java.math.RoundingMode; +//import java.util.Map; +//import java.util.concurrent.ConcurrentHashMap; +// +//import static cn.jlw.Interceptor.GetFile.p; +//import static java.lang.Thread.sleep; +// +//public class ImageAutoJob implements Job { +// +// private final Log log = LogFactory.getLog(this.getClass()); +// +// private static boolean createCompress = true; +// +// public static MapbooleanMap = new ConcurrentHashMap(); +// +// public void execute(JobExecutionContext context) throws JobExecutionException { +// +// //图片生成缩略图 +// try { +// if(createCompress){ +// createCompress = false; +// try{ +// File files = new File(p+"\\filesystem\\temp\\"); +// File compressFiles = new File(p+"\\filesystem\\temp\\compress\\"); +// if(files.exists() && files.canRead() && compressFiles.exists() && compressFiles.canRead()){ +// File file[] = files.listFiles(); +// File compressFile[] = compressFiles.listFiles(); +// for(int i=0,num=0;i 2097152){ // 大于2M +// param.setQuality(0.1f, true); +// }else if(source_file.length() > 2097152/2){ // 大于1M +// param.setQuality(0.2f, true); +// }else if(source_file.length() > 2097152/4 ){// 大于512KB +// param.setQuality(0.5f, true); +// }else if(source_file.length() > 2097152/6 ){// 大于256KB +// param.setQuality(0.6f, true); +// }else if(source_file.length() > 2097152/8 ){// 大于128KB +// param.setQuality(0.7f, true); +// }else { +// param.setQuality(0.9f, true); +// } +// +// encoder.setJPEGEncodeParam(param); +// encoder.encode(bufferedImage); +// +// bufferedImage.flush(); +// sleep(300); +// num++; +// } +// System.out.println(" - "+new BigDecimal(result_file.length()/1024.0).setScale(2,RoundingMode.HALF_UP)); +// } +// }catch (Exception e){ +// if(!e.toString().contains("Width (-1) and height (-1) cannot be <= 0")){ +// e.printStackTrace(); +// log.error("已经遍历到"+i); +// } +// continue; +// }finally { +// try { +// if(null != out){ +// out.close(); +// } +// }catch (Exception e){} +// } +// } +// if(b && num!=0 && num%1000 == 0){ +// sleep(2000); +// System.gc(); +// sleep(5000); +// } +// } +// } +// }catch (Exception e){ +// e.printStackTrace(); +// } +// } +// }catch (Exception e){ +// e.printStackTrace(); +// log.error("商品图片生成缩略图"+e.toString()); +// }finally { +// createCompress = true; +// } +// +// +// } +// +//} \ No newline at end of file