|
@@ -252,7 +252,7 @@ public class BannerTemplateController extends SimpleController {
|
|
if(file != null){
|
|
if(file != null){
|
|
//取得当前上传文件的文件名称(不加路径)
|
|
//取得当前上传文件的文件名称(不加路径)
|
|
uploadName = file.getOriginalFilename();
|
|
uploadName = file.getOriginalFilename();
|
|
- resetName = resetName(uploadName);
|
|
|
|
|
|
+ resetName = bannerTemplateService.resetName(uploadName);
|
|
//获取文件类型
|
|
//获取文件类型
|
|
fileType = uploadName.substring(uploadName.lastIndexOf(".") + 1);
|
|
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(path);
|
|
System.out.println("qqqqqqq+==="+path);
|
|
System.out.println("qqqqqqq+==="+path);
|
|
|
|
|
|
@@ -287,7 +287,7 @@ public class BannerTemplateController extends SimpleController {
|
|
height = image.getHeight();
|
|
height = image.getHeight();
|
|
|
|
|
|
//匹配广告位
|
|
//匹配广告位
|
|
- bannerTemplate=suitSize(width,height, bannerTemplate);
|
|
|
|
|
|
+ bannerTemplate=bannerTemplateService.suitSize(width,height, bannerTemplate);
|
|
//图片匹配
|
|
//图片匹配
|
|
if(bannerTemplate==null){
|
|
if(bannerTemplate==null){
|
|
return "图片尺寸错啦!!";
|
|
return "图片尺寸错啦!!";
|
|
@@ -300,24 +300,24 @@ public class BannerTemplateController extends SimpleController {
|
|
//生成缩略图路径名
|
|
//生成缩略图路径名
|
|
pathSmall = absolutePath.concat(File.separator).concat("small_").concat(resetName);
|
|
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)){
|
|
if((width*_height)==(_width*height)){
|
|
//压缩文件上传路径
|
|
//压缩文件上传路径
|
|
String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(resetName);
|
|
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{
|
|
}else{
|
|
//设置文件上传路径
|
|
//设置文件上传路径
|
|
- bannerTemplate.setPath(replacePath(path));
|
|
|
|
|
|
+ bannerTemplate.setPath(bannerTemplateService.replacePath(path));
|
|
}
|
|
}
|
|
responseStr="上传成功";
|
|
responseStr="上传成功";
|
|
//设置缩略图文件路径
|
|
//设置缩略图文件路径
|
|
- bannerTemplate.setPathSmall(replacePath(pathSmall));
|
|
|
|
|
|
+ bannerTemplate.setPathSmall(bannerTemplateService.replacePath(pathSmall));
|
|
bannerTemplate.setName(uploadName);
|
|
bannerTemplate.setName(uploadName);
|
|
- bannerTemplate.setType(judgeType(fileType));
|
|
|
|
|
|
+ bannerTemplate.setType(bannerTemplateService.judgeType(fileType));
|
|
bannerTemplate.setStatus(0);
|
|
bannerTemplate.setStatus(0);
|
|
bannerTemplate.setAdvertiserId(advertiserId);
|
|
bannerTemplate.setAdvertiserId(advertiserId);
|
|
bannerTemplate.setAgentId(agentId);
|
|
bannerTemplate.setAgentId(agentId);
|
|
@@ -370,7 +370,7 @@ public class BannerTemplateController extends SimpleController {
|
|
if(file != null){
|
|
if(file != null){
|
|
//取得当前上传文件的文件名称(不加路径)
|
|
//取得当前上传文件的文件名称(不加路径)
|
|
uploadName = file.getOriginalFilename();
|
|
uploadName = file.getOriginalFilename();
|
|
- resetName = resetName(uploadName);
|
|
|
|
|
|
+ resetName = bannerTemplateService.resetName(uploadName);
|
|
|
|
|
|
//获取文件类型
|
|
//获取文件类型
|
|
fileType = uploadName.substring(uploadName.lastIndexOf(".") + 1);
|
|
fileType = uploadName.substring(uploadName.lastIndexOf(".") + 1);
|
|
@@ -384,7 +384,7 @@ public class BannerTemplateController extends SimpleController {
|
|
if(uploadName.trim() !=""){
|
|
if(uploadName.trim() !=""){
|
|
|
|
|
|
//处理上传文件路径
|
|
//处理上传文件路径
|
|
- path = getLocalFilePath(resetName);
|
|
|
|
|
|
+ path = bannerTemplateService.getLocalFilePath(resetName);
|
|
|
|
|
|
File localFile = new File(path);
|
|
File localFile = new File(path);
|
|
//上传目录不存在,则新建目录
|
|
//上传目录不存在,则新建目录
|
|
@@ -404,27 +404,27 @@ public class BannerTemplateController extends SimpleController {
|
|
//生成缩略图路径名
|
|
//生成缩略图路径名
|
|
pathSmall = absolutePath.concat(File.separator).concat("small_").concat(resetName);
|
|
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)){
|
|
if((width*_height)==(_width*height)){
|
|
//压缩文件上传路径
|
|
//压缩文件上传路径
|
|
String compressPath =absolutePath.concat(File.separator).concat("compress_").concat(resetName);
|
|
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{
|
|
}else{
|
|
if(width>_width||height>_height){
|
|
if(width>_width||height>_height){
|
|
return "图片尺寸错啦!!";
|
|
return "图片尺寸错啦!!";
|
|
}
|
|
}
|
|
//设置文件上传路径
|
|
//设置文件上传路径
|
|
- bannerTemplate.setPath(replacePath(path));
|
|
|
|
|
|
+ bannerTemplate.setPath(bannerTemplateService.replacePath(path));
|
|
}
|
|
}
|
|
|
|
|
|
//设置缩略图文件路径
|
|
//设置缩略图文件路径
|
|
- bannerTemplate.setPathSmall(replacePath(pathSmall));
|
|
|
|
|
|
+ bannerTemplate.setPathSmall(bannerTemplateService.replacePath(pathSmall));
|
|
bannerTemplate.setName(uploadName);
|
|
bannerTemplate.setName(uploadName);
|
|
- bannerTemplate.setType(judgeType(fileType));
|
|
|
|
|
|
+ bannerTemplate.setType(bannerTemplateService.judgeType(fileType));
|
|
//更新素材
|
|
//更新素材
|
|
bannerTemplateService.edit(bannerTemplate);
|
|
bannerTemplateService.edit(bannerTemplate);
|
|
responseStr="上传成功";
|
|
responseStr="上传成功";
|
|
@@ -439,143 +439,6 @@ public class BannerTemplateController 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;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|