Browse Source

上传文件类型与大小判断

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

+ 19 - 0
src/main/java/com/cloudcross/ssp/web/advertiser/main/ad/BannerTemplateController.java

@@ -86,6 +86,7 @@ public class BannerTemplateController extends SimpleController {
 	static {
 	static {
 			noStuffixSet.add("jpg");
 			noStuffixSet.add("jpg");
 			noStuffixSet.add("gif");
 			noStuffixSet.add("gif");
+			noStuffixSet.add("png");
 		}
 		}
 
 
 	@RequestMapping
 	@RequestMapping
@@ -280,6 +281,9 @@ public class BannerTemplateController extends SimpleController {
 				//设置缩略图文件路径
 				//设置缩略图文件路径
 				bannerTemplate.setPathSmall(replacePath(pathSmall));
 				bannerTemplate.setPathSmall(replacePath(pathSmall));
 				//创建素材
 				//创建素材
+				if(suitSize(path, bannerTemplate)==null){
+					return "图片有点大了啊!";
+				}
 				bannerTemplate= suitSize(path, bannerTemplate);
 				bannerTemplate= suitSize(path, bannerTemplate);
 				bannerTemplate.setName(uploadName);
 				bannerTemplate.setName(uploadName);
 				bannerTemplate.setType(judgeType(fileType));
 				bannerTemplate.setType(judgeType(fileType));
@@ -364,6 +368,9 @@ public class BannerTemplateController extends SimpleController {
 				//设置缩略图文件路径
 				//设置缩略图文件路径
 				bannerTemplate.setPathSmall(replacePath(pathSmall));
 				bannerTemplate.setPathSmall(replacePath(pathSmall));
 				//设置素材宽高
 				//设置素材宽高
+				if(suitSize(path, bannerTemplate)==null){
+					return "图片有点大了啊!";
+				}
 				bannerTemplate= suitSize(path, bannerTemplate);
 				bannerTemplate= suitSize(path, bannerTemplate);
 				bannerTemplate.setName(uploadName);
 				bannerTemplate.setName(uploadName);
 				bannerTemplate.setType(judgeType(fileType));
 				bannerTemplate.setType(judgeType(fileType));
@@ -522,6 +529,8 @@ public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws
 		Image src = javax.imageio.ImageIO.read(file);
 		Image src = javax.imageio.ImageIO.read(file);
 		int width = src.getWidth(null);
 		int width = src.getWidth(null);
 		int height =src.getHeight(null);
 		int height =src.getHeight(null);
+		long max_width = 0;
+		long max_height = 0;
 		Size size = new Size();		
 		Size size = new Size();		
 		int i;
 		int i;
 		int n =0;
 		int n =0;
@@ -535,8 +544,17 @@ public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws
 			size = sizeList.get(i); 
 			size = sizeList.get(i); 
 			_width[i] = size.getWidth(); 
 			_width[i] = size.getWidth(); 
 			_height[i] = size.getHeight();
 			_height[i] = size.getHeight();
+			if(_width[i]>max_width){
+				max_width = _width[i];
+			}
+			if(_height[i]>max_height){
+				max_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]));
 			suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
 		}
 		}
+		if(width>max_width||height>max_height){
+			bannerTemplate = null;
+	}else{
 		double min =suit[0];
 		double min =suit[0];
 		for (i=0;i<suit.length;i++){
 		for (i=0;i<suit.length;i++){
 			if (suit[i]<min){
 			if (suit[i]<min){
@@ -547,6 +565,7 @@ public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws
 		
 		
 		bannerTemplate.setWidth(_width[n]);
 		bannerTemplate.setWidth(_width[n]);
 		bannerTemplate.setHeight(_height[n]);	
 		bannerTemplate.setHeight(_height[n]);	
+	}
 		return bannerTemplate;
 		return bannerTemplate;
 	}
 	}
 }
 }

+ 27 - 15
src/main/java/com/cloudcross/ssp/web/advertiser/main/ad/target/AdController.java

