beetlsql3-dev
Mlxa0324 2 years ago
parent debffde1bf
commit c665201b2d

@ -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;
}
}
//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;
// }
//}

@ -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 Map<String,Boolean>booleanMap = 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<file.length;i++){
boolean b = true;
if(file[i].isFile() && (file[i].getName().toLowerCase().contains(".jpg")
|| file[i].getName().toLowerCase().contains(".jpeg")
|| file[i].getName().toLowerCase().contains(".png")) ){
if(null != booleanMap.get(file[i].getName()) && booleanMap.get(file[i].getName())){
//TODO 如果真不存在了,缓存可能还存在,有误
b = false;
}else {
for(int j=0;j<compressFile.length;j++){
if(file[i].getName().equals(compressFile[j].getName())){
booleanMap.put(file[i].getName(),true);
b = false;
break;
}
}
}
FileOutputStream out = null;
try {
if(b){
//获取到文件的后缀名
String fileName = file[i].getName();
String formatName = fileName.substring(fileName.lastIndexOf(".") + 1);
File source_file = file[i];
File result_file = new File(p+"\\filesystem\\temp\\compress\\"+file[i].getName());
BufferedImage bufferedImage = ToolUtils.imageIoRead(source_file);
//压缩
{
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
// Write the jpeg to a file.
out = new FileOutputStream(result_file);
// Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(bufferedImage);
System.out.print(fileName+" : "+new BigDecimal(source_file.length()/1024.0).setScale(2, RoundingMode.HALF_UP));
if(source_file.length() > 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;
}
}
}
//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 Map<String,Boolean>booleanMap = 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<file.length;i++){
// boolean b = true;
// if(file[i].isFile() && (file[i].getName().toLowerCase().contains(".jpg")
// || file[i].getName().toLowerCase().contains(".jpeg")
// || file[i].getName().toLowerCase().contains(".png")) ){
// if(null != booleanMap.get(file[i].getName()) && booleanMap.get(file[i].getName())){
// //TODO 如果真不存在了,缓存可能还存在,有误
// b = false;
// }else {
// for(int j=0;j<compressFile.length;j++){
// if(file[i].getName().equals(compressFile[j].getName())){
// booleanMap.put(file[i].getName(),true);
// b = false;
// break;
// }
// }
// }
// FileOutputStream out = null;
// try {
// if(b){
//
// //获取到文件的后缀名
// String fileName = file[i].getName();
// String formatName = fileName.substring(fileName.lastIndexOf(".") + 1);
//
// File source_file = file[i];
// File result_file = new File(p+"\\filesystem\\temp\\compress\\"+file[i].getName());
// BufferedImage bufferedImage = ToolUtils.imageIoRead(source_file);
//
// //压缩
// {
// float softenFactor = 0.05f;
// float[] softenArray = { 0, softenFactor, 0, softenFactor,
// 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
// Kernel kernel = new Kernel(3, 3, softenArray);
// ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
// bufferedImage = cOp.filter(bufferedImage, null);
//
// // Write the jpeg to a file.
// out = new FileOutputStream(result_file);
//
// // Encodes image as a JPEG data stream
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//
// JPEGEncodeParam param = encoder
// .getDefaultJPEGEncodeParam(bufferedImage);
//
// System.out.print(fileName+" : "+new BigDecimal(source_file.length()/1024.0).setScale(2, RoundingMode.HALF_UP));
// if(source_file.length() > 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;
// }
//
//
// }
//
//}
Loading…
Cancel
Save