beetlsql3-dev
Mlxa0324 2 years ago
parent debffde1bf
commit c665201b2d

@ -1,119 +1,119 @@
package cn.jlw.cors; //package cn.jlw.cors;
//
import org.apache.commons.lang3.StringUtils; //import org.apache.commons.lang3.StringUtils;
//
import javax.servlet.*; //import javax.servlet.*;
import javax.servlet.annotation.WebFilter; //import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam; //import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest; //import javax.servlet.http.HttpServletRequest;
import java.io.IOException; //import java.io.IOException;
//
import static com.ibeetl.jlw.job.ImageAutoJob.booleanMap; //import static com.ibeetl.jlw.job.ImageAutoJob.booleanMap;
//
/** ///**
* // * 修改图片请求路由
**/ // **/
//
@WebFilter(filterName = "ImageUrlFilter", urlPatterns = "/*", //@WebFilter(filterName = "ImageUrlFilter", urlPatterns = "/*",
initParams = {@WebInitParam(name = "allowOrigin", value = "*"), // initParams = {@WebInitParam(name = "allowOrigin", value = "*"),
@WebInitParam(name = "allowMethods", value = "GET,POST,PUT,DELETE,OPTIONS"), // @WebInitParam(name = "allowMethods", value = "GET,POST,PUT,DELETE,OPTIONS"),
@WebInitParam(name = "allowCredentials", value = "true"), // @WebInitParam(name = "allowCredentials", value = "true"),
@WebInitParam(name = "allowHeaders", value = "Content-Type,X-Token,token,isWx,_sign"), // @WebInitParam(name = "allowHeaders", value = "Content-Type,X-Token,token,isWx,_sign"),
}) // })
//
//
public class ImageUrlFilter implements Filter { //public class ImageUrlFilter implements Filter {
@Override // @Override
public void init(FilterConfig filterConfig) throws ServletException { // public void init(FilterConfig filterConfig) throws ServletException {
} // }
//
@Override // @Override
public void destroy() { // public void destroy() {
} // }
//
@Override // @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request; // HttpServletRequest httpRequest = (HttpServletRequest)request;
//
try{ // try{
// 从 http 请求头中取出 isWx // // 从 http 请求头中取出 isWx
Integer isWx = null; // Integer isWx = null;
if(StringUtils.isBlank(httpRequest.getHeader("isWx")) || "null".equals(httpRequest.getHeader("isWx"))){ // if(StringUtils.isBlank(httpRequest.getHeader("isWx")) || "null".equals(httpRequest.getHeader("isWx"))){
if(null == isWx && StringUtils.isNotBlank(httpRequest.getHeader("User-Agent"))){ // if(null == isWx && StringUtils.isNotBlank(httpRequest.getHeader("User-Agent"))){
String ua = httpRequest.getHeader("User-Agent").toLowerCase(); // String ua = httpRequest.getHeader("User-Agent").toLowerCase();
if (ua.indexOf("micromessenger") > -1) { // if (ua.indexOf("micromessenger") > -1) {
isWx = 1; // isWx = 1;
}else { // }else {
isWx = 2; // isWx = 2;
} // }
} // }
}else { // }else {
isWx = Integer.parseInt(httpRequest.getHeader("isWx")); // isWx = Integer.parseInt(httpRequest.getHeader("isWx"));
} // }
httpRequest.setAttribute("r_isWx",isWx); // httpRequest.setAttribute("r_isWx",isWx);
}catch (Exception e){ // }catch (Exception e){
e.printStackTrace(); // e.printStackTrace();
} // }
//
//
try{ // try{
//判断是否为手机访问 // //判断是否为手机访问
Integer isMobile = null; // Integer isMobile = null;
String[] mobileAgents = { "iphone", "android", "phone", "mobile", "wap", "netfront", "java", "opera mobi", // String[] mobileAgents = { "iphone", "android", "phone", "mobile", "wap", "netfront", "java", "opera mobi",
"opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod", // "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod",
"nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma", // "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma",
"docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", // "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos",
"techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", // "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem",
"wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", // "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos",
"pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320", // "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320",
"240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", // "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", // "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", // "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", // "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", // "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", // "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-", // "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-",
"Googlebot-Mobile" }; // "Googlebot-Mobile" };
if (httpRequest.getHeader("User-Agent") != null) { // if (httpRequest.getHeader("User-Agent") != null) {
for (String mobileAgent : mobileAgents) { // for (String mobileAgent : mobileAgents) {
if (httpRequest.getHeader("User-Agent").toLowerCase().indexOf(mobileAgent) >= 0) { // if (httpRequest.getHeader("User-Agent").toLowerCase().indexOf(mobileAgent) >= 0) {
isMobile = 1; // isMobile = 1;
break; // break;
} // }
} // }
} // }
httpRequest.setAttribute("r_isMobile",isMobile); // httpRequest.setAttribute("r_isMobile",isMobile);
}catch (Exception e){ // }catch (Exception e){
e.printStackTrace(); // e.printStackTrace();
} // }
//
//图片转发 // //图片转发
boolean b = true; // boolean b = true;
if( ((Integer)1).equals(httpRequest.getAttribute("r_isWx")) || ((Integer)1).equals(httpRequest.getAttribute("r_isMobile")) ){ // if( ((Integer)1).equals(httpRequest.getAttribute("r_isWx")) || ((Integer)1).equals(httpRequest.getAttribute("r_isMobile")) ){
if(StringUtils.isNotBlank(httpRequest.getRequestURI()) // if(StringUtils.isNotBlank(httpRequest.getRequestURI())
&&(httpRequest.getRequestURI().toLowerCase().contains(".png") // &&(httpRequest.getRequestURI().toLowerCase().contains(".png")
|| httpRequest.getRequestURI().toLowerCase().contains(".jpeg") // || httpRequest.getRequestURI().toLowerCase().contains(".jpeg")
|| httpRequest.getRequestURI().toLowerCase().contains(".jpg")) // || httpRequest.getRequestURI().toLowerCase().contains(".jpg"))
&& !httpRequest.getRequestURI().contains("compress") // && !httpRequest.getRequestURI().contains("compress")
&& !httpRequest.getRequestURI().contains("goodsQRCode") // && !httpRequest.getRequestURI().contains("goodsQRCode")
&& !httpRequest.getRequestURI().contains("shareQRCode") // && !httpRequest.getRequestURI().contains("shareQRCode")
){ // ){
String path=httpRequest.getRequestURI(); // String path=httpRequest.getRequestURI();
System.out.println(path); // System.out.println(path);
path = path.replace("/filesystem/temp/","/filesystem/temp/compress/").replace("server/",""); // path = path.replace("/filesystem/temp/","/filesystem/temp/compress/").replace("server/","");
System.out.println(path); // System.out.println(path);
String fileName = path.substring(path.lastIndexOf("/")+1); // String fileName = path.substring(path.lastIndexOf("/")+1);
if(null != booleanMap.get(fileName) && booleanMap.get(fileName)){ // if(null != booleanMap.get(fileName) && booleanMap.get(fileName)){
b = false; // b = false;
httpRequest.getRequestDispatcher(path).forward(request,response); // httpRequest.getRequestDispatcher(path).forward(request,response);
} // }
} // }
} // }
//
if(b) { // if(b) {
chain.doFilter(request, response); // chain.doFilter(request, response);
} // }
//
return; // return;
} // }
} //}

