Browse Source

优化创意上传代码

Signed-off-by: jiapeng.dong <jiapeng.dong@cloudcross.com>
jiapeng.dong 9 years ago
parent
commit
1f6b1c34c4

+ 15 - 0
src/main/java/com/cloudcross/ssp/service/IBannerTemplateService.java

@@ -2,6 +2,8 @@ package com.cloudcross.ssp.service;
 
 
 
+import java.io.File;
+import java.io.IOException;
 import java.util.*;
 
 import com.cloudcross.ssp.base.service.IGenericService;
@@ -37,4 +39,17 @@ public interface IBannerTemplateService extends IGenericService<BannerTemplate>
 	boolean updataIds(BannerTemplate t);
 
 	boolean deleteUpdata(List<Long> idList, int status);
+	/**
+	 * 上传创意方法
+	 * @author djp
+	 * */
+	
+	public String resetName(String uploadName);
+	public String getLocalFilePath(String fileName);
+	public String replacePath(String path);
+	public int judgeType(String fileType);
+	//生成缩略图
+	public void compressPicture(File localFile,String pathNew,int width,int height) throws IOException;
+	//匹配广告为宽高,用取平方差最小
+	public BannerTemplate suitSize(int width,int height,BannerTemplate bannerTemplate);
 }

+ 152 - 1
src/main/java/com/cloudcross/ssp/service/impl/BannerTemplateService.java

@@ -2,18 +2,32 @@ package com.cloudcross.ssp.service.impl;
 
 
 
+import java.awt.Image;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.imageio.ImageIO;
+import javax.servlet.ServletContext;
+
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import com.cloudcross.ssp.model.Account;
 import com.cloudcross.ssp.model.BannerTemplate;
+import com.cloudcross.ssp.model.Size;
 import com.cloudcross.ssp.service.IBannerTemplateService;
+import com.cloudcross.ssp.service.ISizeService;
 import com.cloudcross.ssp.base.dao.GenericIBatisDao;
+import com.cloudcross.ssp.base.web.SimpleController;
+import com.cloudcross.ssp.common.Config;
+import com.cloudcross.ssp.common.utils.DateUtil;
+import com.cloudcross.ssp.common.utils.DateUtil.DateFmts;
 import com.cloudcross.ssp.common.utils.MapBuilder;
 import com.cloudcross.ssp.common.utils.Pager;
 import com.cloudcross.ssp.common.utils.SqlHelper;
@@ -21,12 +35,16 @@ import com.cloudcross.ssp.common.utils.SqlHelper;
 
 
 @Service