@@ -103,6 +103,7 @@ public class AdController extends SimpleController {
 			static {
 			static {
 					noStuffixSet.add("jpg");
 					noStuffixSet.add("jpg");
 					noStuffixSet.add("gif");
 					noStuffixSet.add("gif");
+					noStuffixSet.add("png");
 				}
 				}
 		
 		
 
 
@@ -456,7 +457,8 @@ public class AdController extends SimpleController {
 					
 					
 					//判断文件类型
 					//判断文件类型
 					  if(!noStuffixSet.contains(fileType)){
 					  if(!noStuffixSet.contains(fileType)){
-							return "不支持此文件类型";
+							result = "~不支持此文件类型";
+							return result;
 						}
 						}
 					//判断文件名书否存在
 					//判断文件名书否存在
 					if(uploadName.trim() !=""){  
 					if(uploadName.trim() !=""){  
@@ -466,12 +468,14 @@ public class AdController extends SimpleController {
 						
 						
 						//获取上传路径
 						//获取上传路径
 						path = getLocalFilePath(fileName);
 						path = getLocalFilePath(fileName);
+						
 						try{
 						try{
 						//新建上传路径下文件
 						//新建上传路径下文件
 						File localFile = new File(path);  
 						File localFile = new File(path);  
 						if (!localFile.getParentFile().exists()){
 						if (!localFile.getParentFile().exists()){
 							localFile.getParentFile().mkdirs();
 							localFile.getParentFile().mkdirs();
-						}	
+						}
+						//if(checkPic(path)){
 						//复制文件到上传文件
 						//复制文件到上传文件
 						file.transferTo(localFile);
 						file.transferTo(localFile);
 						//取上传文件父级目录
 						//取上传文件父级目录
@@ -482,10 +486,15 @@ public class AdController extends SimpleController {
 						smallPicture(localFile,pathSmall, uploadName);
 						smallPicture(localFile,pathSmall, uploadName);
 						
 						
 						result="文件上传成功";
 						result="文件上传成功";
+//							}else{
+//								result="图片太大了哦!";
+//							}
 						}catch(IOException e){
 						}catch(IOException e){
-							result="文件上传失败";
+							e.printStackTrace();
+							result="~文件上传失败 ";
 							return result;
 							return result;
 						}
 						}
+					  
 					}  			 
 					}  			 
 				}  
 				}  
 			} 
 			} 
@@ -529,6 +538,7 @@ public class AdController extends SimpleController {
 				smallPicture(uploadFile,pathSmall, uploadName);
 				smallPicture(uploadFile,pathSmall, uploadName);
 				
 				
 				//匹配广告位宽高
 				//匹配广告位宽高
+				
 				bannerTemplate = suitSize(filePath, bannerTemplate);
 				bannerTemplate = suitSize(filePath, bannerTemplate);
 				//设置文件上传路径
 				//设置文件上传路径
 				bannerTemplate.setPath(replacePath(filePath));
 				bannerTemplate.setPath(replacePath(filePath));
@@ -1006,7 +1016,7 @@ public class AdController extends SimpleController {
 			Image src = javax.imageio.ImageIO.read(file);
 			Image src = javax.imageio.ImageIO.read(file);
 			int width = src.getWidth(null);
 			int width = src.getWidth(null);
 			int height =src.getHeight(null);
 			int height =src.getHeight(null);
-			Size size = new Size();		
+			Size size = new Size();	
 			int i;
 			int i;
 			int n =0;
 			int n =0;
 			//查找所有广告位
 			//查找所有广告位
@@ -1021,20 +1031,22 @@ public class AdController extends SimpleController {
 				_height[i] = size.getHeight();
 				_height[i] = size.getHeight();
 				suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(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]));
 			}
 			}
-			double min =suit[0];
-			for (i=0;i<suit.length;i++){
-				if (suit[i]<min){
-					min = suit[i];
-					n=i;
-				}	
-			}
-			
-			bannerTemplate.setWidth(_width[n]);
-			bannerTemplate.setHeight(_height[n]);
 			
 			
-		return bannerTemplate;
+				double min =suit[0];
+				for (i=0;i<suit.length;i++){
+					if (suit[i]<min){
+						min = suit[i];
+						n=i;
+					}	
+				}
+		
+				bannerTemplate.setWidth(_width[n]);
+				bannerTemplate.setHeight(_height[n]);				
+				return bannerTemplate;
 	}
 	}
 	
 	
+	
+	
 }
 }
 
 
 
 

+ 32 - 17
src/main/java/com/cloudcross/ssp/web/advertiser/main/ad/target/CreateBannerController.java