@ -1,152 +1,152 @@
package com.ibeetl.jlw.job; //package com.ibeetl.jlw.job;
//
import cn.jlw.util.ToolUtils; //import cn.jlw.util.ToolUtils;
import com.sun.image.codec.jpeg.JPEGCodec; //import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam; //import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder; //import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.commons.logging.Log; //import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; //import org.apache.commons.logging.LogFactory;
import org.quartz.Job; //import org.quartz.Job;
import org.quartz.JobExecutionContext; //import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; //import org.quartz.JobExecutionException;
//
import java.awt.image.BufferedImage; //import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp; //import java.awt.image.ConvolveOp;
import java.awt.image.Kernel; //import java.awt.image.Kernel;
import java.io.File; //import java.io.File;
import java.io.FileOutputStream; //import java.io.FileOutputStream;
import java.math.BigDecimal; //import java.math.BigDecimal;
import java.math.RoundingMode; //import java.math.RoundingMode;
import java.util.Map; //import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; //import java.util.concurrent.ConcurrentHashMap;
//
import static cn.jlw.Interceptor.GetFile.p; //import static cn.jlw.Interceptor.GetFile.p;
import static java.lang.Thread.sleep; //import static java.lang.Thread.sleep;
//
public class ImageAutoJob implements Job { //public class ImageAutoJob implements Job {
//
private final Log log = LogFactory.getLog(this.getClass()); // private final Log log = LogFactory.getLog(this.getClass());
//
private static boolean createCompress = true; // private static boolean createCompress = true;
//
public static Map<String,Boolean>booleanMap = new ConcurrentHashMap(); // public static Map<String,Boolean>booleanMap = new ConcurrentHashMap();
//
public void execute(JobExecutionContext context) throws JobExecutionException { // public void execute(JobExecutionContext context) throws JobExecutionException {
//
//图片生成缩略图 // //图片生成缩略图
try { // try {
if(createCompress){ // if(createCompress){
createCompress = false; // createCompress = false;
try{ // try{
File files = new File(p+"\\filesystem\\temp\\"); // File files = new File(p+"\\filesystem\\temp\\");
File compressFiles = new File(p+"\\filesystem\\temp\\compress\\"); // File compressFiles = new File(p+"\\filesystem\\temp\\compress\\");
if(files.exists() && files.canRead() && compressFiles.exists() && compressFiles.canRead()){ // if(files.exists() && files.canRead() && compressFiles.exists() && compressFiles.canRead()){
File file[] = files.listFiles(); // File file[] = files.listFiles();
File compressFile[] = compressFiles.listFiles(); // File compressFile[] = compressFiles.listFiles();
for(int i=0,num=0;i<file.length;i++){ // for(int i=0,num=0;i<file.length;i++){
boolean b = true; // boolean b = true;
if(file[i].isFile() && (file[i].getName().toLowerCase().contains(".jpg") // if(file[i].isFile() && (file[i].getName().toLowerCase().contains(".jpg")
|| file[i].getName().toLowerCase().contains(".jpeg") // || file[i].getName().toLowerCase().contains(".jpeg")
|| file[i].getName().toLowerCase().contains(".png")) ){ // || file[i].getName().toLowerCase().contains(".png")) ){
if(null != booleanMap.get(file[i].getName()) && booleanMap.get(file[i].getName())){ // if(null != booleanMap.get(file[i].getName()) && booleanMap.get(file[i].getName())){
//TODO 如果真不存在了,缓存可能还存在,有误 // //TODO 如果真不存在了,缓存可能还存在,有误
b = false; // b = false;
}else { // }else {
for(int j=0;j<compressFile.length;j++){ // for(int j=0;j<compressFile.length;j++){
if(file[i].getName().equals(compressFile[j].getName())){ // if(file[i].getName().equals(compressFile[j].getName())){
booleanMap.put(file[i].getName(),true); // booleanMap.put(file[i].getName(),true);
b = false; // b = false;
break; // break;
} // }
} // }
} // }
FileOutputStream out = null; // FileOutputStream out = null;
try { // try {
if(b){ // if(b){
//
//获取到文件的后缀名 // //获取到文件的后缀名
String fileName = file[i].getName(); // String fileName = file[i].getName();
String formatName = fileName.substring(fileName.lastIndexOf(".") + 1); // String formatName = fileName.substring(fileName.lastIndexOf(".") + 1);
//
File source_file = file[i]; // File source_file = file[i];
File result_file = new File(p+"\\filesystem\\temp\\compress\\"+file[i].getName()); // File result_file = new File(p+"\\filesystem\\temp\\compress\\"+file[i].getName());
BufferedImage bufferedImage = ToolUtils.imageIoRead(source_file); // BufferedImage bufferedImage = ToolUtils.imageIoRead(source_file);
//
//压缩 // //压缩
{ // {
float softenFactor = 0.05f; // float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor, // float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 }; // 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray); // Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); // ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null); // bufferedImage = cOp.filter(bufferedImage, null);
//
// Write the jpeg to a file. // // Write the jpeg to a file.
out = new FileOutputStream(result_file); // out = new FileOutputStream(result_file);
//
// Encodes image as a JPEG data stream // // Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//
JPEGEncodeParam param = encoder // JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(bufferedImage); // .getDefaultJPEGEncodeParam(bufferedImage);
//
System.out.print(fileName+" : "+new BigDecimal(source_file.length()/1024.0).setScale(2, RoundingMode.HALF_UP)); // System.out.print(fileName+" : "+new BigDecimal(source_file.length()/1024.0).setScale(2, RoundingMode.HALF_UP));
if(source_file.length() > 2097152){ // 大于2M // if(source_file.length() > 2097152){ // 大于2M
param.setQuality(0.1f, true); // param.setQuality(0.1f, true);
}else if(source_file.length() > 2097152/2){ // 大于1M // }else if(source_file.length() > 2097152/2){ // 大于1M
param.setQuality(0.2f, true); // param.setQuality(0.2f, true);
}else if(source_file.length() > 2097152/4 ){// 大于512KB // }else if(source_file.length() > 2097152/4 ){// 大于512KB
param.setQuality(0.5f, true); // param.setQuality(0.5f, true);
}else if(source_file.length() > 2097152/6 ){// 大于256KB // }else if(source_file.length() > 2097152/6 ){// 大于256KB
param.setQuality(0.6f, true); // param.setQuality(0.6f, true);
}else if(source_file.length() > 2097152/8 ){// 大于128KB // }else if(source_file.length() > 2097152/8 ){// 大于128KB
param.setQuality(0.7f, true); // param.setQuality(0.7f, true);
}else { // }else {
param.setQuality(0.9f, true); // param.setQuality(0.9f, true);
} // }
//
encoder.setJPEGEncodeParam(param); // encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage); // encoder.encode(bufferedImage);
//
bufferedImage.flush(); // bufferedImage.flush();
sleep(300); // sleep(300);
num++; // num++;
} // }
System.out.println(" - "+new BigDecimal(result_file.length()/1024.0).setScale(2,RoundingMode.HALF_UP)); // System.out.println(" - "+new BigDecimal(result_file.length()/1024.0).setScale(2,RoundingMode.HALF_UP));
} // }
}catch (Exception e){ // }catch (Exception e){
if(!e.toString().contains("Width (-1) and height (-1) cannot be <= 0")){ // if(!e.toString().contains("Width (-1) and height (-1) cannot be <= 0")){
e.printStackTrace(); // e.printStackTrace();
log.error("已经遍历到"+i); // log.error("已经遍历到"+i);
} // }
continue; // continue;
}finally { // }finally {
try { // try {
if(null != out){ // if(null != out){
out.close(); // out.close();
} // }
}catch (Exception e){} // }catch (Exception e){}
} // }
} // }
if(b && num!=0 && num%1000 == 0){ // if(b && num!=0 && num%1000 == 0){
sleep(2000); // sleep(2000);
System.gc(); // System.gc();
sleep(5000); // sleep(5000);
} // }
} // }
} // }
}catch (Exception e){ // }catch (Exception e){
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
}catch (Exception e){ // }catch (Exception e){
e.printStackTrace(); // e.printStackTrace();
log.error("商品图片生成缩略图"+e.toString()); // log.error("商品图片生成缩略图"+e.toString());
}finally { // }finally {
createCompress = true; // createCompress = true;
} // }
//
//
} // }
//
} //}
Loading…
Cancel
Save