-public class BannerTemplateService implements IBannerTemplateService {
+public class BannerTemplateService extends SimpleController implements IBannerTemplateService {
 
 	private static final Logger LOG = Logger.getLogger(BannerTemplateService.class);
 	
 	@Autowired
 	private GenericIBatisDao myBatisDao;
+	@Autowired
+	private ISizeService sizeService;
+	@Autowired
+	Config config;
 	
 	@Override
 	public BannerTemplate findById(Long id) {
@@ -268,6 +286,139 @@ public class BannerTemplateService implements IBannerTemplateService {
 		return myBatisDao.getList("operator.bannerTemplateSqlMapper.findByParams4operator", paramMap);
 	}
 
+	@Override
+	public String resetName(String uploadName) {
+		String fileType =  fileType = uploadName.substring(uploadName.lastIndexOf("."));
+		uploadName = new StringBuilder()
+					.append(System.currentTimeMillis())
+					.append(fileType)
+					.toString();
+		return uploadName;
+	}
+
+	@Override
+	public String getLocalFilePath(String fileName) {
+		Account loginUser = getLoginUser();
+		// 计算相对路径名,即不包括默认路径名
+		String path = new StringBuilder()
+				.append(File.separator)
+				.append(loginUser == null ? "anon" : loginUser.getId())							
+				.append(File.separator)
+				.append(DateUtil.getDate(DateFmts.YYYYMMDD))
+				.append(File.separator)
+				.append(System.currentTimeMillis())
+				.append(File.separator)
+				.append(fileName)
+				.toString();
+		
+		String localFilePath = config.getResourceUploadDir().concat(File.separator).concat("banner").concat(path);
+		
+//		// 计算在容器中实际路径名,添加文件所运行所在的实际目录
+		String accessUrl = localFilePath;
+		ServletContext servletContext = getServletContext();
+			if (servletContext == null) {
+				LOG.debug("单体测试情况,servletContext不存在。");
+			} else {
+			
+				accessUrl = servletContext.getRealPath("/");
+				File upFile = new File(accessUrl);
+				accessUrl = upFile.getParent();
+				LOG.info("\naaa----"+localFilePath+" -----"+accessUrl);
+		//		获取绝对路径
+				localFilePath = accessUrl.concat(File.separator).concat(localFilePath);
+		}
+		return localFilePath;
+	}
+
+	@Override
+	public String replacePath(String path) {
+		path = path.substring(path.indexOf("upload")-1);
+
+		return path;
+	}
+
+	@Override
+	public int judgeType(String fileType) {
+		int type = 2;
+		if("jpg".equalsIgnoreCase(fileType)||"jpeg".equalsIgnoreCase(fileType)||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType) ){
+			type = 2;
+		}else if("flv".equalsIgnoreCase(fileType)||"mp4".equalsIgnoreCase(fileType)){
+			type = 3;
+		}else{
+			type = 1;
+		}
+		return type;
+	}
+
+	@Override
+	public void compressPicture(File localFile, String pathNew, int width,
+			int height) throws IOException {
+		//新建缩略图文件
+				File newFile = new File(pathNew);
+				if (!newFile.getParentFile().exists()){
+					newFile.getParentFile().mkdirs();
+				}	
+				//构造image图片
+				Image src = javax.imageio.ImageIO.read(localFile);
+				//表示该图像具有打包成整数的像素的8 位RGB颜色分量
+				BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
+				//h绘制缩小后的图片
+				tag.getGraphics().drawImage(src, 0, 0, width, height, null);
+				//生成缩略图
+				ImageIO.write(tag, "png", newFile);
+	}
+
+	@Override
+	public BannerTemplate suitSize(int width, int height,
+			BannerTemplate bannerTemplate) {
+		//构造image图片
+
+				long max_width = 0;
+				long max_height = 0;
+				Size size = new Size();		
+				int i;
+				int n =0; 
+				//查找所有广告位
+				List<Size> sizeList = sizeService.findAllSize();
+				//取平方差
+				double suit[] = new double[sizeList.size()];
+				long _width[] = new long[sizeList.size()];
+				long _height[] = new long[sizeList.size()];
+				for ( i=0 ;i<sizeList.size();i++){
+					size = sizeList.get(i); 
+					_width[i] = size.getWidth(); 
+					_height[i] = size.getHeight();
+					if(width<_width[i]&&height<_height[i]){
+						suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
+					}else{
+						if((width*_height[i])==(_width[i]*height)){
+							suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
+						}else{
+						suit[i]=0;
+						}
+					}
+					
+					}
+				
+					//取最小值
+					double min=10000;
+					for (i=0;i<suit.length;i++){
+						if(suit[i]!=0){			
+							if (suit[i]<min){						
+								min = suit[i];
+								n=i;
+							}	
+						}	
+					}
+				if(min==10000){
+						bannerTemplate=null;
+					}else{
+			
+				bannerTemplate.setWidth(_width[n]);
+				bannerTemplate.setHeight(_height[n]);
+				}	
+				return bannerTemplate;
+	}
 	
 	
 

+ 17 - 154
src/main/java/com/cloudcross/ssp/web/advertiser/main/ad/BannerTemplateController.java

@@ -252,7 +252,7 @@ public class BannerTemplateController extends SimpleController {
 			if(file != null){ 
 				//取得当前上传文件的文件名称(不加路径)  
 				uploadName = file.getOriginalFilename();  
-				resetName = resetName(uploadName);
+				resetName = bannerTemplateService.resetName(uploadName);
 				//获取文件类型
 				 fileType = uploadName.substring(uploadName.lastIndexOf(".") + 1);
 				 
@@ -270,7 +270,7 @@ public class BannerTemplateController extends SimpleController {
 					
 					
 					//处理上传文件路径
-					path = getLocalFilePath(resetName);
+					path = bannerTemplateService.getLocalFilePath(resetName);
 					System.out.println(path);
 					System.out.println("qqqqqqq+==="+path);
 					
@@ -287,7 +287,7 @@ public class BannerTemplateController extends SimpleController {
 					 height = image.getHeight();
 					 
 					 //匹配广告位
-					 bannerTemplate=suitSize(width,height, bannerTemplate);
+					 bannerTemplate=bannerTemplateService.suitSize(width,height, bannerTemplate);
 					//图片匹配
 						if(bannerTemplate==null){
 							return "图片尺寸错啦!!";
@@ -300,24 +300,24 @@ public class BannerTemplateController extends SimpleController {
 				//生成缩略图路径名
 				pathSmall = absolutePath.concat(File.separator).concat("small_").concat(resetName);
 				//生成缩略图
-				compressPicture(localFile,pathSmall,150,60);
+				bannerTemplateService.compressPicture(localFile,pathSmall,150,60);
 				//判断是否需要缩放
 				if((width*_height)==(_width*height)){
 					//压缩文件上传路径
 					 String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(resetName);
 					 //生成压缩图
-					 compressPicture(localFile,compressPath, _width,_height);
+					 bannerTemplateService.compressPicture(localFile,compressPath, _width,_height);
 					//设置文件上传路径
-					 bannerTemplate.setPath(replacePath(compressPath));
+					 bannerTemplate.setPath(bannerTemplateService.replacePath(compressPath));
 				 }else{
 					 //设置文件上传路径
-					 bannerTemplate.setPath(replacePath(path));
+					 bannerTemplate.setPath(bannerTemplateService.replacePath(path));
 				 }
 				responseStr="上传成功";
 				//设置缩略图文件路径
-				bannerTemplate.setPathSmall(replacePath(pathSmall));
+				bannerTemplate.setPathSmall(bannerTemplateService.replacePath(pathSmall));
 				bannerTemplate.setName(uploadName);
-				bannerTemplate.setType(judgeType(fileType));
+				bannerTemplate.setType(bannerTemplateService.judgeType(fileType));
 				bannerTemplate.setStatus(0);
 				bannerTemplate.setAdvertiserId(advertiserId);
 				bannerTemplate.setAgentId(agentId);
@@ -370,7 +370,7 @@ public class BannerTemplateController extends SimpleController {
 			if(file != null){ 
 				//取得当前上传文件的文件名称(不加路径)  
 				uploadName = file.getOriginalFilename();  
-				resetName = resetName(uploadName);
+				resetName = bannerTemplateService.resetName(uploadName);
 				
 				//获取文件类型
 				 fileType = uploadName.substring(uploadName.lastIndexOf(".") + 1);
@@ -384,7 +384,7 @@ public class BannerTemplateController extends SimpleController {
 				if(uploadName.trim() !=""){  
 					
 					//处理上传文件路径
-					path = getLocalFilePath(resetName);
+					path = bannerTemplateService.getLocalFilePath(resetName);
 					
 					File localFile = new File(path); 
 					//上传目录不存在,则新建目录
@@ -404,27 +404,27 @@ public class BannerTemplateController extends SimpleController {
 					//生成缩略图路径名
 					pathSmall = absolutePath.concat(File.separator).concat("small_").concat(resetName);
 					//生成缩略图
-					compressPicture(localFile,pathSmall,150,60);
+					bannerTemplateService.compressPicture(localFile,pathSmall,150,60);
 					
 					if((width*_height)==(_width*height)){
 						//压缩文件上传路径
 						 String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(resetName);
 						 //生成压缩图
-						 compressPicture(localFile,compressPath, _width,_height);
+						 bannerTemplateService.compressPicture(localFile,compressPath, _width,_height);
 						//设置文件上传路径
-						 bannerTemplate.setPath(replacePath(compressPath));
+						 bannerTemplate.setPath(bannerTemplateService.replacePath(compressPath));
 					 }else{
 						 if(width>_width||height>_height){
 							 return "图片尺寸错啦!!";
 						 }
 						 //设置文件上传路径
-						 bannerTemplate.setPath(replacePath(path));
+						 bannerTemplate.setPath(bannerTemplateService.replacePath(path));
 					 }
 				
 					//设置缩略图文件路径
-					bannerTemplate.setPathSmall(replacePath(pathSmall));
+					bannerTemplate.setPathSmall(bannerTemplateService.replacePath(pathSmall));
 					bannerTemplate.setName(uploadName);
-					bannerTemplate.setType(judgeType(fileType));
+					bannerTemplate.setType(bannerTemplateService.judgeType(fileType));
 				//更新素材
 				bannerTemplateService.edit(bannerTemplate);
 				responseStr="上传成功";
@@ -439,143 +439,6 @@ public class BannerTemplateController extends SimpleController {
 		}	
 		  return responseStr; 
 	}
-	
-	
-//处理图片名称
-	public String resetName(String uploadName){
-		String fileType =  fileType = uploadName.substring(uploadName.lastIndexOf("."));
-		uploadName = new StringBuilder()
-					.append(System.currentTimeMillis())
-					.append(fileType)
-					.toString();
-		return uploadName;
-	}
-	
-	
-//处理上传文件的绝对路径
-public String getLocalFilePath(String fileName){
-					Account loginUser = getLoginUser();
-					// 计算相对路径名,即不包括默认路径名
-					String path = new StringBuilder()
-							.append(File.separator)
-							.append(loginUser == null ? "anon" : loginUser.getId())							
-							.append(File.separator)
-							.append(DateUtil.getDate(DateFmts.YYYYMMDD))
-							.append(File.separator)
-							.append(System.currentTimeMillis())
-							.append(File.separator)
-							.append(fileName)
-							.toString();
-					
-					String localFilePath = config.getResourceUploadDir().concat(File.separator).concat("banner").concat(path);
-					
-//					// 计算在容器中实际路径名,添加文件所运行所在的实际目录
-					String accessUrl = localFilePath;
-					ServletContext servletContext = getServletContext();
-						if (servletContext == null) {
-							LOG.debug("单体测试情况,servletContext不存在。");
-						} else {
-						
-							accessUrl = servletContext.getRealPath("/");
-							File upFile = new File(accessUrl);
-							accessUrl = upFile.getParent();
-							LOG.info("\naaa----"+localFilePath+" -----"+accessUrl);
-					//		获取绝对路径
-							localFilePath = accessUrl.concat(File.separator).concat(localFilePath);
-					}
-					return localFilePath;
-				}
-
-//处理路径为web路径
-	public String replacePath(String path){
-		
-		path = path.substring(path.indexOf("upload")-1);
-
-	return path;
-	}			
-					
-//判断文件类型				
-public int judgeType(String fileType){
-					int type = 2;
-					if("jpg".equalsIgnoreCase(fileType)||"jpeg".equalsIgnoreCase(fileType)||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType) ){
-						type = 2;
-					}else if("flv".equalsIgnoreCase(fileType)||"mp4".equalsIgnoreCase(fileType)){
-						type = 3;
-					}else{
-						type = 1;
-					}
-					return type;
-				}
-
-	//生成缩略图
-	public void compressPicture(File localFile,String pathNew,int width,int height) throws IOException{
-		//新建缩略图文件
-		File newFile = new File(pathNew);
-		if (!newFile.getParentFile().exists()){
-			newFile.getParentFile().mkdirs();
-		}	
-		//构造image图片
-		Image src = javax.imageio.ImageIO.read(localFile);
-		//表示该图像具有打包成整数的像素的8 位RGB颜色分量
-		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
-		//h绘制缩小后的图片
-		tag.getGraphics().drawImage(src, 0, 0, width, height, null);
-		//生成缩略图
-		ImageIO.write(tag, "png", newFile);
-	}
-
-//匹配广告为宽高,用取平方差最小
-public BannerTemplate suitSize(int width,int height,BannerTemplate bannerTemplate) throws IOException{
-
-	//构造image图片
-
-		long max_width = 0;
-		long max_height = 0;
-		Size size = new Size();		
-		int i;
-		int n =0; 
-		//查找所有广告位
-		List<Size> sizeList = sizeService.findAllSize();
-		//取平方差
-		double suit[] = new double[sizeList.size()];
-		long _width[] = new long[sizeList.size()];
-		long _height[] = new long[sizeList.size()];
-		for ( i=0 ;i<sizeList.size();i++){
-			size = sizeList.get(i); 
-			_width[i] = size.getWidth(); 
-			_height[i] = size.getHeight();
-			if(width<_width[i]&&height<_height[i]){
-				suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
-			}else{
-				if((width*_height[i])==(_width[i]*height)){
-					suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
-				}else{
-				suit[i]=0;
-				}
-			}
-			
-			}
-		
-			//取最小值
-			double min=10000;
-			for (i=0;i<suit.length;i++){
-				if(suit[i]!=0){			
-					if (suit[i]<min){						
-						min = suit[i];
-						n=i;
-					}	
-				}	
-			}
-		if(min==10000){
-				bannerTemplate=null;
-			}else{
-	
-		bannerTemplate.setWidth(_width[n]);
-		bannerTemplate.setHeight(_height[n]);
-		}	
-		return bannerTemplate;
-	}
-
 
 
 }

+ 9 - 107
src/main/java/com/cloudcross/ssp/web/advertiser/main/ad/target/AdController.java

@@ -455,7 +455,7 @@ public class AdController extends SimpleController {
 				if(file != null){ 
 					
 					uploadName = file.getOriginalFilename();  
-					resetName = resetName(uploadName);
+					resetName = bannerTemplateService.resetName(uploadName);
 					//取上传文件类型
 					fileType = uploadName.substring(uploadName.lastIndexOf(".") + 1);
 					
@@ -471,7 +471,7 @@ public class AdController extends SimpleController {
 	
 						
 						//获取上传路径
-						path = getLocalFilePath(resetName);
+						path = bannerTemplateService.getLocalFilePath(resetName);
 						
 				try{
 						//新建上传路径下文件
@@ -542,7 +542,7 @@ public class AdController extends SimpleController {
 				//生成缩略图路径名
 				String pathSmall = absolutePath.concat(File.separator).concat("small_").concat(resetName);
 				//生成缩略图
-				compressPicture(uploadFile,pathSmall, 150,60);
+				bannerTemplateService.compressPicture(uploadFile,pathSmall, 150,60);
 				
 				//取上传图片的宽高
 				 BufferedImage image = ImageIO.read(new FileInputStream(filePath));
@@ -555,19 +555,19 @@ public class AdController extends SimpleController {
 					//设置压缩文件上传路径
 					 String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(uploadName);
 					 //生成压缩图
-					 compressPicture(uploadFile,compressPath, _width,_height);
+					 bannerTemplateService.compressPicture(uploadFile,compressPath, _width,_height);
 					//设置文件上传路径
-					 bannerTemplate.setPath(replacePath(compressPath));
+					 bannerTemplate.setPath(bannerTemplateService.replacePath(compressPath));
 				 }else{
 				 
 					 //设置文件上传路径
-					 bannerTemplate.setPath(replacePath(filePath));
+					 bannerTemplate.setPath(bannerTemplateService.replacePath(filePath));
 				 }
 				//设置缩略图上传路径
 				bannerTemplate.setName(banner.getBannerTemplate().getName());
-				bannerTemplate.setPathSmall(replacePath(pathSmall));
+				bannerTemplate.setPathSmall(bannerTemplateService.replacePath(pathSmall));
 				bannerTemplate.setStatus(bannerTemplate.getStatus());
-				bannerTemplate.setType(judgeType(fileType));
+				bannerTemplate.setType(bannerTemplateService.judgeType(fileType));
 				bannerTemplateService.add(bannerTemplate);
 				banner.setBannerTemplateId(bannerTemplate.getId());
 				banner.setBannerTemplate(bannerTemplate);
@@ -895,89 +895,7 @@ public class AdController extends SimpleController {
 			e.printStackTrace();
 		}
 	}
-	
-	
-	//处理图片名称
-		public String resetName(String uploadName){
-			String fileType =  fileType = uploadName.substring(uploadName.lastIndexOf("."));
-			uploadName = new StringBuilder()
-						.append(System.currentTimeMillis())
-						.append(fileType)
-						.toString();
-			return uploadName;
-		}
-	
-	//处理上传文件的绝对路径
-	public String getLocalFilePath(String fileName){
-					Account loginUser = getLoginUser();
-					// 计算相对路径名,即不包括默认路径名
-					String path = new StringBuilder()
-								.append(File.separator)
-								.append(loginUser == null ? "anon" : loginUser.getId())							
-								.append(File.separator)
-								.append(DateUtil.getDate(DateFmts.YYYYMMDD))
-								.append(File.separator)
-								.append(System.currentTimeMillis())
-								.append(File.separator)
-								.append(fileName)
-								.toString();
-						
-					String localFilePath =config.getResourceUploadDir().concat(File.separator).concat("banner").concat(path);
-						
-					String accessUrl = localFilePath;
-					ServletContext servletContext = getServletContext();
-					if (servletContext == null) {
-						LOG.debug("单体测试情况,servletContext不存在。");
-						} else {
-					
-						accessUrl = servletContext.getRealPath("/");
-						File upFile = new File(accessUrl);
-						accessUrl = upFile.getParent();
-						LOG.info("\naaa----"+localFilePath+" -----"+accessUrl);
-						//获取绝对路径
-						localFilePath = accessUrl.concat(File.separator).concat(localFilePath);
-						}
-		return localFilePath;
-	}
-						
-	//处理路径为web路径
-	public String replacePath(String path){
-		
-		path = path.substring(path.indexOf("upload")-1);
 
-	return path;
-	}
-	
-	//处理文件类型
-	private int judgeType(String fileType){
-			int type = 2;
-			if("jpg".equalsIgnoreCase(fileType)||"jpeg".equalsIgnoreCase(fileType)||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType) ){
-				type = 2;
-			}else if("flv".equalsIgnoreCase(fileType)||"mp4".equalsIgnoreCase(fileType)){
-				type = 3;
-			}else{
-				type = 1;
-			}
-			return type;
-	}
-	
-	//生成缩略图
-	public void compressPicture(File localFile,String pathNew,int width,int height) throws IOException{
-		//新建缩略图文件
-		File newFile = new File(pathNew);
-		if (!newFile.getParentFile().exists()){
-			newFile.getParentFile().mkdirs();
-		}	
-		//构造image图片
-		Image src = javax.imageio.ImageIO.read(localFile);
-		//表示该图像具有打包成整数的像素的8 位RGB颜色分量
-		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
-		//h绘制缩小后的图片
-		tag.getGraphics().drawImage(src, 0, 0, width, height, null);
-		//生成缩略图
-		ImageIO.write(tag, "png", newFile);
-	}
-	
 
 	
 	//编辑创意判断上传图片是否符合上传条件
@@ -1002,23 +920,7 @@ public class AdController extends SimpleController {
 			return true;
 	}
 	
-//	//如果是大于广告位且等比例的需要等比例缩小
-//	public void compressPic(File localFile,String compressPath,) throws IOException{
-//		File compressFile = new File(compressPath);
-//		if (!compressFile.getParentFile().exists()){
-//			compressFile.getParentFile().mkdirs();
-//		}	
-//		//构造image图片
-//		Image src = javax.imageio.ImageIO.read(localFile);
-//		
-//		//表示该图像具有打包成整数的像素的8 位RGB颜色分量
-//		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
-//		//h绘制缩小后的图片
-//		tag.getGraphics().drawImage(src, 0, 0, width, height, null);
-//		//生成压缩图
-//		ImageIO.write(tag, "png", compressFile);
-//		
-//	}
+
 		
 }
 

+ 12 - 150
src/main/java/com/cloudcross/ssp/web/advertiser/main/ad/target/CreateBannerController.java

@@ -98,6 +98,8 @@ public class CreateBannerController extends SimpleController{
 	@Autowired
 	IAdGroupService adGroupService;
 	@Autowired
+	IBannerTemplateService bannerTemplateService;
+	@Autowired
 	IBannerService bannerService;
 	@Autowired
 	ISizeService sizeService;
@@ -297,7 +299,7 @@ public class CreateBannerController extends SimpleController{
 			if(file != null){ 
 				//取得当前上传文件的文件名称(不加路径)  
 				uploadName = file.getOriginalFilename();  
-				resetName = resetName(uploadName);
+				resetName = bannerTemplateService.resetName(uploadName);
 				//获取文件类型
 				 fileType = uploadName.substring(uploadName.lastIndexOf(".") + 1);
 				 
@@ -310,7 +312,7 @@ public class CreateBannerController extends SimpleController{
 				if(uploadName.trim() !=""){  
 					
 					//处理上传文件路径
-					path = getLocalFilePath(resetName);
+					path = bannerTemplateService.getLocalFilePath(resetName);
 					
 					File localFile = new File(path); 
 					//上传目录不存在,则新建目录
@@ -324,7 +326,7 @@ public class CreateBannerController extends SimpleController{
 				 width = image.getWidth();
 				 height = image.getHeight();
 				//匹配广告位
-				 bannerTemplate=suitSize(width,height, bannerTemplate);
+				 bannerTemplate=bannerTemplateService.suitSize(width,height, bannerTemplate);
 				//图片匹配
 					if(bannerTemplate==null){
 						return "图片尺寸错啦!!";
@@ -337,23 +339,23 @@ public class CreateBannerController extends SimpleController{
 				//生成缩略图路径名
 				pathSmall = absolutePath.concat(File.separator).concat("small_").concat(resetName);
 				//生成缩略图
-				compressPicture(localFile,pathSmall,150,60);
+				bannerTemplateService.compressPicture(localFile,pathSmall,150,60);
 				//判断是否需要缩放
 				if((width*_height)==(_width*height)){
 					//压缩文件上传路径
 					 String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(resetName);
 					 //生成压缩图
-					 compressPicture(localFile,compressPath, _width,_height);
+					 bannerTemplateService.compressPicture(localFile,compressPath, _width,_height);
 					//设置文件上传路径
-					 bannerTemplate.setPath(replacePath(compressPath));
+					 bannerTemplate.setPath(bannerTemplateService.replacePath(compressPath));
 				 }else{
 					 //设置文件上传路径
-					 bannerTemplate.setPath(replacePath(path));
+					 bannerTemplate.setPath(bannerTemplateService.replacePath(path));
 				 }
 
 				responseStr="文件上传成功";
 				//保存缩略图路径
-				bannerTemplate.setPathSmall(replacePath(pathSmall));	
+				bannerTemplate.setPathSmall(bannerTemplateService.replacePath(pathSmall));	
 				adGroupId =Long.parseLong(ids[0]);
 				AdGroup adGroup = adGroupService.findById(adGroupId);
 				Long campaignId = adGroup.getCampaignId();
@@ -368,7 +370,7 @@ public class CreateBannerController extends SimpleController{
 				model.addAttribute("agentId", agentId);
 				
 				bannerTemplate.setName(uploadName);
-				bannerTemplate.setType(judgeType(fileType));
+				bannerTemplate.setType(bannerTemplateService.judgeType(fileType));
 				bannerTemplate.setStatus(0);
 				bannerTemplate.setAdvertiserId(advertiserId);
 				bannerTemplate.setAgentId(agentId);
@@ -420,150 +422,10 @@ public class CreateBannerController extends SimpleController{
 			}
 			
 		}
-				return responseStr; 
+				return responseStr;  
 	}
-	
-	
 
-	//处理图片名称
-		public String resetName(String uploadName){
-			String fileType =  fileType = uploadName.substring(uploadName.lastIndexOf("."));
-			uploadName = new StringBuilder()
-						.append(System.currentTimeMillis())
-						.append(fileType)
-						.toString();
-			return uploadName;
-		}
-	
-//获取上传文件的绝对路径
-public String getLocalFilePath(String fileName){
-					Account loginUser = getLoginUser();
-					// 计算相对路径名,即不包括默认路径名
-					String path = new StringBuilder()
-							.append(File.separator)
-							.append(loginUser == null ? "anon" : loginUser.getId())							
-							.append(File.separator)
-							.append(DateUtil.getDate(DateFmts.YYYYMMDD))
-							.append(File.separator)
-							.append(System.currentTimeMillis())
-							.append(File.separator)
-							.append(fileName)
-							.toString();
-					
-					String localFilePath = config.getResourceUploadDir()
-										   .concat(File.separator)
-										   .concat("banner")
-										   .concat(path);
-					
-						String accessUrl = localFilePath;
-				ServletContext servletContext = getServletContext();
-					if (servletContext == null) {
-						LOG.debug("单体测试情况,servletContext不存在。");
-					} else {
-					
-						accessUrl = servletContext.getRealPath("/");
-						File upFile = new File(accessUrl);
-						accessUrl = upFile.getParent();
-						LOG.info("\naaa----"+localFilePath+" -----"+accessUrl);
-				//		获取绝对路径
-						localFilePath = accessUrl.concat(File.separator).concat(localFilePath);
-					}  
-					return localFilePath;
-				}
 	
-//处理路径为web路径
-public String replacePath(String path){
-	
-
-	
-	path = path.substring(path.indexOf("upload")-1);
-
-return path;
-}
-
-//判断文件类型				
-public int judgeType(String fileType){
-					int type = 2;
-					if("jpg".equalsIgnoreCase(fileType)||"jpeg".equalsIgnoreCase(fileType)||"gif".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType) ){
-						type = 2;
-					}else if("flv".equalsIgnoreCase(fileType)||"mp4".equalsIgnoreCase(fileType)){
-						type = 3;
-					}else{
-						type = 1;
-					}
-					return type;
-				}
-
-
-
-//生成缩略图
-	public void compressPicture(File localFile,String pathNew,int width,int height) throws IOException{
-		//新建缩略图文件
-		File newFile = new File(pathNew);
-		if (!newFile.getParentFile().exists()){
-			newFile.getParentFile().mkdirs();
-		}	
-		//构造image图片
-		Image src = javax.imageio.ImageIO.read(localFile);
-		//表示该图像具有打包成整数的像素的8 位RGB颜色分量
-		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
-		//h绘制缩小后的图片
-		tag.getGraphics().drawImage(src, 0, 0, width, height, null);
-		//生成缩略图
-		ImageIO.write(tag, "png", newFile);
-	}
-
-//匹配广告为宽高,用取平方差最小
-public BannerTemplate suitSize(int width,int height,BannerTemplate bannerTemplate) throws IOException{
-
-	//构造image图片
-
-		long max_width = 0;
-		long max_height = 0;
-		Size size = new Size();		
-		int i;
-		int n =0; 
-		//查找所有广告位
-		List<Size> sizeList = sizeService.findAllSize();
-		//取平方差
-		double suit[] = new double[sizeList.size()];
-		long _width[] = new long[sizeList.size()];
-		long _height[] = new long[sizeList.size()];
-		for ( i=0 ;i<sizeList.size();i++){
-			size = sizeList.get(i); 
-			_width[i] = size.getWidth(); 
-			_height[i] = size.getHeight();
-			if(width<_width[i]&&height<_height[i]){
-				suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
-			}else{
-				if((width*_height[i])==(_width[i]*height)){
-					suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
-				}else{
-				suit[i]=0;
-				}
-			}
-			
-			}
-		
-			//取最小值
-			double min=10000;
-			for (i=0;i<suit.length;i++){
-				if(suit[i]!=0){			
-					if (suit[i]<min){						
-						min = suit[i];
-						n=i;
-					}	
-				}	
-			}
-		if(min==10000){
-				bannerTemplate=null;
-			}else{
-	
-		bannerTemplate.setWidth(_width[n]);
-		bannerTemplate.setHeight(_height[n]);
-		}	
-		return bannerTemplate;
-	}
 }