@@ -117,6 +117,7 @@ public class CreateBannerController extends SimpleController{
 		static {
 		static {
 				noStuffixSet.add("jpg");
 				noStuffixSet.add("jpg");
 				noStuffixSet.add("gif");
 				noStuffixSet.add("gif");
+				noStuffixSet.add("png");
 			}
 			}
 	
 	
 	@RequestMapping("/list")
 	@RequestMapping("/list")
@@ -305,7 +306,7 @@ public class CreateBannerController extends SimpleController{
 					
 					
 					//处理上传文件路径
 					//处理上传文件路径
 					path = getLocalFilePath(uploadName);
 					path = getLocalFilePath(uploadName);
-					System.out.println("qqqqqqq+==="+path);
+					//System.out.println("qqqqqqq+==="+path);
 					
 					
 					File localFile = new File(path); 
 					File localFile = new File(path); 
 					//上传目录不存在,则新建目录
 					//上传目录不存在,则新建目录
@@ -321,7 +322,7 @@ public class CreateBannerController extends SimpleController{
 				//生成缩略图
 				//生成缩略图
 				smallPicture(localFile,pathSmall,uploadName);
 				smallPicture(localFile,pathSmall,uploadName);
 
 
-				responseStr="上传成功";
+				responseStr="文件上传成功";
 				//保存上传文件路径 
 				//保存上传文件路径 
 				bannerTemplate.setPath(replacePath(path));
 				bannerTemplate.setPath(replacePath(path));
 				//保存缩略图路径
 				//保存缩略图路径
@@ -340,6 +341,9 @@ public class CreateBannerController extends SimpleController{
 				model.addAttribute("agentId", agentId);
 				model.addAttribute("agentId", agentId);
 				
 				
 				//创建素材
 				//创建素材
+				if(suitSize(path, bannerTemplate)==null){
+					return "图片有点大了!";
+				}
 				bannerTemplate= suitSize(path, bannerTemplate);
 				bannerTemplate= suitSize(path, bannerTemplate);
 				bannerTemplate.setName(uploadName);
 				bannerTemplate.setName(uploadName);
 				bannerTemplate.setType(judgeType(fileType));
 				bannerTemplate.setType(judgeType(fileType));
@@ -542,7 +546,9 @@ public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws
 		Image src = javax.imageio.ImageIO.read(file);
 		Image src = javax.imageio.ImageIO.read(file);
 		int width = src.getWidth(null);
 		int width = src.getWidth(null);
 		int height =src.getHeight(null);
 		int height =src.getHeight(null);
-		Size size = new Size();		
+		Size size = new Size();	
+		long max_width = 0;
+		long max_height = 0;
 		int i;
 		int i;
 		int n =0;
 		int n =0;
 		//查找所有广告位
 		//查找所有广告位
@@ -555,24 +561,33 @@ public BannerTemplate suitSize(String path,BannerTemplate bannerTemplate) throws
 			size = sizeList.get(i); 
 			size = sizeList.get(i); 
 			_width[i] = size.getWidth(); 
 			_width[i] = size.getWidth(); 
 			_height[i] = size.getHeight();
 			_height[i] = size.getHeight();
+			if(_width[i]>max_width){
+				max_width = _width[i];
+			}
+			if(_height[i]>max_height){
+				max_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]));
 			suit[i] =Math.sqrt(Math.abs(width-_width[i])*Math.abs(width-_width[i])+Math.abs(height-_height[i])*Math.abs(height-_height[i]));
 		}
 		}
-		double min =suit[0];
-		for (i=0;i<suit.length;i++){
-			if (suit[i]<min){
-				min = suit[i];
-				n=i;
-			}	
-		}
+		if(width>max_width||height>max_height){
+				bannerTemplate = null;
+		}else{
+				double min =suit[0];
+				for (i=0;i<suit.length;i++){
+					if (suit[i]<min){
+						min = suit[i];
+						n=i;
+					}	
+				}
 		
 		
-		bannerTemplate.setWidth(_width[n]);
-		bannerTemplate.setHeight(_height[n]);	
-		return bannerTemplate;
-	}
+				bannerTemplate.setWidth(_width[n]);
+				bannerTemplate.setHeight(_height[n]);
+				}
+				return bannerTemplate;
+				
+		}
 }
 }
-		 
-		 
-		 
+		 	 
 	
 	
 	
 	
 
 

+ 4 - 1
src/main/webapp/WEB-INF/pages/advertiser/main/ad/target/ad/edit.ftl

@@ -33,13 +33,16 @@
 			$('#input-file').fileupload({
 			$('#input-file').fileupload({
 				autoUpload: true,
 				autoUpload: true,
 				url: '${ctx}/advertiser/main/ad/target/ad/upload?bannerId=${banner.id}',		
 				url: '${ctx}/advertiser/main/ad/target/ad/upload?bannerId=${banner.id}',		
+				formData:{bannerId:$('form input[name="id"]').val()},
 				done:function(e,data){
 				done:function(e,data){
 					var path = data.result;
 					var path = data.result;
-					if(path.indexOf("不支持") < 0){
+					if(path.indexOf("~") < 0){
 					var file_name = path.split("\\")[path.split("\\").length-1];
 					var file_name = path.split("\\")[path.split("\\").length-1];
 					$("#bannerTemplateName").val(file_name);
 					$("#bannerTemplateName").val(file_name);
 		              $("#bannerTemplateName1").val(file_name); 
 		              $("#bannerTemplateName1").val(file_name); 
 					 $("#upload-path").val(data.result);
 					 $("#upload-path").val(data.result);
+			     }else{
+			    	 alert(data.result);
 			     }
 			     }
 				}
 				}
 			})
 			})

+ 1 - 0
src/main/webapp/WEB-INF/pages/advertiser/main/ad/target/create-banner/list.ftl

@@ -173,6 +173,7 @@ $(function(){
 		autoUpload: true,
 		autoUpload: true,
 		url: '${ctx}/advertiser/main/ad/target/create-banner/upload?adGroupId=${adGroupId}',		
 		url: '${ctx}/advertiser/main/ad/target/create-banner/upload?adGroupId=${adGroupId}',		
 		done:function(e,data){
 		done:function(e,data){
+			alert(data.result);
 			window.location.reload();
 			window.location.reload();
 		}				
 		}				
 	});
 	});